-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (69 loc) · 3.14 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* clone
The MIT License (MIT)
Copyright (c) 2016 Pehr Boman, Hexagon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
"use strict";
function klon (source, target, breakCircular, trace) {
// No need to copy null, undefined or primitive values
// Shortest way first
if ( source === null
|| source === undefined
|| ( source.valueOf() === source
&& source.constructor !== Object
&& !Array.isArray(source)))
return target = source;
var seed,
key,
prop,
circularReference;
// Choose seed for new source.constructor
// - Default seed is undefined
if (!target) {
// Special case for Date
if (source.constructor === Date) seed = source.getTime();
// Everything else, but pure objects and arrays, use themselves as seed
else if (!(Array.isArray(source) || source.constructor === Object)) seed = source;
}
target = target || new source.constructor(seed);
// For tracking circular references
trace = trace || new WeakMap();
for (key in source) {
prop = source[key];
// Check that this is an actual property
if ( !(key in source) || !source.hasOwnProperty(key) || !Object.getOwnPropertyDescriptor(source, key).writable )
continue;
// Circular reference? Store the circular reference and move on to next property
if ( circularReference = trace.get(prop) ) {
if ( !breakCircular ) {
target[key] = circularReference;
continue;
} else {
target[key] = "[Circular]"
continue;
}
}
// Pure objects and arrays need tracing
if ( prop !== null && prop !== undefined && (Array.isArray(prop) || prop.constructor === Object) ) {
trace.set(prop, target[key] = Array.isArray(prop) ? Object.assign([], target[key]) : Object.assign({}, target[key]));
}
// Ok, this seem to be a simple value, assign it
target[key] = klon(prop, target[key], breakCircular, trace);
}
return target;
}
module.exports = klon;