| 5707 | module.exports = clone; |
| 5708 | |
| 5709 | function clone(parent, circular, depth, prototype) { |
| 5710 | var allParents = []; |
| 5711 | var allChildren = []; |
| 5712 | |
| 5713 | var useBuffer = typeof Buffer != 'undefined'; |
| 5714 | |
| 5715 | if (typeof circular == 'undefined') |
| 5716 | circular = true; |
| 5717 | |
| 5718 | if (typeof depth == 'undefined') |
| 5719 | depth = Infinity; |
| 5720 | function _clone(parent, depth) { |
| 5721 | if (parent === null) |
| 5722 | return null; |
| 5723 | |
| 5724 | if (depth == 0) |
| 5725 | return parent; |
| 5726 | |
| 5727 | var child; |
| 5728 | if (typeof parent != 'object') { |
| 5729 | return parent; |
| 5730 | } |
| 5731 | |
| 5732 | if (util.isArray(parent)) { |
| 5733 | child = []; |
| 5734 | } else if (util.isRegExp(parent)) { |
| 5735 | child = new RegExp(parent.source, util.getRegExpFlags(parent)); |
| 5736 | if (parent.lastIndex) child.lastIndex = parent.lastIndex; |
| 5737 | } else if (util.isDate(parent)) { |
| 5738 | child = new Date(parent.getTime()); |
| 5739 | } else if (useBuffer && Buffer.isBuffer(parent)) { |
| 5740 | child = new Buffer(parent.length); |
| 5741 | parent.copy(child); |
| 5742 | return child; |
| 5743 | } else { |
| 5744 | if (typeof prototype == 'undefined') child = Object.create(Object.getPrototypeOf(parent)); |
| 5745 | else child = Object.create(prototype); |
| 5746 | } |
| 5747 | |
| 5748 | if (circular) { |
| 5749 | var index = allParents.indexOf(parent); |
| 5750 | |
| 5751 | if (index != -1) { |
| 5752 | return allChildren[index]; |
| 5753 | } |
| 5754 | allParents.push(parent); |
| 5755 | allChildren.push(child); |
| 5756 | } |
| 5757 | |
| 5758 | for (var i in parent) { |
| 5759 | child[i] = _clone(parent[i], depth - 1); |
| 5760 | } |
| 5761 | |
| 5762 | return child; |
| 5763 | } |
| 5764 | |
| 5765 | return _clone(parent, depth); |
| 5766 | } |