No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

diff.js 4.3 KiB

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