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