all files / built/src/ field.js

86.51% Statements 109/126
57.58% Branches 38/66
91.43% Functions 32/35
86.99% Lines 107/123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                                                      62×             10×                                             10×                   45×           10×                                               68×                         34×               15×             86×     12× 12×                          
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) Eif (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var mobx_1 = require("mobx");
var ValidationError = (function () {
    function ValidationError(errors) {
        this.errors = typeof errors === "string" ? [errors] : errors;
        this.message = this.errors.join("\n");
    }
    return ValidationError;
}());
exports.ValidationError = ValidationError;
function getErrors(e) {
    Eif (e instanceof ValidationError) {
        return e.errors;
    }
    if (e && e.message) {
        return [e.message];
    }
    try {
        return [JSON.stringify(e)];
    }
    catch (x) {
        return [x.message || "Unknown error"];
    }
}
var Adaptation = (function () {
    function Adaptation(init, modelBox, label, render, parse) {
        this.modelBox = modelBox;
        this.label = label;
        this.render = render;
        this.parse = parse;
        this.modelStore = init;
        this.viewStoreCanonical = this.viewStoreAny = render(init);
        // Set up the initial validation error, if any
        try {
            this.parse(this.viewStoreAny);
            this.errorStore = [];
        }
        catch (error) {
            if (error instanceof ValidationError) {
                this.errorStore = getErrors(error);
            }
            else {
                throw error;
            }
        }
    }
    Object.defineProperty(Adaptation.prototype, "viewFromModel", {
        get: function () {
            return this.render(this.modelBox ? this.modelBox.get() : this.modelStore);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Adaptation.prototype, "view", {
        get: function () {
            // If viewFromModel doesn't match viewStoreCanonical, use viewFromModel:
            return this.viewFromModel !== this.viewStoreCanonical
                ? this.viewFromModel
                : this.viewStoreAny; // otherwise return our view state
        },
        set: function (value) {
            // Make modelBox and modelStore consistent (one way or the other)
            // and set errorStore appropriately:
            try {
                this.modelStore = this.parse(value);
                this.errorStore = [];
                Iif (this.modelBox) {
                    this.modelBox.set(this.modelStore);
                }
            }
            catch (error) {
                Eif (error instanceof ValidationError) {
                    this.errorStore = getErrors(error);
                    Iif (this.modelBox) {
                        this.modelStore = this.modelBox.get();
                    }
                }
                else {
                    throw error;
                }
            }
            this.viewStoreAny = value;
            this.viewStoreCanonical = this.viewFromModel;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Adaptation.prototype, "model", {
        get: function () {
            return this.modelBox ? this.modelBox.get() : this.modelStore;
        },
        set: function (value) {
            Iif (this.modelBox) {
                this.modelBox.set(value);
            }
            this.modelStore = value;
            this.viewStoreCanonical = this.viewStoreAny = this.render(value);
            this.errorStore = [];
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Adaptation.prototype, "error", {
        get: function () {
            return this.viewFromModel !== this.viewStoreCanonical ?
                [] : this.errorStore;
        },
        enumerable: true,
        configurable: true
    });
    Adaptation.prototype.get = function () {
        return this.view;
    };
    Adaptation.prototype.set = function (v) {
        this.view = v;
    };
    Adaptation.prototype.toJSON = function () {
        return this.modelStore;
    };
    return Adaptation;
}());
__decorate([
    mobx_1.observable
], Adaptation.prototype, "modelStore", void 0);
__decorate([
    mobx_1.observable
], Adaptation.prototype, "viewStoreCanonical", void 0);
__decorate([
    mobx_1.observable
], Adaptation.prototype, "viewStoreAny", void 0);
__decorate([
    mobx_1.observable
], Adaptation.prototype, "errorStore", void 0);
__decorate([
    mobx_1.computed
], Adaptation.prototype, "viewFromModel", null);
__decorate([
    mobx_1.computed
], Adaptation.prototype, "view", null);
__decorate([
    mobx_1.computed
], Adaptation.prototype, "model", null);
__decorate([
    mobx_1.computed
], Adaptation.prototype, "error", null);
__decorate([
    mobx_1.action
], Adaptation.prototype, "set", null);
function field(inner) {
    function also(outer) {
        function render(m) {
            return outer.render(inner.render(m));
        }
        ;
        function parse(v) {
            return inner.parse(outer.parse(v));
        }
        ;
        return field({ render: render, parse: parse });
    }
    ;
    function check(check) {
        return also(checker(check));
    }
    function create(value, label) {
        return new Adaptation(value, undefined, label, inner.render, inner.parse);
    }
    function use(box, label) {
        return new Adaptation(box.get(), box, label, inner.render, inner.parse);
    }
    return { also: also, check: check, create: create, use: use };
}
exports.field = field;
function numberAsString(decimalPlaces) {
    var pattern = /^\s*[\-\+]?\d*[\.\,]?\d*\s*$/;
    function render(value) {
        return (decimalPlaces === undefined ?
            value : value.toFixed(decimalPlaces)) + "";
    }
    function parse(str) {
        var value = parseFloat(str);
        Iif (isNaN(value) || !pattern.test(str)) {
            throw new ValidationError("Must be a number");
        }
        return value;
    }
    return { render: render, parse: parse };
}
exports.numberAsString = numberAsString;
function identity() {
    return {
        render: function (value) {
            return value;
        },
        parse: function (value) {
            return value;
        }
    };
}
exports.identity = identity;
function checker(check) {
    return {
        render: function (value) {
            return value;
        },
        parse: function (value) {
            var error = check(value);
            if (error) {
                throw new ValidationError(error);
            }
            return value;
        }
    };
}
exports.checker = checker;
function numberLimits(min, max) {
    return checker(function (val) {
        return (val < min) ? "Minimum value " + min :
            (val > max) ? "Maximum value " + max :
                undefined;
    });
}
exports.numberLimits = numberLimits;
function stringLimits(minLength, maxLength) {
    return checker(function (str) {
        return (str.length < minLength) ? "Minimum length " + minLength + " characters" :
            (str.length > maxLength) ? "Maximum length " + maxLength + " characters" :
                undefined;
    });
}
exports.stringLimits = stringLimits;
//# sourceMappingURL=field.js.map