deep_cyclic_copy.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = deepCyclicCopy;
  6. /**
  7. * Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. *
  12. *
  13. */
  14. const EMPTY = new Set();
  15. // $FlowFixMe: Node 6 does not have gOPDs, so we define a simple polyfill for it.
  16. if (!Object.getOwnPropertyDescriptors) {
  17. // $FlowFixMe: polyfill
  18. Object.getOwnPropertyDescriptors = obj => {
  19. const list = {};
  20. Object.getOwnPropertyNames(obj)
  21. .concat(Object.getOwnPropertySymbols(obj))
  22. // $FlowFixMe: assignment with a Symbol is OK.
  23. .forEach(key => (list[key] = Object.getOwnPropertyDescriptor(obj, key)));
  24. return list;
  25. };
  26. }
  27. function deepCyclicCopy(value) {
  28. let options =
  29. arguments.length > 1 && arguments[1] !== undefined
  30. ? arguments[1]
  31. : {blacklist: EMPTY, keepPrototype: false};
  32. let cycles =
  33. arguments.length > 2 && arguments[2] !== undefined
  34. ? arguments[2]
  35. : new WeakMap();
  36. if (typeof value !== 'object' || value === null) {
  37. return value;
  38. } else if (cycles.has(value)) {
  39. return cycles.get(value);
  40. } else if (Array.isArray(value)) {
  41. return deepCyclicCopyArray(value, options, cycles);
  42. } else {
  43. return deepCyclicCopyObject(value, options, cycles);
  44. }
  45. }
  46. function deepCyclicCopyObject(object, options, cycles) {
  47. const newObject = options.keepPrototype
  48. ? Object.create(Object.getPrototypeOf(object))
  49. : {};
  50. // $FlowFixMe: Object.getOwnPropertyDescriptors is polyfilled above.
  51. const descriptors = Object.getOwnPropertyDescriptors(object);
  52. cycles.set(object, newObject);
  53. Object.keys(descriptors).forEach(key => {
  54. if (options.blacklist && options.blacklist.has(key)) {
  55. delete descriptors[key];
  56. return;
  57. }
  58. const descriptor = descriptors[key];
  59. if (typeof descriptor.value !== 'undefined') {
  60. descriptor.value = deepCyclicCopy(
  61. descriptor.value,
  62. {blacklist: EMPTY, keepPrototype: options.keepPrototype},
  63. cycles
  64. );
  65. }
  66. descriptor.configurable = true;
  67. });
  68. return Object.defineProperties(newObject, descriptors);
  69. }
  70. function deepCyclicCopyArray(array, options, cycles) {
  71. const newArray = options.keepPrototype // $FlowFixMe: getPrototypeOf an array is OK.
  72. ? new (Object.getPrototypeOf(array)).constructor(array.length)
  73. : [];
  74. const length = array.length;
  75. cycles.set(array, newArray);
  76. for (let i = 0; i < length; i++) {
  77. newArray[i] = deepCyclicCopy(
  78. array[i],
  79. {blacklist: EMPTY, keepPrototype: options.keepPrototype},
  80. cycles
  81. );
  82. }
  83. return newArray;
  84. }