clone.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. child = new Buffer(parent.length);
  59. parent.copy(child);
  60. return child;
  61. } else {
  62. if (typeof prototype == 'undefined') {
  63. proto = Object.getPrototypeOf(parent);
  64. child = Object.create(proto);
  65. }
  66. else {
  67. child = Object.create(prototype);
  68. proto = prototype;
  69. }
  70. }
  71. if (circular) {
  72. var index = allParents.indexOf(parent);
  73. if (index != -1) {
  74. return allChildren[index];
  75. }
  76. allParents.push(parent);
  77. allChildren.push(child);
  78. }
  79. for (var i in parent) {
  80. var attrs;
  81. if (proto) {
  82. attrs = Object.getOwnPropertyDescriptor(proto, i);
  83. }
  84. if (attrs && attrs.set == null) {
  85. continue;
  86. }
  87. child[i] = _clone(parent[i], depth - 1);
  88. }
  89. return child;
  90. }
  91. return _clone(parent, depth);
  92. }
  93. /**
  94. * Simple flat clone using prototype, accepts only objects, usefull for property
  95. * override on FLAT configuration object (no nested props).
  96. *
  97. * USE WITH CAUTION! This may not behave as you wish if you do not know how this
  98. * works.
  99. */
  100. clone.clonePrototype = function clonePrototype(parent) {
  101. if (parent === null)
  102. return null;
  103. var c = function () {};
  104. c.prototype = parent;
  105. return new c();
  106. };
  107. // private utility functions
  108. function __objToStr(o) {
  109. return Object.prototype.toString.call(o);
  110. };
  111. clone.__objToStr = __objToStr;
  112. function __isDate(o) {
  113. return typeof o === 'object' && __objToStr(o) === '[object Date]';
  114. };
  115. clone.__isDate = __isDate;
  116. function __isArray(o) {
  117. return typeof o === 'object' && __objToStr(o) === '[object Array]';
  118. };
  119. clone.__isArray = __isArray;
  120. function __isRegExp(o) {
  121. return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
  122. };
  123. clone.__isRegExp = __isRegExp;
  124. function __getRegExpFlags(re) {
  125. var flags = '';
  126. if (re.global) flags += 'g';
  127. if (re.ignoreCase) flags += 'i';
  128. if (re.multiline) flags += 'm';
  129. return flags;
  130. };
  131. clone.__getRegExpFlags = __getRegExpFlags;
  132. return clone;
  133. })();
  134. if (typeof module === 'object' && module.exports) {
  135. module.exports = clone;
  136. }