Source

object/difference.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isEqual_1 = __importDefault(require("lodash/isEqual"));
var transform_1 = __importDefault(require("lodash/transform"));
var isObj_1 = __importDefault(require("./isObj"));
/**
 * Source: https://gist.github.com/Yimiprod/7ee176597fef230d1451
 *
 * Deep diff between two next, using lodash
 * @param {Object} next Object compared
 * @param {Object} previous  Object to compare with
 * @return {Object}    Return a new next who represent the diff
 * @category object
 * @module difference
 */
function difference(previous, next, depth) {
    if (depth === void 0) { depth = Infinity; }
    var changes = function (previous, next, currentDepth) {
        if (!previous)
            return next;
        if (!next)
            return null;
        // Get next depth
        var nextDepth = currentDepth + 1;
        // @ts-expect-error: @TODO: fix type ts(2769)
        return (0, transform_1.default)(next, function (result, value, key) {
            // @ts-expect-error: @TODO: Fix type ts(7053)
            if (!(0, isEqual_1.default)(value, previous[key])) {
                // @ts-expect-error: @TODO: Fix type
                result[key] = ((0, isObj_1.default)(value)
                    // @ts-expect-error: @TODO: Fix type ts(7053)
                    && (0, isObj_1.default)(previous[key]))
                    ? (function () {
                        // * Abort if it reaches max depth
                        if (nextDepth > depth)
                            return value;
                        // @ts-expect-error: @TODO: fix type ts(2352)
                        return changes(previous[key], value, nextDepth);
                    })()
                    : value;
            }
        });
    };
    // @ts-expect-error: @TODO: Fix type
    return changes(previous, next, 1);
}
exports.default = difference;