@stryke/helpers 0.2.0 → 0.2.1

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.
@@ -3,81 +3,188 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.cloneRegExp = cloneRegExp;
7
6
  exports.deepCopy = deepCopy;
8
- function a(r) {
9
- const t = new ArrayBuffer(r.byteLength);
10
- return new Uint8Array(t).set(new Uint8Array(r)), t;
11
- }
12
- function A(r) {
13
- const t = a(r.buffer);
14
- return new DataView(t, r.byteOffset, r.byteLength);
7
+ var _typeDetect = _interopRequireDefault(require("type-detect"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ const m = typeof Buffer < "u",
10
+ S = m ? Buffer.isBuffer.bind(Buffer) : function (r) {
11
+ return !1;
12
+ },
13
+ I = m ? Buffer.from.bind(Buffer) : function (r) {
14
+ return r;
15
+ };
16
+ function U(n, r) {
17
+ switch (r) {
18
+ case "ArrayBuffer":
19
+ return n.slice(0);
20
+ case "Boolean":
21
+ return !!n.valueOf();
22
+ case "Buffer":
23
+ return I(n);
24
+ case "DataView":
25
+ return new DataView(n.buffer);
26
+ case "Date":
27
+ return new Date(n.getTime());
28
+ case "Number":
29
+ return Number(n);
30
+ case "RegExp":
31
+ return new RegExp(n.source, n.flags);
32
+ case "String":
33
+ return String(n);
34
+ case "Float32Array":
35
+ return new Float32Array(n);
36
+ case "Float64Array":
37
+ return new Float64Array(n);
38
+ case "Int16Array":
39
+ return new Int16Array(n);
40
+ case "Int32Array":
41
+ return new Int32Array(n);
42
+ case "Int8Array":
43
+ return new Int8Array(n);
44
+ case "Uint16Array":
45
+ return new Uint16Array(n);
46
+ case "Uint32Array":
47
+ return new Uint32Array(n);
48
+ case "Uint8Array":
49
+ return new Uint8Array(n);
50
+ case "Uint8ClampedArray":
51
+ return new Uint8ClampedArray(n);
52
+ case "Array Iterator":
53
+ return n;
54
+ case "Map Iterator":
55
+ return n;
56
+ case "Promise":
57
+ return n;
58
+ case "Set Iterator":
59
+ return n;
60
+ case "String Iterator":
61
+ return n;
62
+ case "function":
63
+ return n;
64
+ case "global":
65
+ return n;
66
+ case "WeakMap":
67
+ return n;
68
+ case "WeakSet":
69
+ return n;
70
+ case "boolean":
71
+ return n;
72
+ case "null":
73
+ return n;
74
+ case "number":
75
+ return n;
76
+ case "string":
77
+ return n;
78
+ case "symbol":
79
+ return n;
80
+ case "undefined":
81
+ return n;
82
+ case a:
83
+ return [];
84
+ case c:
85
+ return [];
86
+ case i:
87
+ return new Map();
88
+ case u:
89
+ return {};
90
+ case f:
91
+ return new Set();
92
+ default:
93
+ return n;
94
+ }
15
95
  }
16
- function i(r) {
17
- return new Date(r.getTime());
96
+ function w(n, r, e = null) {
97
+ if (e && r === "Object") {
98
+ const t = e(n, r);
99
+ if (t !== void 0) return t;
100
+ }
101
+ return U(n, r);
18
102
  }
19
- function s(r) {
20
- const t = new Map();
21
- for (const [n, o] of r.entries()) t.set(deepCopy(n), deepCopy(o));
22
- return t;
103
+ const a = "Arguments",
104
+ c = "Array",
105
+ u = "Object",
106
+ i = "Map",
107
+ f = "Set",
108
+ M = new Set([a, c, i, u, f]);
109
+ function g(n) {
110
+ return M.has(n);
23
111
  }
24
- const y = {
25
- "[object Float32Array]": Float32Array,
26
- "[object Float64Array]": Float64Array,
27
- "[object Int8Array]": Int8Array,
28
- "[object Int16Array]": Int16Array,
29
- "[object Int32Array]": Int32Array,
30
- "[object Uint8Array]": Uint8Array,
31
- "[object Uint16Array]": Uint16Array,
32
- "[object Uint32Array]": Uint32Array,
33
- "[object Uint8ClampedArray]": Uint8ClampedArray
34
- },
35
- c = {
36
- "[object Date]": i,
37
- "[object ArrayBuffer]": a,
38
- "[object DataView]": A,
39
- "[object Float32Array]": e,
40
- "[object Float64Array]": e,
41
- "[object Int8Array]": e,
42
- "[object Int16Array]": e,
43
- "[object Int32Array]": e,
44
- "[object Uint8Array]": e,
45
- "[object Uint8ClampedArray]": e,
46
- "[object Uint16Array]": e,
47
- "[object Uint32Array]": e,
48
- "[object BigInt64Array]": e,
49
- "[object BigUint64Array]": e,
50
- "[object RegExp]": cloneRegExp,
51
- "[object Map]": s
52
- };
53
- function e(r) {
54
- try {
55
- y["[object BigInt64Array]"] = BigInt64Array, y["[object BigUint64Array]"] = BigUint64Array;
56
- } catch {}
57
- const t = a(r.buffer),
58
- n = y[Object.prototype.toString.call(r)];
59
- if (!n) throw new Error("Unsupported typed array type in `cloneTypedArray`.");
60
- return new n(t).subarray(r.byteOffset, r.byteOffset + r.length);
112
+ function O(n, r) {
113
+ switch (r) {
114
+ case a:
115
+ case c:
116
+ return Object.keys(n);
117
+ case u:
118
+ return [].concat(Object.keys(n), Object.getOwnPropertySymbols(n));
119
+ case i:
120
+ case f:
121
+ return [...n.keys()];
122
+ default:
123
+ return [];
124
+ }
61
125
  }
62
- function cloneRegExp(r) {
63
- const t = new RegExp(r.source, r.flags);
64
- return t.lastIndex = r.lastIndex, t;
126
+ function V(n, r, e) {
127
+ switch (e) {
128
+ case a:
129
+ case c:
130
+ case u:
131
+ return n[r];
132
+ case i:
133
+ return n.get(r);
134
+ case f:
135
+ return r;
136
+ default:
137
+ }
65
138
  }
66
- function deepCopy(r) {
67
- const t = Object.prototype.toString.call(r);
68
- if (c[t]) return c[t](r);
69
- if (r === null) return r;
70
- if (Array.isArray(r)) {
71
- const n = [];
72
- for (const o of r) n.push(o);
73
- return n.map(o => deepCopy(o));
139
+ function B(n, r, e, t) {
140
+ switch (t) {
141
+ case a:
142
+ case c:
143
+ case u:
144
+ {
145
+ n[r] = e;
146
+ break;
147
+ }
148
+ case i:
149
+ {
150
+ n.set(r, e);
151
+ break;
152
+ }
153
+ case f:
154
+ {
155
+ n.add(e);
156
+ break;
157
+ }
158
+ default:
74
159
  }
75
- if (typeof r == "object") {
76
- const n = {
77
- ...r
78
- };
79
- for (const o of Object.keys(n)) n[o] = deepCopy(n[o]);
80
- return n;
160
+ return n;
161
+ }
162
+ function A(n) {
163
+ return S(n) ? "Buffer" : (0, _typeDetect.default)(n);
164
+ }
165
+ function l(n, r, e, t, y) {
166
+ const s = A(n),
167
+ p = w(n, s);
168
+ if (!g(s)) return p;
169
+ const C = O(n, s);
170
+ for (const k of C) {
171
+ const o = V(n, k, s);
172
+ if (t.has(o)) B(r, k, e.get(o), s);else {
173
+ const d = A(o),
174
+ b = w(o, d);
175
+ g(d) && (e.set(o, b), t.add(o)), B(r, k, l(o, b, e, t, y), s);
176
+ }
81
177
  }
82
178
  return r;
179
+ }
180
+ function deepCopy(n, r) {
181
+ const {
182
+ customizer: e = null
183
+ } = r ?? {},
184
+ t = A(n);
185
+ if (!g(t)) return w(n, t, e);
186
+ const y = w(n, t, e),
187
+ s = new WeakMap([[n, y]]),
188
+ p = new WeakSet([n]);
189
+ return l(n, y, s, p, e);
83
190
  }
@@ -1,69 +1,13 @@
1
+ type Customizer = (value: unknown, type: string) => unknown;
2
+ export type Options = {
3
+ customizer?: Customizer;
4
+ };
1
5
  /**
2
- * Creates a clone of `regexp`.
6
+ * Deep copy value
3
7
  *
4
- * @param targetRegexp - The regexp to clone.
5
- * @returns Returns the cloned regexp.
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.
8
+ * @param value - The value to copy.
9
+ * @param options - The options object.
67
10
  * @returns Returns the copied value.
68
11
  */
69
- export declare function deepCopy<T>(target: T): T;
12
+ export declare function deepCopy<T extends Record<string, any>>(value: T, options?: Options): T;
13
+ export {};
@@ -1 +1 @@
1
- function a(r){const t=new ArrayBuffer(r.byteLength);return new Uint8Array(t).set(new Uint8Array(r)),t}function A(r){const t=a(r.buffer);return new DataView(t,r.byteOffset,r.byteLength)}function i(r){return new Date(r.getTime())}function s(r){const t=new Map;for(const[n,o]of r.entries())t.set(deepCopy(n),deepCopy(o));return t}const y={"[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},c={"[object Date]":i,"[object ArrayBuffer]":a,"[object DataView]":A,"[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]":s};function e(r){try{y["[object BigInt64Array]"]=BigInt64Array,y["[object BigUint64Array]"]=BigUint64Array}catch{}const t=a(r.buffer),n=y[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)}export function cloneRegExp(r){const t=new RegExp(r.source,r.flags);return t.lastIndex=r.lastIndex,t}export function deepCopy(r){const t=Object.prototype.toString.call(r);if(c[t])return c[t](r);if(r===null)return r;if(Array.isArray(r)){const n=[];for(const o of r)n.push(o);return n.map(o=>deepCopy(o))}if(typeof r=="object"){const n={...r};for(const o of Object.keys(n))n[o]=deepCopy(n[o]);return n}return r}
1
+ import R from"type-detect";const m=typeof Buffer<"u",S=m?Buffer.isBuffer.bind(Buffer):function(r){return!1},I=m?Buffer.from.bind(Buffer):function(r){return r};function U(n,r){switch(r){case"ArrayBuffer":return n.slice(0);case"Boolean":return!!n.valueOf();case"Buffer":return I(n);case"DataView":return new DataView(n.buffer);case"Date":return new Date(n.getTime());case"Number":return Number(n);case"RegExp":return new RegExp(n.source,n.flags);case"String":return String(n);case"Float32Array":return new Float32Array(n);case"Float64Array":return new Float64Array(n);case"Int16Array":return new Int16Array(n);case"Int32Array":return new Int32Array(n);case"Int8Array":return new Int8Array(n);case"Uint16Array":return new Uint16Array(n);case"Uint32Array":return new Uint32Array(n);case"Uint8Array":return new Uint8Array(n);case"Uint8ClampedArray":return new Uint8ClampedArray(n);case"Array Iterator":return n;case"Map Iterator":return n;case"Promise":return n;case"Set Iterator":return n;case"String Iterator":return n;case"function":return n;case"global":return n;case"WeakMap":return n;case"WeakSet":return n;case"boolean":return n;case"null":return n;case"number":return n;case"string":return n;case"symbol":return n;case"undefined":return n;case a:return[];case c:return[];case i:return new Map;case u:return{};case f:return new Set;default:return n}}function w(n,r,e=null){if(e&&r==="Object"){const t=e(n,r);if(t!==void 0)return t}return U(n,r)}const a="Arguments",c="Array",u="Object",i="Map",f="Set",M=new Set([a,c,i,u,f]);function g(n){return M.has(n)}function O(n,r){switch(r){case a:case c:return Object.keys(n);case u:return[].concat(Object.keys(n),Object.getOwnPropertySymbols(n));case i:case f:return[...n.keys()];default:return[]}}function V(n,r,e){switch(e){case a:case c:case u:return n[r];case i:return n.get(r);case f:return r;default:}}function B(n,r,e,t){switch(t){case a:case c:case u:{n[r]=e;break}case i:{n.set(r,e);break}case f:{n.add(e);break}default:}return n}function A(n){return S(n)?"Buffer":R(n)}function l(n,r,e,t,y){const s=A(n),p=w(n,s);if(!g(s))return p;const C=O(n,s);for(const k of C){const o=V(n,k,s);if(t.has(o))B(r,k,e.get(o),s);else{const d=A(o),b=w(o,d);g(d)&&(e.set(o,b),t.add(o)),B(r,k,l(o,b,e,t,y),s)}}return r}export function deepCopy(n,r){const{customizer:e=null}=r??{},t=A(n);if(!g(t))return w(n,t,e);const y=w(n,t,e),s=new WeakMap([[n,y]]),p=new WeakSet([n]);return l(n,y,s,p,e)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/helpers",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
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.2" },
13
+ "dependencies": { "type-detect": "^4.1.0", "@stryke/types": ">=0.1.3" },
14
14
  "devDependencies": {},
15
15
  "sideEffects": false,
16
16
  "files": ["dist/**/*"],