Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_baseClone.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var Stack = require('./_Stack'),
  2. arrayEach = require('./_arrayEach'),
  3. assignValue = require('./_assignValue'),
  4. baseAssign = require('./_baseAssign'),
  5. baseAssignIn = require('./_baseAssignIn'),
  6. cloneBuffer = require('./_cloneBuffer'),
  7. copyArray = require('./_copyArray'),
  8. copySymbols = require('./_copySymbols'),
  9. copySymbolsIn = require('./_copySymbolsIn'),
  10. getAllKeys = require('./_getAllKeys'),
  11. getAllKeysIn = require('./_getAllKeysIn'),
  12. getTag = require('./_getTag'),
  13. initCloneArray = require('./_initCloneArray'),
  14. initCloneByTag = require('./_initCloneByTag'),
  15. initCloneObject = require('./_initCloneObject'),
  16. isArray = require('./isArray'),
  17. isBuffer = require('./isBuffer'),
  18. isMap = require('./isMap'),
  19. isObject = require('./isObject'),
  20. isSet = require('./isSet'),
  21. keys = require('./keys');
  22. /** Used to compose bitmasks for cloning. */
  23. var CLONE_DEEP_FLAG = 1,
  24. CLONE_FLAT_FLAG = 2,
  25. CLONE_SYMBOLS_FLAG = 4;
  26. /** `Object#toString` result references. */
  27. var argsTag = '[object Arguments]',
  28. arrayTag = '[object Array]',
  29. boolTag = '[object Boolean]',
  30. dateTag = '[object Date]',
  31. errorTag = '[object Error]',
  32. funcTag = '[object Function]',
  33. genTag = '[object GeneratorFunction]',
  34. mapTag = '[object Map]',
  35. numberTag = '[object Number]',
  36. objectTag = '[object Object]',
  37. regexpTag = '[object RegExp]',
  38. setTag = '[object Set]',
  39. stringTag = '[object String]',
  40. symbolTag = '[object Symbol]',
  41. weakMapTag = '[object WeakMap]';
  42. var arrayBufferTag = '[object ArrayBuffer]',
  43. dataViewTag = '[object DataView]',
  44. float32Tag = '[object Float32Array]',
  45. float64Tag = '[object Float64Array]',
  46. int8Tag = '[object Int8Array]',
  47. int16Tag = '[object Int16Array]',
  48. int32Tag = '[object Int32Array]',
  49. uint8Tag = '[object Uint8Array]',
  50. uint8ClampedTag = '[object Uint8ClampedArray]',
  51. uint16Tag = '[object Uint16Array]',
  52. uint32Tag = '[object Uint32Array]';
  53. /** Used to identify `toStringTag` values supported by `_.clone`. */
  54. var cloneableTags = {};
  55. cloneableTags[argsTag] = cloneableTags[arrayTag] =
  56. cloneableTags[arrayBufferTag] = cloneableTags[dataViewTag] =
  57. cloneableTags[boolTag] = cloneableTags[dateTag] =
  58. cloneableTags[float32Tag] = cloneableTags[float64Tag] =
  59. cloneableTags[int8Tag] = cloneableTags[int16Tag] =
  60. cloneableTags[int32Tag] = cloneableTags[mapTag] =
  61. cloneableTags[numberTag] = cloneableTags[objectTag] =
  62. cloneableTags[regexpTag] = cloneableTags[setTag] =
  63. cloneableTags[stringTag] = cloneableTags[symbolTag] =
  64. cloneableTags[uint8Tag] = cloneableTags[uint8ClampedTag] =
  65. cloneableTags[uint16Tag] = cloneableTags[uint32Tag] = true;
  66. cloneableTags[errorTag] = cloneableTags[funcTag] =
  67. cloneableTags[weakMapTag] = false;
  68. /**
  69. * The base implementation of `_.clone` and `_.cloneDeep` which tracks
  70. * traversed objects.
  71. *
  72. * @private
  73. * @param {*} value The value to clone.
  74. * @param {boolean} bitmask The bitmask flags.
  75. * 1 - Deep clone
  76. * 2 - Flatten inherited properties
  77. * 4 - Clone symbols
  78. * @param {Function} [customizer] The function to customize cloning.
  79. * @param {string} [key] The key of `value`.
  80. * @param {Object} [object] The parent object of `value`.
  81. * @param {Object} [stack] Tracks traversed objects and their clone counterparts.
  82. * @returns {*} Returns the cloned value.
  83. */
  84. function baseClone(value, bitmask, customizer, key, object, stack) {
  85. var result,
  86. isDeep = bitmask & CLONE_DEEP_FLAG,
  87. isFlat = bitmask & CLONE_FLAT_FLAG,
  88. isFull = bitmask & CLONE_SYMBOLS_FLAG;
  89. if (customizer) {
  90. result = object ? customizer(value, key, object, stack) : customizer(value);
  91. }
  92. if (result !== undefined) {
  93. return result;
  94. }
  95. if (!isObject(value)) {
  96. return value;
  97. }
  98. var isArr = isArray(value);
  99. if (isArr) {
  100. result = initCloneArray(value);
  101. if (!isDeep) {
  102. return copyArray(value, result);
  103. }
  104. } else {
  105. var tag = getTag(value),
  106. isFunc = tag == funcTag || tag == genTag;
  107. if (isBuffer(value)) {
  108. return cloneBuffer(value, isDeep);
  109. }
  110. if (tag == objectTag || tag == argsTag || (isFunc && !object)) {
  111. result = (isFlat || isFunc) ? {} : initCloneObject(value);
  112. if (!isDeep) {
  113. return isFlat
  114. ? copySymbolsIn(value, baseAssignIn(result, value))
  115. : copySymbols(value, baseAssign(result, value));
  116. }
  117. } else {
  118. if (!cloneableTags[tag]) {
  119. return object ? value : {};
  120. }
  121. result = initCloneByTag(value, tag, isDeep);
  122. }
  123. }
  124. // Check for circular references and return its corresponding clone.
  125. stack || (stack = new Stack);
  126. var stacked = stack.get(value);
  127. if (stacked) {
  128. return stacked;
  129. }
  130. stack.set(value, result);
  131. if (isSet(value)) {
  132. value.forEach(function(subValue) {
  133. result.add(baseClone(subValue, bitmask, customizer, subValue, value, stack));
  134. });
  135. return result;
  136. }
  137. if (isMap(value)) {
  138. value.forEach(function(subValue, key) {
  139. result.set(key, baseClone(subValue, bitmask, customizer, key, value, stack));
  140. });
  141. return result;
  142. }
  143. var keysFunc = isFull
  144. ? (isFlat ? getAllKeysIn : getAllKeys)
  145. : (isFlat ? keysIn : keys);
  146. var props = isArr ? undefined : keysFunc(value);
  147. arrayEach(props || value, function(subValue, key) {
  148. if (props) {
  149. key = subValue;
  150. subValue = value[key];
  151. }
  152. // Recursively populate clone (susceptible to call stack limits).
  153. assignValue(result, key, baseClone(subValue, bitmask, customizer, key, value, stack));
  154. });
  155. return result;
  156. }
  157. module.exports = baseClone;