all files / built/src/ core.js

100% Statements 35/35
95.83% Branches 23/24
100% Functions 6/6
100% Lines 35/35
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  200× 200× 99× 99×     101×   200×   95×   525×   183×   262×   82×   81×   80× 78× 78×                
"use strict";
var mobx_1 = require("mobx");
function getOrCreateComputed(obj, key, options) {
    var result;
    if (!Object.prototype.hasOwnProperty.call(obj, key)) {
        var _a = options(), get = _a.get, set = _a.set;
        obj[key] = result = mobx_1.computed(get, set);
    }
    else {
        result = obj[key];
    }
    return result;
}
exports.getOrCreateComputed = getOrCreateComputed;
function isArray(obj) {
    return Array.isArray(obj) || mobx_1.isObservableArray(obj);
}
exports.isArray = isArray;
function hasJsonProperty(obj) {
    return obj && typeof obj === "object" && ("json" in obj);
}
exports.hasJsonProperty = hasJsonProperty;
function canLoadInto(obj) {
    return hasJsonProperty(obj) || (obj && isArray(obj));
}
exports.canLoadInto = canLoadInto;
function save(obj) {
    return hasJsonProperty(obj) ? obj.json : obj;
}
exports.save = save;
function load(obj, data) {
    if (data === "undefined" || !obj) {
        return;
    }
    if (!canLoadInto(obj)) {
        throw new Error("Can only load JSON into an object with a json property, or an array");
    }
    if (hasJsonProperty(obj)) {
        obj.json = data;
        return;
    }
    Eif (isArray(obj)) {
        // Plain array data, so just replace everything
        if (isArray(data)) {
            obj.splice.apply(obj, [0, obj.length].concat(data));
        }
        else {
            obj.length = 0;
        }
        return;
    }
}
exports.load = load;
//# sourceMappingURL=core.js.map