Code coverage report for dist\lib\utils\Omnom.js

Statements: 100% (139 / 139)      Branches: 96.91% (94 / 97)      Functions: 100% (19 / 19)      Lines: 100% (137 / 137)      Ignored: none     

All files » dist/lib/utils/ » Omnom.js
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  1 1 1 1 30 30 30   1   220         1 30 30   1 12   1 159 3 156 13 1 12   143 1 142 2 140 78 62   147 21   126     62 147 3     1 22 22   22 5 5 15 10   5   5 6 5 4 1   8     1     17 10 10 25 4 4   10 6 10 6           11 11 11 44 44 14 30 3   11 3 8 5 8 3   1 32 19 32   1 3 3 3   1 1 1 1   1 10 4 10 4 2   2     6   1 9 2 9 2   7 3 3 3 1 3   4   1 3 2 3   1 158 158 316   158 158 316 233   158   1 96 93 3 3 3 3 12 12 3 3 3 3 12 3 3   1   1    
/// <reference path="../../_references.d.ts" />
var _ = require('lodash');
var MongoDB = require('mongodb');
var Omnom = (function () {
    function Omnom(options) {
        if (options === void 0) { options = {}; }
        this.options = options;
        this._changes = {};
    }
    Object.defineProperty(Omnom.prototype, "changes", {
        get: function () {
            return this._changes;
        },
        enumerable: true,
        configurable: true
    });
    Omnom.prototype.diff = function (original, modified) {
        this.onObject(original, modified);
        return this;
    };
    Omnom.diff = function (original, modified, options) {
        return new Omnom(options).diff(original, modified).changes;
    };
    Omnom.prototype.onObject = function (original, modified, changePath) {
        if (original === undefined || original === null)
            return (original !== modified) && this.set(changePath, modified);
        if (typeof original == 'number' && typeof modified == 'number' && original !== modified) {
            if (this.options.atomicNumbers)
                return this.inc(changePath, modified - original);
            return this.set(changePath, modified);
        }
        if (Array.isArray(original) && Array.isArray(modified))
            return this.onArray(original, modified, changePath);
        if (original instanceof MongoDB.ObjectID && modified instanceof MongoDB.ObjectID)
            return !original.equals(modified) && this.set(changePath, modified);
        if (!_.isPlainObject(original) || !_.isPlainObject(modified))
            return !_.isEqual(original, modified) && this.set(changePath, modified);
        _.each(modified, function (value, key) {
            // Handle array diffs in their own special way
            if (Array.isArray(value) && Array.isArray(original[key]))
                this.onArray(original[key], value, this.resolve(changePath, key));
            else
                this.onObject(original[key], value, this.resolve(changePath, key));
        }, this);
        // Unset removed properties
        _.each(original, function (value, key) {
            if (modified[key] === undefined || modified[key] === null)
                return this.unset(this.resolve(changePath, key));
        }, this);
    };
    Omnom.prototype.onArray = function (original, modified, changePath) {
        var _this = this;
        var i, j;
        // Check if we can get from original => modified using just pulls
        if (original.length > modified.length) {
            var pulls = [];
            for (i = 0, j = 0; i < original.length && j < modified.length; i++) {
                if (this.almostEqual(original[i], modified[j]))
                    j++;
                else
                    pulls.push(original[i]);
            }
            for (; i < original.length; i++)
                pulls.push(original[i]);
            if (j === modified.length) {
                if (pulls.length === 1)
                    return this.pull(changePath, pulls[0]);
                // We can complete using just pulls
                return pulls.forEach(function (pull) { return _this.pull(changePath, pull); });
            }
            else
                return this.set(changePath, modified);
        }
        // Check if we can get from original => modified using just pushes
        if (original.length < modified.length) {
            var canPush = true;
            for (i = 0; i < original.length; i++)
                if (this.almostEqual(original[i], modified[i]) < 1) {
                    canPush = false;
                    break;
                }
            if (canPush) {
                for (i = original.length; i < modified.length; i++)
                    this.push(changePath, modified[i]);
                return;
            }
        }
        // Otherwise, we need to use $set to generate the new array
        // Check how many manipulations would need to be performed, if it's more than half the array size
        // then rather re-create the array
        var sets = [];
        var partials = [];
        for (i = 0; i < modified.length; i++) {
            var equality = this.almostEqual(original[i], modified[i]);
            if (equality === 0)
                sets.push(i);
            else if (equality < 1)
                partials.push(i);
        }
        if (sets.length > modified.length / 2)
            return this.set(changePath, modified);
        for (i = 0; i < sets.length; i++)
            this.set(this.resolve(changePath, sets[i].toString()), modified[sets[i]]);
        for (i = 0; i < partials.length; i++)
            this.onObject(original[partials[i]], modified[partials[i]], this.resolve(changePath, partials[i].toString()));
    };
    Omnom.prototype.set = function (path, value) {
        if (!this.changes.$set)
            this.changes.$set = {};
        this.changes.$set[path] = value;
    };
    Omnom.prototype.unset = function (path) {
        Eif (!this.changes.$unset)
            this.changes.$unset = {};
        this.changes.$unset[path] = 1;
    };
    Omnom.prototype.inc = function (path, value) {
        Eif (!this.changes.$inc)
            this.changes.$inc = {};
        this.changes.$inc[path] = value;
    };
    Omnom.prototype.push = function (path, value) {
        if (!this.changes.$push)
            this.changes.$push = {};
        if (this.changes.$push[path]) {
            if (this.changes.$push[path].$each)
                this.changes.$push[path].$each.push(value);
            else
                this.changes.$push[path] = { $each: [this.changes.$push[path], value] };
        }
        else
            this.changes.$push[path] = value;
    };
    Omnom.prototype.pull = function (path, value) {
        if (!this.changes.$pull)
            this.changes.$pull = {};
        if (this.changes.$pullAll && this.changes.$pullAll[path]) {
            return this.changes.$pullAll[path].push(value);
        }
        if (this.changes.$pull[path]) {
            this.pullAll(path, [this.changes.$pull[path], value]);
            delete this.changes.$pull[path];
            if (_.keys(this.changes.$pull).length === 0)
                delete this.changes.$pull;
            return;
        }
        this.changes.$pull[path] = value;
    };
    Omnom.prototype.pullAll = function (path, values) {
        if (!this.changes.$pullAll)
            this.changes.$pullAll = {};
        this.changes.$pullAll[path] = values;
    };
    Omnom.prototype.resolve = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i - 0] = arguments[_i];
        }
        var validArguments = [];
        args.forEach(function (arg) {
            if (arg)
                validArguments.push(arg);
        });
        return validArguments.join('.');
    };
    Omnom.prototype.almostEqual = function (o1, o2) {
        if (!_.isPlainObject(o1) || !_.isPlainObject(o2))
            return o1 == o2 ? 1 : 0;
        var o1i, o1k = Object.keys(o1);
        var o2k = Object.keys(o2);
        var commonKeys = [];
        for (o1i = 0; o1i < o1k.length; o1i++)
            Eif (~o2k.indexOf(o1k[o1i]))
                commonKeys.push(o1k[o1i]);
        var totalKeys = o1k.length + o2k.length - commonKeys.length;
        var keysDifference = totalKeys - commonKeys.length;
        var requiredChanges = 0;
        for (var i = 0; i < commonKeys.length; i++)
            if (this.almostEqual(o1[commonKeys[i]], o2[commonKeys[i]]) < 1)
                requiredChanges++;
        return 1 - (keysDifference / totalKeys) - (requiredChanges / commonKeys.length);
    };
    return Omnom;
})();
exports.default = Omnom;
 
//# sourceMappingURL=../../lib/utils/Omnom.js.map