cloneDeep.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var toString = Object.prototype.toString;
  4. function cloneDeep(value) {
  5. return cloneDeepHelper(value, new Map());
  6. }
  7. exports.cloneDeep = cloneDeep;
  8. function cloneDeepHelper(val, seen) {
  9. switch (toString.call(val)) {
  10. case "[object Array]": {
  11. if (seen.has(val))
  12. return seen.get(val);
  13. var copy_1 = val.slice(0);
  14. seen.set(val, copy_1);
  15. copy_1.forEach(function (child, i) {
  16. copy_1[i] = cloneDeepHelper(child, seen);
  17. });
  18. return copy_1;
  19. }
  20. case "[object Object]": {
  21. if (seen.has(val))
  22. return seen.get(val);
  23. var copy_2 = Object.create(Object.getPrototypeOf(val));
  24. seen.set(val, copy_2);
  25. Object.keys(val).forEach(function (key) {
  26. copy_2[key] = cloneDeepHelper(val[key], seen);
  27. });
  28. return copy_2;
  29. }
  30. default:
  31. return val;
  32. }
  33. }
  34. //# sourceMappingURL=cloneDeep.js.map