1 | var _cloneRegExp =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./_cloneRegExp.js");
|
---|
4 |
|
---|
5 | var type =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("../type.js");
|
---|
8 | /**
|
---|
9 | * Copies an object.
|
---|
10 | *
|
---|
11 | * @private
|
---|
12 | * @param {*} value The value to be copied
|
---|
13 | * @param {Boolean} deep Whether or not to perform deep cloning.
|
---|
14 | * @return {*} The copied value.
|
---|
15 | */
|
---|
16 |
|
---|
17 |
|
---|
18 | function _clone(value, deep, map) {
|
---|
19 | map || (map = new _ObjectMap()); // this avoids the slower switch with a quick if decision removing some milliseconds in each run.
|
---|
20 |
|
---|
21 | if (_isPrimitive(value)) {
|
---|
22 | return value;
|
---|
23 | }
|
---|
24 |
|
---|
25 | var copy = function copy(copiedValue) {
|
---|
26 | // Check for circular and same references on the object graph and return its corresponding clone.
|
---|
27 | var cachedCopy = map.get(value);
|
---|
28 |
|
---|
29 | if (cachedCopy) {
|
---|
30 | return cachedCopy;
|
---|
31 | }
|
---|
32 |
|
---|
33 | map.set(value, copiedValue);
|
---|
34 |
|
---|
35 | for (var key in value) {
|
---|
36 | if (Object.prototype.hasOwnProperty.call(value, key)) {
|
---|
37 | copiedValue[key] = deep ? _clone(value[key], true, map) : value[key];
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | return copiedValue;
|
---|
42 | };
|
---|
43 |
|
---|
44 | switch (type(value)) {
|
---|
45 | case 'Object':
|
---|
46 | return copy(Object.create(Object.getPrototypeOf(value)));
|
---|
47 |
|
---|
48 | case 'Array':
|
---|
49 | return copy([]);
|
---|
50 |
|
---|
51 | case 'Date':
|
---|
52 | return new Date(value.valueOf());
|
---|
53 |
|
---|
54 | case 'RegExp':
|
---|
55 | return _cloneRegExp(value);
|
---|
56 |
|
---|
57 | case 'Int8Array':
|
---|
58 | case 'Uint8Array':
|
---|
59 | case 'Uint8ClampedArray':
|
---|
60 | case 'Int16Array':
|
---|
61 | case 'Uint16Array':
|
---|
62 | case 'Int32Array':
|
---|
63 | case 'Uint32Array':
|
---|
64 | case 'Float32Array':
|
---|
65 | case 'Float64Array':
|
---|
66 | case 'BigInt64Array':
|
---|
67 | case 'BigUint64Array':
|
---|
68 | return value.slice();
|
---|
69 |
|
---|
70 | default:
|
---|
71 | return value;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | module.exports = _clone;
|
---|
76 |
|
---|
77 | function _isPrimitive(param) {
|
---|
78 | var type = typeof param;
|
---|
79 | return param == null || type != 'object' && type != 'function';
|
---|
80 | }
|
---|
81 |
|
---|
82 | var _ObjectMap =
|
---|
83 | /*#__PURE__*/
|
---|
84 | function () {
|
---|
85 | function _ObjectMap() {
|
---|
86 | this.map = {};
|
---|
87 | this.length = 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | _ObjectMap.prototype.set = function (key, value) {
|
---|
91 | const hashedKey = this.hash(key);
|
---|
92 | let bucket = this.map[hashedKey];
|
---|
93 |
|
---|
94 | if (!bucket) {
|
---|
95 | this.map[hashedKey] = bucket = [];
|
---|
96 | }
|
---|
97 |
|
---|
98 | bucket.push([key, value]);
|
---|
99 | this.length += 1;
|
---|
100 | };
|
---|
101 |
|
---|
102 | _ObjectMap.prototype.hash = function (key) {
|
---|
103 | let hashedKey = [];
|
---|
104 |
|
---|
105 | for (var value in key) {
|
---|
106 | hashedKey.push(Object.prototype.toString.call(key[value]));
|
---|
107 | }
|
---|
108 |
|
---|
109 | return hashedKey.join();
|
---|
110 | };
|
---|
111 |
|
---|
112 | _ObjectMap.prototype.get = function (key) {
|
---|
113 | /**
|
---|
114 | * depending on the number of objects to be cloned is faster to just iterate over the items in the map just because the hash function is so costly,
|
---|
115 | * on my tests this number is 180, anything above that using the hash function is faster.
|
---|
116 | */
|
---|
117 | if (this.length <= 180) {
|
---|
118 | for (const p in this.map) {
|
---|
119 | const bucket = this.map[p];
|
---|
120 |
|
---|
121 | for (let i = 0; i < bucket.length; i += 1) {
|
---|
122 | const element = bucket[i];
|
---|
123 |
|
---|
124 | if (element[0] === key) {
|
---|
125 | return element[1];
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | const hashedKey = this.hash(key);
|
---|
134 | const bucket = this.map[hashedKey];
|
---|
135 |
|
---|
136 | if (!bucket) {
|
---|
137 | return;
|
---|
138 | }
|
---|
139 |
|
---|
140 | for (let i = 0; i < bucket.length; i += 1) {
|
---|
141 | const element = bucket[i];
|
---|
142 |
|
---|
143 | if (element[0] === key) {
|
---|
144 | return element[1];
|
---|
145 | }
|
---|
146 | }
|
---|
147 | };
|
---|
148 |
|
---|
149 | return _ObjectMap;
|
---|
150 | }(); |
---|