UNPKG

935 BJavaScriptView Raw
1/**
2 * Copyright (C) 2012 Yusuke Suzuki (twitter: @Constellation) and other contributors.
3 * Released under the BSD license.
4 * https://github.com/Constellation/esmangle/blob/master/LICENSE.BSD
5 */
6'use strict';
7
8var isArray = require('isarray');
9
10function deepCopyInternal (obj, result) {
11 var key, val;
12 for (key in obj) {
13 if (key.lastIndexOf('_', 0) === 0) {
14 continue;
15 }
16 if (obj.hasOwnProperty(key)) {
17 val = obj[key];
18 if (typeof val === 'object' && val !== null) {
19 if (val instanceof RegExp) {
20 val = new RegExp(val);
21 } else {
22 val = deepCopyInternal(val, isArray(val) ? [] : {});
23 }
24 }
25 result[key] = val;
26 }
27 }
28 return result;
29}
30
31function deepCopy (obj) {
32 return deepCopyInternal(obj, isArray(obj) ? [] : {});
33}
34
35module.exports = deepCopy;