1 | var _cloneRegExp =
|
2 |
|
3 | require("./_cloneRegExp");
|
4 |
|
5 | var type =
|
6 |
|
7 | require("../type");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function _clone(value, refFrom, refTo, deep) {
|
21 | var copy = function copy(copiedValue) {
|
22 | var len = refFrom.length;
|
23 | var idx = 0;
|
24 |
|
25 | while (idx < len) {
|
26 | if (value === refFrom[idx]) {
|
27 | return refTo[idx];
|
28 | }
|
29 |
|
30 | idx += 1;
|
31 | }
|
32 |
|
33 | refFrom[idx + 1] = value;
|
34 | refTo[idx + 1] = copiedValue;
|
35 |
|
36 | for (var key in value) {
|
37 | copiedValue[key] = deep ? _clone(value[key], refFrom, refTo, true) : value[key];
|
38 | }
|
39 |
|
40 | return copiedValue;
|
41 | };
|
42 |
|
43 | switch (type(value)) {
|
44 | case 'Object':
|
45 | return copy({});
|
46 |
|
47 | case 'Array':
|
48 | return copy([]);
|
49 |
|
50 | case 'Date':
|
51 | return new Date(value.valueOf());
|
52 |
|
53 | case 'RegExp':
|
54 | return _cloneRegExp(value);
|
55 |
|
56 | default:
|
57 | return value;
|
58 | }
|
59 | }
|
60 |
|
61 | module.exports = _clone; |
\ | No newline at end of file |