Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

_baseClone.js 5.5KB

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