@stryke/helpers 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/deep-copy.cjs +38 -35
- package/dist/deep-copy.d.ts +7 -65
- package/dist/deep-copy.mjs +1 -1
- package/package.json +2 -2
package/dist/deep-copy.cjs
CHANGED
|
@@ -3,25 +3,24 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.cloneRegExp = cloneRegExp;
|
|
7
6
|
exports.deepCopy = deepCopy;
|
|
8
|
-
function
|
|
7
|
+
function A(r) {
|
|
9
8
|
const t = new ArrayBuffer(r.byteLength);
|
|
10
9
|
return new Uint8Array(t).set(new Uint8Array(r)), t;
|
|
11
10
|
}
|
|
12
|
-
function
|
|
13
|
-
const t =
|
|
11
|
+
function C(r) {
|
|
12
|
+
const t = A(r.buffer);
|
|
14
13
|
return new DataView(t, r.byteOffset, r.byteLength);
|
|
15
14
|
}
|
|
16
|
-
function
|
|
15
|
+
function g(r) {
|
|
17
16
|
return new Date(r.getTime());
|
|
18
17
|
}
|
|
19
|
-
function
|
|
18
|
+
function d(r) {
|
|
20
19
|
const t = new Map();
|
|
21
20
|
for (const [n, o] of r.entries()) t.set(deepCopy(n), deepCopy(o));
|
|
22
21
|
return t;
|
|
23
22
|
}
|
|
24
|
-
const
|
|
23
|
+
const u = {
|
|
25
24
|
"[object Float32Array]": Float32Array,
|
|
26
25
|
"[object Float64Array]": Float64Array,
|
|
27
26
|
"[object Int8Array]": Int8Array,
|
|
@@ -32,10 +31,10 @@ const y = {
|
|
|
32
31
|
"[object Uint32Array]": Uint32Array,
|
|
33
32
|
"[object Uint8ClampedArray]": Uint8ClampedArray
|
|
34
33
|
},
|
|
35
|
-
|
|
36
|
-
"[object Date]":
|
|
37
|
-
"[object ArrayBuffer]":
|
|
38
|
-
"[object DataView]":
|
|
34
|
+
j = {
|
|
35
|
+
"[object Date]": g,
|
|
36
|
+
"[object ArrayBuffer]": A,
|
|
37
|
+
"[object DataView]": C,
|
|
39
38
|
"[object Float32Array]": e,
|
|
40
39
|
"[object Float64Array]": e,
|
|
41
40
|
"[object Int8Array]": e,
|
|
@@ -48,36 +47,40 @@ const y = {
|
|
|
48
47
|
"[object BigInt64Array]": e,
|
|
49
48
|
"[object BigUint64Array]": e,
|
|
50
49
|
"[object RegExp]": cloneRegExp,
|
|
51
|
-
"[object Map]":
|
|
50
|
+
"[object Map]": d
|
|
52
51
|
};
|
|
53
52
|
function e(r) {
|
|
54
53
|
try {
|
|
55
|
-
|
|
54
|
+
u["[object BigInt64Array]"] = BigInt64Array, u["[object BigUint64Array]"] = BigUint64Array;
|
|
56
55
|
} catch {}
|
|
57
|
-
const t =
|
|
58
|
-
n =
|
|
56
|
+
const t = A(r.buffer),
|
|
57
|
+
n = u[Object.prototype.toString.call(r)];
|
|
59
58
|
if (!n) throw new Error("Unsupported typed array type in `cloneTypedArray`.");
|
|
60
59
|
return new n(t).subarray(r.byteOffset, r.byteOffset + r.length);
|
|
61
60
|
}
|
|
62
|
-
function
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
61
|
+
function b(r, t, n, o, y) {
|
|
62
|
+
const a = detectType(r),
|
|
63
|
+
i = copy(r, a);
|
|
64
|
+
if (!isCollection(a)) return i;
|
|
65
|
+
const f = getKeys(r, a);
|
|
66
|
+
for (const s of f) {
|
|
67
|
+
const c = getValue(r, s, a);
|
|
68
|
+
if (o.has(c)) setValue(t, s, n.get(c), a);else {
|
|
69
|
+
const p = detectType(c),
|
|
70
|
+
l = copy(c, p);
|
|
71
|
+
isCollection(p) && (n.set(c, l), o.add(c)), setValue(t, s, b(c, l, n, o, y), a);
|
|
72
|
+
}
|
|
74
73
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return r;
|
|
74
|
+
return t;
|
|
75
|
+
}
|
|
76
|
+
function deepCopy(r, t) {
|
|
77
|
+
const {
|
|
78
|
+
customizer: n = null
|
|
79
|
+
} = t ?? {},
|
|
80
|
+
o = detectType(r);
|
|
81
|
+
if (!isCollection(o)) return copy(r, o, n);
|
|
82
|
+
const y = copy(r, o, n),
|
|
83
|
+
a = new WeakMap([[r, y]]),
|
|
84
|
+
i = new WeakSet([r]);
|
|
85
|
+
return b(r, y, a, i, n);
|
|
83
86
|
}
|
package/dist/deep-copy.d.ts
CHANGED
|
@@ -1,69 +1,11 @@
|
|
|
1
|
+
export type Options = {
|
|
2
|
+
customizer?: Customizer;
|
|
3
|
+
};
|
|
1
4
|
/**
|
|
2
|
-
*
|
|
5
|
+
* Deep copy value
|
|
3
6
|
*
|
|
4
|
-
* @param
|
|
5
|
-
* @
|
|
6
|
-
*/
|
|
7
|
-
export declare function cloneRegExp(targetRegexp: RegExp): RegExp;
|
|
8
|
-
/**
|
|
9
|
-
* Creates a deep copy of `target`.
|
|
10
|
-
*
|
|
11
|
-
* @see Original source: ts-deepcopy https://github.com/ykdr2017/ts-deepcopy
|
|
12
|
-
* @see Code pen https://codepen.io/erikvullings/pen/ejyBYg
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* **Use this method instead of {@link ./deep-clone#deepClone} if you want to deep copy a value and retain its type (not cloning into a plain object).**
|
|
16
|
-
*
|
|
17
|
-
* This method is a deep copy of the given value. It supports the following types:
|
|
18
|
-
* - `ArrayBuffer`
|
|
19
|
-
* - `DataView`
|
|
20
|
-
* - `Date`
|
|
21
|
-
* - `Map`
|
|
22
|
-
* - `RegExp`
|
|
23
|
-
* - `Set`
|
|
24
|
-
* - `TypedArray`
|
|
25
|
-
* - `WeakMap`
|
|
26
|
-
* - `WeakSet`
|
|
27
|
-
* - `Array`
|
|
28
|
-
* - `Object`
|
|
29
|
-
* - `null`
|
|
30
|
-
* - `undefined`
|
|
31
|
-
* - `string`
|
|
32
|
-
* - `number`
|
|
33
|
-
* - `boolean`
|
|
34
|
-
* - `symbol`
|
|
35
|
-
* - `bigint`
|
|
36
|
-
* - `function`
|
|
37
|
-
* - `Error`
|
|
38
|
-
* - `Promise`
|
|
39
|
-
* - `MapIterator`
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```typescript
|
|
43
|
-
* const original = { a: 1, b: { c: 2 } };
|
|
44
|
-
* const copy = deepCopy(original);
|
|
45
|
-
* console.log(copy); // { a: 1, b: { c: 2 } }
|
|
46
|
-
* console.log(copy !== original); // true
|
|
47
|
-
* console.log(copy.b !== original.b); // true
|
|
48
|
-
*
|
|
49
|
-
* const date = new Date();
|
|
50
|
-
* const dateCopy = deepCopy(date);
|
|
51
|
-
* console.log(dateCopy); // Same date value as `date`
|
|
52
|
-
* console.log(dateCopy !== date); // true
|
|
53
|
-
*
|
|
54
|
-
* const map = new Map([['key', 'value']]);
|
|
55
|
-
* const mapCopy = deepCopy(map);
|
|
56
|
-
* console.log(mapCopy.get('key')); // 'value'
|
|
57
|
-
* console.log(mapCopy !== map); // true
|
|
58
|
-
*
|
|
59
|
-
* const arrayBuffer = new ArrayBuffer(8);
|
|
60
|
-
* const arrayBufferCopy = deepCopy(arrayBuffer);
|
|
61
|
-
* console.log(arrayBufferCopy.byteLength); // 8
|
|
62
|
-
* console.log(arrayBufferCopy !== arrayBuffer); // true
|
|
63
|
-
* ```
|
|
64
|
-
*
|
|
65
|
-
* @param T - Generic type of target/copied value.
|
|
66
|
-
* @param target - Target value to be copied.
|
|
7
|
+
* @param value - The value to copy.
|
|
8
|
+
* @param options - The options object.
|
|
67
9
|
* @returns Returns the copied value.
|
|
68
10
|
*/
|
|
69
|
-
export declare function deepCopy<T
|
|
11
|
+
export declare function deepCopy<T extends Record<string, any>>(value: T, options?: Options): T;
|
package/dist/deep-copy.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
function A(r){const t=new ArrayBuffer(r.byteLength);return new Uint8Array(t).set(new Uint8Array(r)),t}function C(r){const t=A(r.buffer);return new DataView(t,r.byteOffset,r.byteLength)}function g(r){return new Date(r.getTime())}function d(r){const t=new Map;for(const[n,o]of r.entries())t.set(deepCopy(n),deepCopy(o));return t}const u={"[object Float32Array]":Float32Array,"[object Float64Array]":Float64Array,"[object Int8Array]":Int8Array,"[object Int16Array]":Int16Array,"[object Int32Array]":Int32Array,"[object Uint8Array]":Uint8Array,"[object Uint16Array]":Uint16Array,"[object Uint32Array]":Uint32Array,"[object Uint8ClampedArray]":Uint8ClampedArray},j={"[object Date]":g,"[object ArrayBuffer]":A,"[object DataView]":C,"[object Float32Array]":e,"[object Float64Array]":e,"[object Int8Array]":e,"[object Int16Array]":e,"[object Int32Array]":e,"[object Uint8Array]":e,"[object Uint8ClampedArray]":e,"[object Uint16Array]":e,"[object Uint32Array]":e,"[object BigInt64Array]":e,"[object BigUint64Array]":e,"[object RegExp]":cloneRegExp,"[object Map]":d};function e(r){try{u["[object BigInt64Array]"]=BigInt64Array,u["[object BigUint64Array]"]=BigUint64Array}catch{}const t=A(r.buffer),n=u[Object.prototype.toString.call(r)];if(!n)throw new Error("Unsupported typed array type in `cloneTypedArray`.");return new n(t).subarray(r.byteOffset,r.byteOffset+r.length)}function b(r,t,n,o,y){const a=detectType(r),i=copy(r,a);if(!isCollection(a))return i;const f=getKeys(r,a);for(const s of f){const c=getValue(r,s,a);if(o.has(c))setValue(t,s,n.get(c),a);else{const p=detectType(c),l=copy(c,p);isCollection(p)&&(n.set(c,l),o.add(c)),setValue(t,s,b(c,l,n,o,y),a)}}return t}export function deepCopy(r,t){const{customizer:n=null}=t??{},o=detectType(r);if(!isCollection(o))return copy(r,o,n);const y=copy(r,o,n),a=new WeakMap([[r,y]]),i=new WeakSet([r]);return b(r,y,a,i,n)}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/helpers",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing miscellaneous helper functions that are used across many different Storm Software projects.",
|
|
6
6
|
"repository": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"private": false,
|
|
12
12
|
"publishConfig": { "access": "public" },
|
|
13
|
-
"dependencies": { "@stryke/types": ">=0.1.
|
|
13
|
+
"dependencies": { "@stryke/types": ">=0.1.3" },
|
|
14
14
|
"devDependencies": {},
|
|
15
15
|
"sideEffects": false,
|
|
16
16
|
"files": ["dist/**/*"],
|