clone.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. const Reach = require('./reach');
  3. const Types = require('./types');
  4. const Utils = require('./utils');
  5. const internals = {
  6. needsProtoHack: new Set([Types.set, Types.map, Types.weakSet, Types.weakMap])
  7. };
  8. module.exports = internals.clone = function (obj, options = {}, _seen = null) {
  9. if (typeof obj !== 'object' ||
  10. obj === null) {
  11. return obj;
  12. }
  13. let clone = internals.clone;
  14. let seen = _seen;
  15. if (options.shallow) {
  16. if (options.shallow !== true) {
  17. return internals.cloneWithShallow(obj, options);
  18. }
  19. clone = (value) => value;
  20. }
  21. else if (seen) {
  22. const lookup = seen.get(obj);
  23. if (lookup) {
  24. return lookup;
  25. }
  26. }
  27. else {
  28. seen = new Map();
  29. }
  30. // Built-in object types
  31. const baseProto = Types.getInternalProto(obj);
  32. if (baseProto === Types.buffer) {
  33. return Buffer && Buffer.from(obj); // $lab:coverage:ignore$
  34. }
  35. if (baseProto === Types.date) {
  36. return new Date(obj.getTime());
  37. }
  38. if (baseProto === Types.regex) {
  39. return new RegExp(obj);
  40. }
  41. // Generic objects
  42. const newObj = internals.base(obj, baseProto, options);
  43. if (newObj === obj) {
  44. return obj;
  45. }
  46. if (seen) {
  47. seen.set(obj, newObj); // Set seen, since obj could recurse
  48. }
  49. if (baseProto === Types.set) {
  50. for (const value of obj) {
  51. newObj.add(clone(value, options, seen));
  52. }
  53. }
  54. else if (baseProto === Types.map) {
  55. for (const [key, value] of obj) {
  56. newObj.set(key, clone(value, options, seen));
  57. }
  58. }
  59. const keys = Utils.keys(obj, options);
  60. for (const key of keys) {
  61. if (key === '__proto__') {
  62. continue;
  63. }
  64. if (baseProto === Types.array &&
  65. key === 'length') {
  66. newObj.length = obj.length;
  67. continue;
  68. }
  69. const descriptor = Object.getOwnPropertyDescriptor(obj, key);
  70. if (descriptor) {
  71. if (descriptor.get ||
  72. descriptor.set) {
  73. Object.defineProperty(newObj, key, descriptor);
  74. }
  75. else if (descriptor.enumerable) {
  76. newObj[key] = clone(obj[key], options, seen);
  77. }
  78. else {
  79. Object.defineProperty(newObj, key, { enumerable: false, writable: true, configurable: true, value: clone(obj[key], options, seen) });
  80. }
  81. }
  82. else {
  83. Object.defineProperty(newObj, key, {
  84. enumerable: true,
  85. writable: true,
  86. configurable: true,
  87. value: clone(obj[key], options, seen)
  88. });
  89. }
  90. }
  91. return newObj;
  92. };
  93. internals.cloneWithShallow = function (source, options) {
  94. const keys = options.shallow;
  95. options = Object.assign({}, options);
  96. options.shallow = false;
  97. const seen = new Map();
  98. for (const key of keys) {
  99. const ref = Reach(source, key);
  100. if (typeof ref === 'object' ||
  101. typeof ref === 'function') {
  102. seen.set(ref, ref);
  103. }
  104. }
  105. return internals.clone(source, options, seen);
  106. };
  107. internals.base = function (obj, baseProto, options) {
  108. if (options.prototype === false) { // Defaults to true
  109. if (internals.needsProtoHack.has(baseProto)) {
  110. return new baseProto.constructor();
  111. }
  112. return baseProto === Types.array ? [] : {};
  113. }
  114. const proto = Object.getPrototypeOf(obj);
  115. if (proto &&
  116. proto.isImmutable) {
  117. return obj;
  118. }
  119. if (baseProto === Types.array) {
  120. const newObj = [];
  121. if (proto !== baseProto) {
  122. Object.setPrototypeOf(newObj, proto);
  123. }
  124. return newObj;
  125. }
  126. if (internals.needsProtoHack.has(baseProto)) {
  127. const newObj = new proto.constructor();
  128. if (proto !== baseProto) {
  129. Object.setPrototypeOf(newObj, proto);
  130. }
  131. return newObj;
  132. }
  133. return Object.create(proto);
  134. };