不能使用deep-diff javascript函数

时间:2017-06-20 12:43:54

标签: javascript angularjs node.js

我试图在我的angularJS应用程序中使用javascript包。我添加了对index.html的引用

scope :with_template, -> { joins(:email_template) }

我正在我的控制器中使用那个例子:

<script src="deep-diff-0.3.1.min.js"></script>

但是当我尝试运行我的应用程序时,我收到以下错误:

var lhs = {
    name: 'my object',
    description: 'it\'s an object!',
    details: {
        it: 'has',
        an: 'array',
        with: ['a', 'few', 'elements']
    }
};

var rhs = {
    name: 'updated object',
    description: 'it\'s an object!',
    details: {
        it: 'has',
        an: 'array',
        with: ['a', 'few', 'more', 'elements', { than: 'before' }]
    }
};

var differences = diff(lhs, rhs);

我试过指向所有不同的版本,我也试过通过bower和npm安装

1 个答案:

答案 0 :(得分:1)

我认为你必须在代码中使用它:

var diff = require('deep-diff').diff;

你也可以使用下面的函数来做深度差异。我使用它并且效果很好:

&#13;
&#13;
var deepDiffMapper = function () {
    return {
        VALUE_CREATED: 'created',
        VALUE_UPDATED: 'updated',
        VALUE_DELETED: 'deleted',
        VALUE_UNCHANGED: 'unchanged',
        map: function (obj1, obj2) {
            if (this.isFunction(obj1) || this.isFunction(obj2)) {
                throw 'Invalid argument. Function given, object expected.';
            }

            if (this.isValue(obj1) || this.isValue(obj2)) {
                return {
                    type: this.compareValues(obj1, obj2),
                    data1: obj1,
                    data2: obj2
                };
            }

            var diff = {};
            for (var key in obj1) {
                if (this.isFunction(obj1[key])) {
                    continue;
                }

                var value2 = undefined;
                if ('undefined' != typeof (obj2[key])) {
                    value2 = obj2[key];
                }

                var m = this.map(obj1[key], value2);
                diff[key] = m;
            }

            for (var key in obj2) {
                if (this.isFunction(obj2[key]) || ('undefined' != typeof (diff[key]))) {
                    continue;
                }

                var m = this.map(undefined, obj2[key]);
                diff[key] = m;
            }

            return diff;

        },
        compareValues: function (value1, value2) {
            if (value1 === value2) {
                return this.VALUE_UNCHANGED;
            }
            if ('undefined' == typeof (value1)) {
                return this.VALUE_CREATED;
            }
            if ('undefined' == typeof (value2)) {
                return this.VALUE_DELETED;
            }

            return this.VALUE_UPDATED;
        },
        isFunction: function (obj) {
            return {}.toString.apply(obj) === '[object Function]';
        },
        isArray: function (obj) {
            return {}.toString.apply(obj) === '[object Array]';
        },
        isObject: function (obj) {
            return {}.toString.apply(obj) === '[object Object]';
        },
        isValue: function (obj) {
            return !this.isObject(obj) && !this.isArray(obj);
        },
        changed: function (m) {

            if (!this.isObject(m))
                return false;

            var c = false;
            var found = false;

            angular.forEach(m, function (value, key) {

                if (!found) {

                    if (deepDiffMapper.isObject(value))
                        c = (c || deepDiffMapper.changed(value));
                    else if (key == "type") {
                        c = (c || (value == "updated") || (value == "created") || (value == "deleted"));
                    }

                    if (c) {
                        found = true;
                    }
                }
            });

            return c;
        }
    }
}();
&#13;
&#13;
&#13;