-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (59 loc) · 1.94 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
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['./ramda'], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('./ramda'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root.ramda);
}
}(this, function(R) {
var toString = Object.prototype.toString;
var cloneDeep = function(obj) {
var refFrom = [];
var refTo = [];
return baseCopy(obj, refFrom, refTo);
}
var baseCopy = function(obj, refFrom, refTo) {
var type = toString.call(obj);
if (type === '[object Object]' || type === '[object Array]') {
return copyObj(obj, refFrom, refTo);
} else if (type === '[object Function]') {
return obj;
} else if (type === '[object Date]') {
return new obj.constructor(obj);
} else {
return obj;
}
};
var copyObj = function(value, refFrom, refTo) {
var copiedValue, idx;
var duplicate = false;
for (idx = 0, len = refFrom.length; idx < len; idx++) {
if (value === refFrom[idx]) {
duplicate = true;
break;
}
}
if (duplicate) {
copiedValue = refTo[idx];
} else {
if (toString.call(value) === '[object Object]') {
copiedValue = {};
} else {
copiedValue = [];
}
refFrom.push(value);
refTo.push(copiedValue);
for (var key in value) {
copiedValue[key] = baseCopy(value[key], refFrom, refTo);
}
}
return copiedValue;
};
return cloneDeep;
}));