@arrai-innovations/reactive-helpers 20.1.2 → 20.1.3
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/index.js +1 -0
- package/package.json +1 -1
- package/types/index.d.ts +1 -0
- package/types/utils/deepUnref.d.ts +2 -22
- package/types/utils/deepUnref.d.ts.map +1 -1
- package/types/utils/deleteKey.d.ts +2 -1
- package/types/utils/deleteKey.d.ts.map +1 -1
- package/types/utils/flattenPathsWithValues.d.ts +30 -0
- package/types/utils/flattenPathsWithValues.d.ts.map +1 -0
- package/types/utils/keyDiff.d.ts +9 -9
- package/types/utils/keyDiff.d.ts.map +1 -1
- package/utils/deepUnref.js +15 -2
- package/utils/deleteKey.js +4 -2
- package/utils/flattenPathsWithValues.js +65 -0
- package/utils/keyDiff.js +5 -5
package/index.js
CHANGED
|
@@ -30,6 +30,7 @@ export * from "./utils/compact.js";
|
|
|
30
30
|
export * from "./utils/deepUnref.js";
|
|
31
31
|
export * from "./utils/deleteKey.js";
|
|
32
32
|
export * from "./utils/flattenPaths.js";
|
|
33
|
+
export * from "./utils/flattenPathsWithValues.js";
|
|
33
34
|
export * from "./utils/getFakePk.js";
|
|
34
35
|
export * from "./utils/keepAliveTry.js";
|
|
35
36
|
export * from "./utils/keyDiff.js";
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export * from "./utils/compact.js";
|
|
|
29
29
|
export * from "./utils/deepUnref.js";
|
|
30
30
|
export * from "./utils/deleteKey.js";
|
|
31
31
|
export * from "./utils/flattenPaths.js";
|
|
32
|
+
export * from "./utils/flattenPathsWithValues.js";
|
|
32
33
|
export * from "./utils/getFakePk.js";
|
|
33
34
|
export * from "./utils/keepAliveTry.js";
|
|
34
35
|
export * from "./utils/keyDiff.js";
|
|
@@ -1,26 +1,6 @@
|
|
|
1
|
+
export function deepUnref<T>(val: T): DeepUnwrap<T> | T;
|
|
1
2
|
/**
|
|
2
3
|
* Recursively unwraps refs from a nested object, array, or primitive.
|
|
3
|
-
*
|
|
4
|
-
* @template T
|
|
5
|
-
* @typedef {T extends import('vue').Ref<infer U>
|
|
6
|
-
* ? DeepUnwrap<U>
|
|
7
|
-
* : T extends Array<infer V>
|
|
8
|
-
* ? Array<DeepUnwrap<V>>
|
|
9
|
-
* : T extends object
|
|
10
|
-
* ? { [K in keyof T]: DeepUnwrap<T[K]> }
|
|
11
|
-
* : T
|
|
12
|
-
* } DeepUnwrap
|
|
13
4
|
*/
|
|
14
|
-
|
|
15
|
-
* Safe, recursively-typed deep unref.
|
|
16
|
-
*
|
|
17
|
-
* @template T
|
|
18
|
-
* @param {T} val - The value to deeply unwrap.
|
|
19
|
-
* @returns {DeepUnwrap<T>} - The deeply unwrapped value.
|
|
20
|
-
*/
|
|
21
|
-
export const deepUnref: any;
|
|
22
|
-
/**
|
|
23
|
-
* Recursively unwraps refs from a nested object, array, or primitive.
|
|
24
|
-
*/
|
|
25
|
-
export type DeepUnwrap<T_1> = T_1 extends import("vue").Ref<infer U> ? DeepUnwrap<U> : T_1 extends Array<infer V> ? Array<DeepUnwrap<V>> : T_1 extends object ? { [K in keyof T_1]: DeepUnwrap<T_1[K]>; } : T_1;
|
|
5
|
+
export type DeepUnwrap<T> = T extends import("vue").Ref<infer U> ? DeepUnwrap<U> : T extends Array<infer V> ? Array<DeepUnwrap<V>> : T extends object ? { [K in keyof T]: DeepUnwrap<T[K]>; } : T;
|
|
26
6
|
//# sourceMappingURL=deepUnref.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deepUnref.d.ts","sourceRoot":"","sources":["../../utils/deepUnref.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"deepUnref.d.ts","sourceRoot":"","sources":["../../utils/deepUnref.js"],"names":[],"mappings":"AA2BO,0BAJM,CAAC,OACH,CAAC,GACC,UAAU,CAAC,CAAC,CAAC,GAAC,CAAC,CAe3B;;;;uBAjCY,CAAC,IACD,CAAC,SAAS,OAAO,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,GAC1C,UAAU,CAAC,CAAC,CAAC,GACb,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GACtB,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GACpB,CAAC,SAAS,MAAM,GACd,GAAG,CAAC,IAAI,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAE,GACpC,CAAC"}
|
|
@@ -11,6 +11,7 @@ export function lodashLikePathSplit(string: string, object: object): string[];
|
|
|
11
11
|
*
|
|
12
12
|
* @param {object} obj - The object to modify.
|
|
13
13
|
* @param {string} path - The key to delete.
|
|
14
|
+
* @returns {boolean} Returns true if the key was deleted, false otherwise.
|
|
14
15
|
*/
|
|
15
|
-
export function del(obj: object, path: string):
|
|
16
|
+
export function del(obj: object, path: string): boolean;
|
|
16
17
|
//# sourceMappingURL=deleteKey.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deleteKey.d.ts","sourceRoot":"","sources":["../../utils/deleteKey.js"],"names":[],"mappings":"AAgBA;;;;;;GAMG;AACH,4CAJW,MAAM,UACN,MAAM,GACJ,MAAM,EAAE,CAkBpB;AAED
|
|
1
|
+
{"version":3,"file":"deleteKey.d.ts","sourceRoot":"","sources":["../../utils/deleteKey.js"],"names":[],"mappings":"AAgBA;;;;;;GAMG;AACH,4CAJW,MAAM,UACN,MAAM,GACJ,MAAM,EAAE,CAkBpB;AAED;;;;;;GAMG;AACH,yBAJW,MAAM,QACN,MAAM,GACJ,OAAO,CAkBnB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get all primitive paths and their corresponding values from an array or object.
|
|
3
|
+
*
|
|
4
|
+
* @module utils/flattenPathsWithValues.js
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Turn an array or object into an array of `[path, value]` pairs (for primitives)
|
|
8
|
+
* and a list of container paths (for arrays and objects).
|
|
9
|
+
*
|
|
10
|
+
* Array indexes are wrapped in square brackets and object keys are prefixed with a period.
|
|
11
|
+
*
|
|
12
|
+
* @param {any[] | object} arrayOrObject - Array or object to flatten.
|
|
13
|
+
* @param {object} [options] - Options.
|
|
14
|
+
* @param {string} [options.currentPath=""] - Current path, for recursion or as a starting point.
|
|
15
|
+
* @param {number} [options.depth=0] - Current depth, for recursion.
|
|
16
|
+
* @param {number} [options.limit=0] - Limit the depth of recursion.
|
|
17
|
+
* @returns {{
|
|
18
|
+
* pathValues: [string, any][],
|
|
19
|
+
* containerPaths: string[]
|
|
20
|
+
* }} - Paths and their corresponding values.
|
|
21
|
+
*/
|
|
22
|
+
export function flattenPathsWithValues(arrayOrObject: any[] | object, { currentPath, depth, limit }?: {
|
|
23
|
+
currentPath?: string;
|
|
24
|
+
depth?: number;
|
|
25
|
+
limit?: number;
|
|
26
|
+
}): {
|
|
27
|
+
pathValues: [string, any][];
|
|
28
|
+
containerPaths: string[];
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=flattenPathsWithValues.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flattenPathsWithValues.d.ts","sourceRoot":"","sources":["../../utils/flattenPathsWithValues.js"],"names":[],"mappings":"AAGA;;;;GAIG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,sDAVW,GAAG,EAAE,GAAG,MAAM,kCAEtB;IAAyB,WAAW,GAA5B,MAAM;IACW,KAAK,GAAtB,MAAM;IACW,KAAK,GAAtB,MAAM;CACd,GAAU;IACR,UAAU,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;IAC5B,cAAc,EAAE,MAAM,EAAE,CAAA;CACzB,CAyCH"}
|
package/types/utils/keyDiff.d.ts
CHANGED
|
@@ -8,23 +8,23 @@
|
|
|
8
8
|
* Result object of keyDiff and keyDiffDeep.
|
|
9
9
|
*
|
|
10
10
|
* @typedef {object} KeyDiffResult
|
|
11
|
-
* @property {Set} [sameKeys] - If sameKeys option is true, return keys that are the same.
|
|
12
|
-
* @property {Set} [removedKeys] - If removedKeys option is true, return keys that are removed.
|
|
13
|
-
* @property {Set} [addedKeys] - If addedKeys option is true, return keys that are added.
|
|
11
|
+
* @property {Set<string>} [sameKeys] - If sameKeys option is true, return keys that are the same.
|
|
12
|
+
* @property {Set<string>} [removedKeys] - If removedKeys option is true, return keys that are removed.
|
|
13
|
+
* @property {Set<string>} [addedKeys] - If addedKeys option is true, return keys that are added.
|
|
14
14
|
*/
|
|
15
15
|
/**
|
|
16
16
|
* Calculate the difference between two arrays of keys, in terms of what keys
|
|
17
17
|
* are the same, what keys are removed, and what keys are added.
|
|
18
18
|
*
|
|
19
|
-
* @param {string[]|Set} newKeys - Keys to consider as new.
|
|
20
|
-
* @param {string[]|Set} oldKeys - Keys to consider as old.
|
|
19
|
+
* @param {string[]|Set<string>} newKeys - Keys to consider as new.
|
|
20
|
+
* @param {string[]|Set<string>} oldKeys - Keys to consider as old.
|
|
21
21
|
* @param {object} [options] - Which differences are returned.
|
|
22
22
|
* @param {boolean} [options.sameKeys=true] - If true, return keys that are the same.
|
|
23
23
|
* @param {boolean} [options.removedKeys=true] - If true, return keys that are removed.
|
|
24
24
|
* @param {boolean} [options.addedKeys=true] - If true, return keys that are added.
|
|
25
25
|
* @returns {KeyDiffResult} - The differences.
|
|
26
26
|
*/
|
|
27
|
-
export function keyDiff(newKeys: string[] | Set<
|
|
27
|
+
export function keyDiff(newKeys: string[] | Set<string>, oldKeys: string[] | Set<string>, { sameKeys, removedKeys, addedKeys }?: {
|
|
28
28
|
sameKeys?: boolean;
|
|
29
29
|
removedKeys?: boolean;
|
|
30
30
|
addedKeys?: boolean;
|
|
@@ -50,14 +50,14 @@ export type KeyDiffResult = {
|
|
|
50
50
|
/**
|
|
51
51
|
* - If sameKeys option is true, return keys that are the same.
|
|
52
52
|
*/
|
|
53
|
-
sameKeys?: Set<
|
|
53
|
+
sameKeys?: Set<string>;
|
|
54
54
|
/**
|
|
55
55
|
* - If removedKeys option is true, return keys that are removed.
|
|
56
56
|
*/
|
|
57
|
-
removedKeys?: Set<
|
|
57
|
+
removedKeys?: Set<string>;
|
|
58
58
|
/**
|
|
59
59
|
* - If addedKeys option is true, return keys that are added.
|
|
60
60
|
*/
|
|
61
|
-
addedKeys?: Set<
|
|
61
|
+
addedKeys?: Set<string>;
|
|
62
62
|
};
|
|
63
63
|
//# sourceMappingURL=keyDiff.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyDiff.d.ts","sourceRoot":"","sources":["../../utils/keyDiff.js"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH;;;;;;;GAOG;AAEH;;;;;;;;;;;GAWG;AACH,iCARW,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"keyDiff.d.ts","sourceRoot":"","sources":["../../utils/keyDiff.js"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH;;;;;;;GAOG;AAEH;;;;;;;;;;;GAWG;AACH,iCARW,MAAM,EAAE,GAAC,GAAG,CAAC,MAAM,CAAC,WACpB,MAAM,EAAE,GAAC,GAAG,CAAC,MAAM,CAAC,yCAE5B;IAA0B,QAAQ,GAA1B,OAAO;IACW,WAAW,GAA7B,OAAO;IACW,SAAS,GAA3B,OAAO;CACf,GAAU,aAAa,CAgBzB;AAED;;;;;;;;;;;;GAYG;AACH,oCATW,MAAM,UACN,MAAM,YACN,MAAM,GAKJ,aAAa,CAUzB;;;;;;;;eAtDa,GAAG,CAAC,MAAM,CAAC;;;;kBACX,GAAG,CAAC,MAAM,CAAC;;;;gBACX,GAAG,CAAC,MAAM,CAAC"}
|
package/utils/deepUnref.js
CHANGED
|
@@ -23,6 +23,19 @@ import { deepUnref as _deepUnref } from "vue-deepunref";
|
|
|
23
23
|
*
|
|
24
24
|
* @template T
|
|
25
25
|
* @param {T} val - The value to deeply unwrap.
|
|
26
|
-
* @returns {DeepUnwrap<T
|
|
26
|
+
* @returns {DeepUnwrap<T>|T} - The deeply unwrapped value.
|
|
27
27
|
*/
|
|
28
|
-
export const deepUnref =
|
|
28
|
+
export const deepUnref = (val) => {
|
|
29
|
+
if (
|
|
30
|
+
val instanceof Date ||
|
|
31
|
+
val instanceof RegExp ||
|
|
32
|
+
val instanceof Map ||
|
|
33
|
+
val instanceof Set ||
|
|
34
|
+
val instanceof WeakMap ||
|
|
35
|
+
val instanceof WeakSet
|
|
36
|
+
) {
|
|
37
|
+
return val;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return _deepUnref(val);
|
|
41
|
+
};
|
package/utils/deleteKey.js
CHANGED
|
@@ -44,10 +44,11 @@ export function lodashLikePathSplit(string, object) {
|
|
|
44
44
|
*
|
|
45
45
|
* @param {object} obj - The object to modify.
|
|
46
46
|
* @param {string} path - The key to delete.
|
|
47
|
+
* @returns {boolean} Returns true if the key was deleted, false otherwise.
|
|
47
48
|
*/
|
|
48
49
|
export function del(obj, path) {
|
|
49
50
|
if (!obj) {
|
|
50
|
-
return;
|
|
51
|
+
return false;
|
|
51
52
|
}
|
|
52
53
|
const pathArray = lodashLikePathSplit(path, obj);
|
|
53
54
|
let index = 0;
|
|
@@ -57,7 +58,8 @@ export function del(obj, path) {
|
|
|
57
58
|
obj = obj[toKey(pathArray[index++])];
|
|
58
59
|
}
|
|
59
60
|
if (!obj) {
|
|
60
|
-
return;
|
|
61
|
+
return false;
|
|
61
62
|
}
|
|
62
63
|
delete obj[toKey(pathArray[index])];
|
|
64
|
+
return true;
|
|
63
65
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import isArray from "lodash-es/isArray.js";
|
|
2
|
+
import isObject from "lodash-es/isObject.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get all primitive paths and their corresponding values from an array or object.
|
|
6
|
+
*
|
|
7
|
+
* @module utils/flattenPathsWithValues.js
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Turn an array or object into an array of `[path, value]` pairs (for primitives)
|
|
12
|
+
* and a list of container paths (for arrays and objects).
|
|
13
|
+
*
|
|
14
|
+
* Array indexes are wrapped in square brackets and object keys are prefixed with a period.
|
|
15
|
+
*
|
|
16
|
+
* @param {any[] | object} arrayOrObject - Array or object to flatten.
|
|
17
|
+
* @param {object} [options] - Options.
|
|
18
|
+
* @param {string} [options.currentPath=""] - Current path, for recursion or as a starting point.
|
|
19
|
+
* @param {number} [options.depth=0] - Current depth, for recursion.
|
|
20
|
+
* @param {number} [options.limit=0] - Limit the depth of recursion.
|
|
21
|
+
* @returns {{
|
|
22
|
+
* pathValues: [string, any][],
|
|
23
|
+
* containerPaths: string[]
|
|
24
|
+
* }} - Paths and their corresponding values.
|
|
25
|
+
*/
|
|
26
|
+
export function flattenPathsWithValues(arrayOrObject, { currentPath = "", depth = 0, limit = 0 } = {}) {
|
|
27
|
+
/** @type {[string, any][]} */
|
|
28
|
+
const pathValues = [];
|
|
29
|
+
/** @type {string[]} */
|
|
30
|
+
const containerPaths = [];
|
|
31
|
+
|
|
32
|
+
const keysOrIndexes = isArray(arrayOrObject);
|
|
33
|
+
const dotOrNot = currentPath ? "." : "";
|
|
34
|
+
|
|
35
|
+
if (limit && depth >= limit) {
|
|
36
|
+
return { pathValues, containerPaths };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (isObject(arrayOrObject)) {
|
|
40
|
+
if (currentPath) {
|
|
41
|
+
containerPaths.push(currentPath);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
for (const [key, value] of Object.entries(arrayOrObject)) {
|
|
45
|
+
const keyPath = keysOrIndexes ? `[${key}]` : `${dotOrNot}${key}`;
|
|
46
|
+
const nextPath = `${currentPath}${keyPath}`;
|
|
47
|
+
|
|
48
|
+
if (isObject(value) || isArray(value)) {
|
|
49
|
+
const { pathValues: childValues, containerPaths: childContainers } = flattenPathsWithValues(value, {
|
|
50
|
+
currentPath: nextPath,
|
|
51
|
+
depth: depth + 1,
|
|
52
|
+
limit,
|
|
53
|
+
});
|
|
54
|
+
pathValues.push(...childValues);
|
|
55
|
+
containerPaths.push(...childContainers);
|
|
56
|
+
} else {
|
|
57
|
+
pathValues.push([nextPath, value]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
} else if (currentPath) {
|
|
61
|
+
pathValues.push([currentPath, arrayOrObject]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return { pathValues, containerPaths };
|
|
65
|
+
}
|
package/utils/keyDiff.js
CHANGED
|
@@ -12,17 +12,17 @@ import { difference, intersection } from "./set.js";
|
|
|
12
12
|
* Result object of keyDiff and keyDiffDeep.
|
|
13
13
|
*
|
|
14
14
|
* @typedef {object} KeyDiffResult
|
|
15
|
-
* @property {Set} [sameKeys] - If sameKeys option is true, return keys that are the same.
|
|
16
|
-
* @property {Set} [removedKeys] - If removedKeys option is true, return keys that are removed.
|
|
17
|
-
* @property {Set} [addedKeys] - If addedKeys option is true, return keys that are added.
|
|
15
|
+
* @property {Set<string>} [sameKeys] - If sameKeys option is true, return keys that are the same.
|
|
16
|
+
* @property {Set<string>} [removedKeys] - If removedKeys option is true, return keys that are removed.
|
|
17
|
+
* @property {Set<string>} [addedKeys] - If addedKeys option is true, return keys that are added.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Calculate the difference between two arrays of keys, in terms of what keys
|
|
22
22
|
* are the same, what keys are removed, and what keys are added.
|
|
23
23
|
*
|
|
24
|
-
* @param {string[]|Set} newKeys - Keys to consider as new.
|
|
25
|
-
* @param {string[]|Set} oldKeys - Keys to consider as old.
|
|
24
|
+
* @param {string[]|Set<string>} newKeys - Keys to consider as new.
|
|
25
|
+
* @param {string[]|Set<string>} oldKeys - Keys to consider as old.
|
|
26
26
|
* @param {object} [options] - Which differences are returned.
|
|
27
27
|
* @param {boolean} [options.sameKeys=true] - If true, return keys that are the same.
|
|
28
28
|
* @param {boolean} [options.removedKeys=true] - If true, return keys that are removed.
|