_initCloneByTag.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var cloneArrayBuffer = require('./_cloneArrayBuffer'),
  2. cloneDataView = require('./_cloneDataView'),
  3. cloneRegExp = require('./_cloneRegExp'),
  4. cloneSymbol = require('./_cloneSymbol'),
  5. cloneTypedArray = require('./_cloneTypedArray');
  6. /** `Object#toString` result references. */
  7. var boolTag = '[object Boolean]',
  8. dateTag = '[object Date]',
  9. mapTag = '[object Map]',
  10. numberTag = '[object Number]',
  11. regexpTag = '[object RegExp]',
  12. setTag = '[object Set]',
  13. stringTag = '[object String]',
  14. symbolTag = '[object Symbol]';
  15. var arrayBufferTag = '[object ArrayBuffer]',
  16. dataViewTag = '[object DataView]',
  17. float32Tag = '[object Float32Array]',
  18. float64Tag = '[object Float64Array]',
  19. int8Tag = '[object Int8Array]',
  20. int16Tag = '[object Int16Array]',
  21. int32Tag = '[object Int32Array]',
  22. uint8Tag = '[object Uint8Array]',
  23. uint8ClampedTag = '[object Uint8ClampedArray]',
  24. uint16Tag = '[object Uint16Array]',
  25. uint32Tag = '[object Uint32Array]';
  26. /**
  27. * Initializes an object clone based on its `toStringTag`.
  28. *
  29. * **Note:** This function only supports cloning values with tags of
  30. * `Boolean`, `Date`, `Error`, `Map`, `Number`, `RegExp`, `Set`, or `String`.
  31. *
  32. * @private
  33. * @param {Object} object The object to clone.
  34. * @param {string} tag The `toStringTag` of the object to clone.
  35. * @param {boolean} [isDeep] Specify a deep clone.
  36. * @returns {Object} Returns the initialized clone.
  37. */
  38. function initCloneByTag(object, tag, isDeep) {
  39. var Ctor = object.constructor;
  40. switch (tag) {
  41. case arrayBufferTag:
  42. return cloneArrayBuffer(object);
  43. case boolTag:
  44. case dateTag:
  45. return new Ctor(+object);
  46. case dataViewTag:
  47. return cloneDataView(object, isDeep);
  48. case float32Tag: case float64Tag:
  49. case int8Tag: case int16Tag: case int32Tag:
  50. case uint8Tag: case uint8ClampedTag: case uint16Tag: case uint32Tag:
  51. return cloneTypedArray(object, isDeep);
  52. case mapTag:
  53. return new Ctor;
  54. case numberTag:
  55. case stringTag:
  56. return new Ctor(object);
  57. case regexpTag:
  58. return cloneRegExp(object);
  59. case setTag:
  60. return new Ctor;
  61. case symbolTag:
  62. return cloneSymbol(object);
  63. }
  64. }
  65. module.exports = initCloneByTag;