@takeshape/util 11.133.0 → 11.133.4

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.
@@ -8,7 +8,7 @@ export declare function deepClone<T>(value: T): T;
8
8
  * @param parent the parent object or array if applicable
9
9
  * @return a new value to use instead of the value argument. Return REMOVE to remove value from the parent. Return undefined to continue to clone recursively.
10
10
  */
11
- type CloneHelper = (value: unknown, key: string | number | undefined, parent: Record<string, unknown> | unknown[] | undefined) => unknown;
11
+ export type CloneHelper = (value: unknown, key: string | number | undefined, parent: Record<string, unknown> | unknown[] | undefined) => unknown;
12
12
  /**
13
13
  * The callback for deepCloneWith which is called for every value in a object
14
14
  * @param value the value to clone
@@ -19,10 +19,23 @@ type CloneHelper = (value: unknown, key: string | number | undefined, parent: Re
19
19
  */
20
20
  export type DeepCloneWithCustomizer = (value: unknown, key: string | number | undefined, parent: Record<string, unknown> | unknown[] | undefined, clone: CloneHelper) => unknown;
21
21
  export declare const REMOVE: unique symbol;
22
+ /**
23
+ * Clone any JSON serializable value and transform using a callback
24
+ * @param obj the value to clone
25
+ * @param customizer a callback that is called for every key/index
26
+ */
27
+ export declare function deepCloneWith(obj: unknown, customizer: DeepCloneWithCustomizer): unknown;
28
+ /**
29
+ * @param value the value to clone
30
+ * @param key the key/index this value is stored under if parent is an object or array
31
+ * @param parent the parent object or array if applicable
32
+ * @param path the path to the current value being cloned
33
+ * @return a new value to use instead of the value argument. Return REMOVE to remove value from the parent. Return undefined to continue to clone recursively.
34
+ */
35
+ export type CloneWithPathHelper = (value: unknown, key: string | number | undefined, parent: Record<string, unknown> | unknown[] | undefined, path: string[]) => unknown;
22
36
  /**
23
37
  * Clone any JSON serializable value and transform using a callback
24
38
  * @param value the value to clone
25
39
  * @param customizer a callback that is called for every key/index
26
40
  */
27
- export declare function deepCloneWith(value: unknown, customizer: DeepCloneWithCustomizer): unknown;
28
- export {};
41
+ export declare function deepCloneWithPath(obj: unknown, customizer: CloneWithPathHelper): unknown;
@@ -33,10 +33,10 @@ function arrayFilter(value) {
33
33
  }
34
34
  /**
35
35
  * Clone any JSON serializable value and transform using a callback
36
- * @param value the value to clone
36
+ * @param obj the value to clone
37
37
  * @param customizer a callback that is called for every key/index
38
38
  */
39
- export function deepCloneWith(value, customizer) {
39
+ export function deepCloneWith(obj, customizer) {
40
40
  const clone = (value, key, parent) => {
41
41
  const newValue = customizer(value, key, parent, clone);
42
42
  if (newValue !== undefined) {
@@ -67,5 +67,44 @@ export function deepCloneWith(value, customizer) {
67
67
  }
68
68
  return value;
69
69
  };
70
- return clone(value, undefined, undefined);
70
+ return clone(obj, undefined, undefined);
71
+ }
72
+ /**
73
+ * Clone any JSON serializable value and transform using a callback
74
+ * @param value the value to clone
75
+ * @param customizer a callback that is called for every key/index
76
+ */
77
+ export function deepCloneWithPath(obj, customizer) {
78
+ const clone = (value, key, parent, path) => {
79
+ const customizedValue = customizer(value, key, parent, path);
80
+ if (customizedValue === REMOVE) {
81
+ return customizedValue;
82
+ }
83
+ const cloneValue = customizedValue ?? value;
84
+ if (Array.isArray(cloneValue)) {
85
+ const { length } = cloneValue;
86
+ const arr = new Array(length);
87
+ let edited = false;
88
+ for (let i = 0; i < length; i++) {
89
+ const newValue = clone(cloneValue[i], i, cloneValue, [...path, String(i)]);
90
+ if (newValue === REMOVE) {
91
+ edited = true;
92
+ }
93
+ arr[i] = newValue;
94
+ }
95
+ return edited ? arr.filter(arrayFilter) : arr;
96
+ }
97
+ if (isIterableObject(cloneValue)) {
98
+ const newObject = {};
99
+ for (const key of Object.keys(cloneValue)) {
100
+ const newValue = clone(cloneValue[key], key, cloneValue, [...path, key]);
101
+ if (newValue !== undefined && newValue !== REMOVE) {
102
+ newObject[key] = newValue;
103
+ }
104
+ }
105
+ return newObject;
106
+ }
107
+ return cloneValue;
108
+ };
109
+ return clone(obj, undefined, undefined, []);
71
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takeshape/util",
3
- "version": "11.133.0",
3
+ "version": "11.133.4",
4
4
  "description": "Shared utilities",
5
5
  "homepage": "https://www.takeshape.io",
6
6
  "repository": {
@@ -45,8 +45,8 @@
45
45
  "tiny-invariant": "1.3.3",
46
46
  "uint8array-extras": "1.4.0",
47
47
  "url-parse": "1.5.3",
48
- "@takeshape/prism": "11.133.0",
49
- "@takeshape/routing": "11.133.0"
48
+ "@takeshape/prism": "11.133.4",
49
+ "@takeshape/routing": "11.133.4"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@types/classnames": "2.2.11",
@@ -76,6 +76,7 @@
76
76
  "test:ci": "TZ=UTC vitest run --reporter=default --reporter=junit --outputFile=\"${GITHUB_WORKSPACE}/test-results/${npm_package_name#*\\/}/vitest-results.xml\" --coverage.enabled --coverage.reportsDirectory=\"${GITHUB_WORKSPACE}/coverage/${npm_package_name#*\\/}\"",
77
77
  "todo": "leasot 'src/**/*.{js,jsx,ts,tsx}'",
78
78
  "typecheck": "tsc --noEmit",
79
- "typecheck:ci": "mkdir -p \"${GITHUB_WORKSPACE}/test-results/${npm_package_name#*\\/}\" && tsc --noEmit --pretty false | typescript-jest-junit-reporter | tee \"${GITHUB_WORKSPACE}/test-results/${npm_package_name#*\\/}/typescript-results.xml\""
79
+ "typecheck:ci": "mkdir -p \"${GITHUB_WORKSPACE}/test-results/${npm_package_name#*\\/}\" && tsc --noEmit --pretty false | typescript-jest-junit-reporter | tee \"${GITHUB_WORKSPACE}/test-results/${npm_package_name#*\\/}/typescript-results.xml\"",
80
+ "watch": "TZ=UTC vitest --watch"
80
81
  }
81
82
  }