@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.
@@ -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 a(r) {
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 A(r) {
13
- const t = a(r.buffer);
11
+ function C(r) {
12
+ const t = A(r.buffer);
14
13
  return new DataView(t, r.byteOffset, r.byteLength);
15
14
  }
16
- function i(r) {
15
+ function g(r) {
17
16
  return new Date(r.getTime());
18
17
  }
19
- function s(r) {
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 y = {
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
- c = {
36
- "[object Date]": i,
37
- "[object ArrayBuffer]": a,
38
- "[object DataView]": A,
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]": s
50
+ "[object Map]": d
52
51
  };
53
52
  function e(r) {
54
53
  try {
55
- y["[object BigInt64Array]"] = BigInt64Array, y["[object BigUint64Array]"] = BigUint64Array;
54
+ u["[object BigInt64Array]"] = BigInt64Array, u["[object BigUint64Array]"] = BigUint64Array;
56
55
  } catch {}
57
- const t = a(r.buffer),
58
- n = y[Object.prototype.toString.call(r)];
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 cloneRegExp(r) {
63
- const t = new RegExp(r.source, r.flags);
64
- return t.lastIndex = r.lastIndex, t;
65
- }
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));
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
- 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;
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
  }
@@ -1,69 +1,11 @@
1
+ export type Options = {
2
+ customizer?: Customizer;
3
+ };
1
4
  /**
2
- * Creates a clone of `regexp`.
5
+ * Deep copy value
3
6
  *
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.
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>(target: T): T;
11
+ export declare function deepCopy<T extends Record<string, any>>(value: T, options?: Options): T;
@@ -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
+ 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.0",
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.2" },
13
+ "dependencies": { "@stryke/types": ">=0.1.3" },
14
14
  "devDependencies": {},
15
15
  "sideEffects": false,
16
16
  "files": ["dist/**/*"],