clone.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var clone = (function() {
  2. 'use strict';
  3. /**
  4. * Clones (copies) an Object using deep copying.
  5. *
  6. * This function supports circular references by default, but if you are certain
  7. * there are no circular references in your object, you can save some CPU time
  8. * by calling clone(obj, false).
  9. *
  10. * Caution: if `circular` is false and `parent` contains circular references,
  11. * your program may enter an infinite loop and crash.
  12. *
  13. * @param `parent` - the object to be cloned
  14. * @param `circular` - set to true if the object to be cloned may contain
  15. * circular references. (optional - true by default)
  16. * @param `depth` - set to a number if the object is only to be cloned to
  17. * a particular depth. (optional - defaults to Infinity)
  18. * @param `prototype` - sets the prototype to be used when cloning an object.
  19. * (optional - defaults to parent prototype).
  20. */
  21. function clone(parent, circular, depth, prototype) {
  22. var filter;
  23. if (typeof circular === 'object') {
  24. depth = circular.depth;
  25. prototype = circular.prototype;
  26. filter = circular.filter;
  27. circular = circular.circular
  28. }
  29. // maintain two arrays for circular references, where corresponding parents
  30. // and children have the same index
  31. var allParents = [];
  32. var allChildren = [];
  33. var useBuffer = typeof Buffer != 'undefined';
  34. if (typeof circular == 'undefined')
  35. circular = true;
  36. if (typeof depth == 'undefined')
  37. depth = Infinity;
  38. // recurse this function so we don't reset allParents and allChildren
  39. function _clone(parent, depth) {
  40. // cloning null always returns null
  41. if (parent === null)
  42. return null;
  43. if (depth == 0)
  44. return parent;
  45. var child;
  46. var proto;
  47. if (typeof parent != 'object') {
  48. return parent;
  49. }
  50. if (clone.__isArray(parent)) {
  51. child = [];
  52. } else if (clone.__isRegExp(parent)) {
  53. child = new RegExp(parent.source, __getRegExpFlags(parent));
  54. if (parent.lastIndex) child.lastIndex = parent.lastIndex;
  55. } else if (clone.__isDate(parent)) {
  56. child = new Date(parent.getTime());
  57. } else if (useBuffer && Buffer.isBuffer(parent)) {
  58. if (Buffer.allocUnsafe) {
  59. // Node.js >= 4.5.0
  60. child = Buffer.allocUnsafe(parent.length);
  61. } else {
  62. // Older Node.js versions
  63. child = new Buffer(parent.length);
  64. }
  65. parent.copy(child);
  66. return child;
  67. } else {
  68. if (typeof prototype == 'undefined') {
  69. proto = Object.getPrototypeOf(parent);
  70. child = Object.create(proto);
  71. }
  72. else {
  73. child = Object.create(prototype);
  74. proto = prototype;
  75. }
  76. }
  77. if (circular) {
  78. var index = allParents.indexOf(parent);
  79. if (index != -1) {
  80. return allChildren[index];
  81. }
  82. allParents.push(parent);
  83. allChildren.push(child);
  84. }
  85. for (var i in parent) {
  86. var attrs;
  87. if (proto) {
  88. attrs = Object.getOwnPropertyDescriptor(proto, i);
  89. }
  90. if (attrs && attrs.set == null) {
  91. continue;
  92. }
  93. child[i] = _clone(parent[i], depth - 1);
  94. }
  95. return child;
  96. }
  97. return _clone(parent, depth);
  98. }
  99. /**
  100. * Simple flat clone using prototype, accepts only objects, usefull for property
  101. * override on FLAT configuration object (no nested props).
  102. *
  103. * USE WITH CAUTION! This may not behave as you wish if you do not know how this
  104. * works.
  105. */
  106. clone.clonePrototype = function clonePrototype(parent) {
  107. if (parent === null)
  108. return null;
  109. var c = function () {};
  110. c.prototype = parent;
  111. return new c();
  112. };
  113. // private utility functions
  114. function __objToStr(o) {
  115. return Object.prototype.toString.call(o);
  116. };
  117. clone.__objToStr = __objToStr;
  118. function __isDate(o) {
  119. return typeof o === 'object' && __objToStr(o) === '[object Date]';
  120. };
  121. clone.__isDate = __isDate;
  122. function __isArray(o) {
  123. return typeof o === 'object' && __objToStr(o) === '[object Array]';
  124. };
  125. clone.__isArray = __isArray;
  126. function __isRegExp(o) {
  127. return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
  128. };
  129. clone.__isRegExp = __isRegExp;
  130. function __getRegExpFlags(re) {
  131. var flags = '';
  132. if (re.global) flags += 'g';
  133. if (re.ignoreCase) flags += 'i';
  134. if (re.multiline) flags += 'm';
  135. return flags;
  136. };
  137. clone.__getRegExpFlags = __getRegExpFlags;
  138. return clone;
  139. })();
  140. if (typeof module === 'object' && module.exports) {
  141. module.exports = clone;
  142. }