deepCopy.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.deepCopyWith = exports.deepCopy = void 0;
  4. const isBuffer_1 = require("./isBuffer");
  5. exports.deepCopy = obj => {
  6. if (obj === null || typeof obj !== 'object') {
  7. return obj;
  8. }
  9. if (isBuffer_1.isBuffer(obj)) {
  10. return obj.slice();
  11. }
  12. const copy = Array.isArray(obj) ? [] : {};
  13. Object.keys(obj).forEach(key => {
  14. copy[key] = exports.deepCopy(obj[key]);
  15. });
  16. return copy;
  17. };
  18. exports.deepCopyWith = (obj, customizer) => {
  19. function deepCopyWithHelper(value, innerKey, innerObject) {
  20. const result = customizer(value, innerKey, innerObject);
  21. if (result !== undefined)
  22. return result;
  23. if (value === null || typeof value !== 'object') {
  24. return value;
  25. }
  26. if (isBuffer_1.isBuffer(value)) {
  27. return value.slice();
  28. }
  29. const copy = Array.isArray(value) ? [] : {};
  30. Object.keys(value).forEach(k => {
  31. copy[k] = deepCopyWithHelper(value[k], k, value);
  32. });
  33. return copy;
  34. }
  35. if (customizer) {
  36. return deepCopyWithHelper(obj, '', null);
  37. }
  38. else {
  39. return exports.deepCopy(obj);
  40. }
  41. };