diff.js
4.29 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = diff;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* eslint no-proto: 0 */
function createArray() {
var arr = [];
arr.__proto__ = new Array();
arr.__proto__.format = function toString() {
return this.map(function (obj) {
return _objectSpread(_objectSpread({}, obj), {}, {
path: obj.path.join(' > ')
});
});
};
arr.__proto__.toString = function toString() {
return JSON.stringify(this.format(), null, 2);
};
return arr;
}
function diff(obj1, obj2) {
var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
var diffList = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : createArray();
if (depth <= 0) return diffList;
var keys = new Set([].concat(_toConsumableArray(Object.keys(obj1)), _toConsumableArray(Object.keys(obj2))));
keys.forEach(function (key) {
var value1 = obj1[key];
var value2 = obj2[key]; // Same value
if (value1 === value2) return;
var type1 = _typeof(value1);
var type2 = _typeof(value2); // Diff type
if (type1 !== type2) {
diffList.push({
path: path.concat(key),
value1: value1,
value2: value2
});
return;
} // NaN
if (Number.isNaN(value1) && Number.isNaN(value2)) {
return;
} // Object & Array
if (type1 === 'object' && value1 !== null && value2 !== null) {
diff(value1, value2, depth - 1, path.concat(key), diffList);
return;
} // Rest
diffList.push({
path: path.concat(key),
value1: value1,
value2: value2
});
});
return diffList;
}