Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseClone = require('../internal/baseClone'),
2
bindCallback = require('../internal/bindCallback'),
3
isIterateeCall = require('../internal/isIterateeCall');
4
5
/**
6
* Creates a clone of `value`. If `isDeep` is `true` nested objects are cloned,
7
* otherwise they are assigned by reference. If `customizer` is provided it is
8
* invoked to produce the cloned values. If `customizer` returns `undefined`
9
* cloning is handled by the method instead. The `customizer` is bound to
10
* `thisArg` and invoked with two argument; (value [, index|key, object]).
11
*
12
* **Note:** This method is loosely based on the
13
* [structured clone algorithm](http://www.w3.org/TR/html5/infrastructure.html#internal-structured-cloning-algorithm).
14
* The enumerable properties of `arguments` objects and objects created by
15
* constructors other than `Object` are cloned to plain `Object` objects. An
16
* empty object is returned for uncloneable values such as functions, DOM nodes,
17
* Maps, Sets, and WeakMaps.
18
*
19
* @static
20
* @memberOf _
21
* @category Lang
22
* @param {*} value The value to clone.
23
* @param {boolean} [isDeep] Specify a deep clone.
24
* @param {Function} [customizer] The function to customize cloning values.
25
* @param {*} [thisArg] The `this` binding of `customizer`.
26
* @returns {*} Returns the cloned value.
27
* @example
28
*
29
* var users = [
30
* { 'user': 'barney' },
31
* { 'user': 'fred' }
32
* ];
33
*
34
* var shallow = _.clone(users);
35
* shallow[0] === users[0];
36
* // => true
37
*
38
* var deep = _.clone(users, true);
39
* deep[0] === users[0];
40
* // => false
41
*
42
* // using a customizer callback
43
* var el = _.clone(document.body, function(value) {
44
* if (_.isElement(value)) {
45
* return value.cloneNode(false);
46
* }
47
* });
48
*
49
* el === document.body
50
* // => false
51
* el.nodeName
52
* // => BODY
53
* el.childNodes.length;
54
* // => 0
55
*/
56
function clone(value, isDeep, customizer, thisArg) {
57
if (isDeep && typeof isDeep != 'boolean' && isIterateeCall(value, isDeep, customizer)) {
58
isDeep = false;
59
}
60
else if (typeof isDeep == 'function') {
61
thisArg = customizer;
62
customizer = isDeep;
63
isDeep = false;
64
}
65
return typeof customizer == 'function'
66
? baseClone(value, isDeep, bindCallback(customizer, thisArg, 1))
67
: baseClone(value, isDeep);
68
}
69
70
module.exports = clone;
71
72