-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (48 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* 替换,如果存在 replacer,则替换为 replacer(src),
* @param replacer
* @param src
* @param dist
* @returns {*}
*/
function replace(replacer, src, dist) {
return replacer ? replacer(src) : dist;
}
/**
* nano version deep clone
* @param v
* @param replacer 可选
* @returns {*}
*/
function clone(v, replacer) {
// null, undefined
if (v === null || v === undefined) return replace(replacer, v, v);
var t = typeof v;
// number, string
if (t === 'number' || t === 'string') return replace(replacer, v, v);
// TODO Symbol
// if (t === 'symbol') return replace(replacer, v, v);
// function
if (t === 'function') return replace(replacer, v, v);
// Date
if (v instanceof Date) return replace(replacer, v, new Date(v.getTime()));
// RegExp
if (v instanceof RegExp) return replace(replacer, v, new RegExp(v));
// array
if (Array.isArray(v)) {
return v.map(function(item) {
return clone(item, replacer);
});
}
// object
if (v instanceof Object) {
var nv = {};
for (var k in v) {
nv[k] = clone(v[k], replacer);
}
return nv;
}
// others, keep it
return v;
}
module.exports = clone;