You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

86 lines
4.2 KiB

  1. 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); }
  2. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  3. 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."); }
  4. 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(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
  6. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  7. 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; }
  8. 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; }
  9. 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; }
  10. 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; }
  11. /* eslint no-proto: 0 */
  12. function createArray() {
  13. var arr = [];
  14. arr.__proto__ = new Array();
  15. arr.__proto__.format = function toString() {
  16. return this.map(function (obj) {
  17. return _objectSpread({}, obj, {
  18. path: obj.path.join(' > ')
  19. });
  20. });
  21. };
  22. arr.__proto__.toString = function toString() {
  23. return JSON.stringify(this.format(), null, 2);
  24. };
  25. return arr;
  26. }
  27. export default function diff(obj1, obj2) {
  28. var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
  29. var path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
  30. var diffList = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : createArray();
  31. if (depth <= 0) return diffList;
  32. var keys = new Set([].concat(_toConsumableArray(Object.keys(obj1)), _toConsumableArray(Object.keys(obj2))));
  33. keys.forEach(function (key) {
  34. var value1 = obj1[key];
  35. var value2 = obj2[key]; // Same value
  36. if (value1 === value2) return;
  37. var type1 = _typeof(value1);
  38. var type2 = _typeof(value2); // Diff type
  39. if (type1 !== type2) {
  40. diffList.push({
  41. path: path.concat(key),
  42. value1: value1,
  43. value2: value2
  44. });
  45. return;
  46. } // NaN
  47. if (Number.isNaN(value1) && Number.isNaN(value2)) {
  48. return;
  49. } // Object & Array
  50. if (type1 === 'object' && value1 !== null && value2 !== null) {
  51. diff(value1, value2, depth - 1, path.concat(key), diffList);
  52. return;
  53. } // Rest
  54. diffList.push({
  55. path: path.concat(key),
  56. value1: value1,
  57. value2: value2
  58. });
  59. });
  60. return diffList;
  61. }