topkat-utils 1.1.8 → 1.1.9
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/CHANGELOG.md +3 -0
- package/dist/src/loop-utils.d.ts +5 -2
- package/dist/src/loop-utils.js +14 -10
- package/dist/src/loop-utils.js.map +1 -1
- package/package.json +1 -1
- package/src/loop-utils.ts +11 -10
package/CHANGELOG.md
CHANGED
package/dist/src/loop-utils.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { ObjectGeneric } from "./private/types";
|
|
|
2
2
|
export declare function forI<T extends any[] | any>(nbIterations: number, callback: (number: number, previousValue: any, arrayOfPreviousValues: any[]) => T): T[];
|
|
3
3
|
export declare function forIasync<T extends any[] | any>(nbIterations: number, callback: (number: any) => T): Promise<T[]>;
|
|
4
4
|
export declare type RecursiveCallback = (item: any, addr: string, lastElementKey: string | number, parent: ObjectGeneric | any[]) => false | any;
|
|
5
|
+
export declare type RecursiveConfig = {
|
|
6
|
+
disableCircularDependencyRemoval?: boolean;
|
|
7
|
+
};
|
|
5
8
|
/**
|
|
6
9
|
* @param {any} item the first array or object or whatever you want to recursively browse
|
|
7
10
|
* @param {function} callback the callback you want to apply on items including the main one
|
|
@@ -17,7 +20,7 @@ export declare type RecursiveCallback = (item: any, addr: string, lastElementKey
|
|
|
17
20
|
* NOTE: will remove circular references
|
|
18
21
|
* /!\ check return values
|
|
19
22
|
*/
|
|
20
|
-
export declare function recursiveGenericFunction(item: ObjectGeneric | any[], callback: RecursiveCallback, addr$?: string, lastElementKey?: string | number, parent?: any, techFieldToAvoidCircularDependency?: any[]): Promise<any[] | ObjectGeneric>;
|
|
23
|
+
export declare function recursiveGenericFunction(item: ObjectGeneric | any[], callback: RecursiveCallback, config?: RecursiveConfig, addr$?: string, lastElementKey?: string | number, parent?: any, techFieldToAvoidCircularDependency?: any[]): Promise<any[] | ObjectGeneric>;
|
|
21
24
|
/**
|
|
22
25
|
* @param {any} item the first array or object or whatever you want to recursively browse
|
|
23
26
|
* @param {function} callback the callback you want to apply on items including the main one
|
|
@@ -34,4 +37,4 @@ export declare function recursiveGenericFunction(item: ObjectGeneric | any[], ca
|
|
|
34
37
|
* NOTE: will remove circular references
|
|
35
38
|
* /!\ check return values
|
|
36
39
|
*/
|
|
37
|
-
export declare function recursiveGenericFunctionSync(item: ObjectGeneric | any[], callback: RecursiveCallback, addr$?: string, lastElementKey?: string | number, parent?: any, techFieldToAvoidCircularDependency?: any[]): any[] | ObjectGeneric;
|
|
40
|
+
export declare function recursiveGenericFunctionSync(item: ObjectGeneric | any[], callback: RecursiveCallback, config?: RecursiveConfig, addr$?: string, lastElementKey?: string | number, parent?: any, techFieldToAvoidCircularDependency?: any[]): any[] | ObjectGeneric;
|
package/dist/src/loop-utils.js
CHANGED
|
@@ -35,19 +35,21 @@ exports.forIasync = forIasync;
|
|
|
35
35
|
* NOTE: will remove circular references
|
|
36
36
|
* /!\ check return values
|
|
37
37
|
*/
|
|
38
|
-
async function recursiveGenericFunction(item, callback, addr$ = '', lastElementKey = '', parent, techFieldToAvoidCircularDependency = []) {
|
|
38
|
+
async function recursiveGenericFunction(item, callback, config, addr$ = '', lastElementKey = '', parent, techFieldToAvoidCircularDependency = []) {
|
|
39
39
|
(0, error_utils_1.err500IfNotSet)({ callback });
|
|
40
40
|
if (!techFieldToAvoidCircularDependency.includes(item)) {
|
|
41
41
|
const result = addr$ === '' ? true : await callback(item, addr$, lastElementKey, parent);
|
|
42
42
|
if (result !== false) {
|
|
43
43
|
const addr = addr$ ? addr$ + '.' : '';
|
|
44
44
|
if (Array.isArray(item)) {
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
if (config.disableCircularDependencyRemoval !== true)
|
|
46
|
+
techFieldToAvoidCircularDependency.push(item);
|
|
47
|
+
await Promise.all(item.map((e, i) => recursiveGenericFunction(e, callback, config, addr + '[' + i + ']', i, item, techFieldToAvoidCircularDependency)));
|
|
47
48
|
}
|
|
48
49
|
else if ((0, is_object_1.isObject)(item)) {
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
if (config.disableCircularDependencyRemoval !== true)
|
|
51
|
+
techFieldToAvoidCircularDependency.push(item);
|
|
52
|
+
await Promise.all(Object.entries(item).map(([key, val]) => recursiveGenericFunction(val, callback, config, addr + key.replace(/\./g, '%'), key, item, techFieldToAvoidCircularDependency)));
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
}
|
|
@@ -70,19 +72,21 @@ exports.recursiveGenericFunction = recursiveGenericFunction;
|
|
|
70
72
|
* NOTE: will remove circular references
|
|
71
73
|
* /!\ check return values
|
|
72
74
|
*/
|
|
73
|
-
function recursiveGenericFunctionSync(item, callback, addr$ = '', lastElementKey = '', parent, techFieldToAvoidCircularDependency = []) {
|
|
75
|
+
function recursiveGenericFunctionSync(item, callback, config, addr$ = '', lastElementKey = '', parent, techFieldToAvoidCircularDependency = []) {
|
|
74
76
|
(0, error_utils_1.err500IfNotSet)({ callback });
|
|
75
77
|
if (!techFieldToAvoidCircularDependency.includes(item)) {
|
|
76
78
|
const result = addr$ === '' ? true : callback(item, addr$, lastElementKey, parent);
|
|
77
79
|
if (result !== false) {
|
|
78
80
|
const addr = addr$ ? addr$ + '.' : '';
|
|
79
81
|
if (Array.isArray(item)) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
if (config.disableCircularDependencyRemoval !== true)
|
|
83
|
+
techFieldToAvoidCircularDependency.push(item); // do not up one level
|
|
84
|
+
item.forEach((e, i) => recursiveGenericFunctionSync(e, callback, config, addr + '[' + i + ']', i, item, techFieldToAvoidCircularDependency));
|
|
82
85
|
}
|
|
83
86
|
else if ((0, is_object_1.isObject)(item)) {
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
if (config.disableCircularDependencyRemoval !== true)
|
|
88
|
+
techFieldToAvoidCircularDependency.push(item);
|
|
89
|
+
Object.entries(item).forEach(([key, val]) => recursiveGenericFunctionSync(val, callback, config, addr + key.replace(/\./g, '%'), key, item, techFieldToAvoidCircularDependency));
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loop-utils.js","sourceRoot":"","sources":["../../src/loop-utils.ts"],"names":[],"mappings":";;;AAIA,+CAA8C;AAC9C,2CAAsC;AAEtC,SAAgB,IAAI,CAAwB,YAAoB,EAAE,QAA4E;IAC1I,MAAM,OAAO,GAAG,EAAE,CAAA;IAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE;QACnC,MAAM,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;QAC7C,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,CAAA;KAChD;IACD,OAAO,OAAO,CAAA;AAClB,CAAC;AAPD,oBAOC;AAEM,KAAK,UAAU,SAAS,CAAwB,YAAoB,EAAE,QAAuB;IAChG,MAAM,OAAO,GAAG,EAAE,CAAA;IAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE;QACnC,OAAO,CAAC,IAAI,CAAC,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;KAClC;IACD,OAAO,OAAO,CAAA;AAClB,CAAC;AAND,8BAMC;
|
|
1
|
+
{"version":3,"file":"loop-utils.js","sourceRoot":"","sources":["../../src/loop-utils.ts"],"names":[],"mappings":";;;AAIA,+CAA8C;AAC9C,2CAAsC;AAEtC,SAAgB,IAAI,CAAwB,YAAoB,EAAE,QAA4E;IAC1I,MAAM,OAAO,GAAG,EAAE,CAAA;IAClB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE;QACnC,MAAM,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,C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K,KAAK,EAAE;YAClB,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,EAAE,CAAA;YACrC,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;gBACrB,IAAI,MAAM,CAAC,gCAAgC,KAAK,IAAI;oBAAE,kCAAkC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA,CAAC,sBAAsB;gBAC1H,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,4BAA4B,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAG,GAAG,GAAG,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,kCAAkC,CAAC,CAAC,CAAA;aAC/I;iBAAM,IAAI,IAAA,oBAAQ,EAAC,IAAI,CAAC,EAAE;gBACvB,IAAI,MAAM,CAAC,gCAAgC,KAAK,IAAI;oBAAE,kCAAkC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;gBACnG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,4BAA4B,CAAC,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,kCAAkC,CAAC,CAAC,CAAA;aACnL;SACJ;KACJ;IACD,OAAO,IAAI,CAAA;AACf,CAAC;AAlBD,oEAkBC"}
|
package/package.json
CHANGED
package/src/loop-utils.ts
CHANGED
|
@@ -25,6 +25,7 @@ export async function forIasync<T extends any[] | any>(nbIterations: number, cal
|
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
export type RecursiveCallback = (item: any, addr: string, lastElementKey: string | number, parent: ObjectGeneric | any[]) => false | any
|
|
28
|
+
export type RecursiveConfig = { disableCircularDependencyRemoval?: boolean }
|
|
28
29
|
/**
|
|
29
30
|
* @param {any} item the first array or object or whatever you want to recursively browse
|
|
30
31
|
* @param {function} callback the callback you want to apply on items including the main one
|
|
@@ -40,7 +41,7 @@ export type RecursiveCallback = (item: any, addr: string, lastElementKey: string
|
|
|
40
41
|
* NOTE: will remove circular references
|
|
41
42
|
* /!\ check return values
|
|
42
43
|
*/
|
|
43
|
-
export async function recursiveGenericFunction(item: ObjectGeneric | any[], callback: RecursiveCallback, addr$ = '', lastElementKey: string | number = '', parent?, techFieldToAvoidCircularDependency = []) {
|
|
44
|
+
export async function recursiveGenericFunction(item: ObjectGeneric | any[], callback: RecursiveCallback, config?: RecursiveConfig, addr$ = '', lastElementKey: string | number = '', parent?, techFieldToAvoidCircularDependency = []) {
|
|
44
45
|
err500IfNotSet({ callback })
|
|
45
46
|
|
|
46
47
|
if (!techFieldToAvoidCircularDependency.includes(item)) {
|
|
@@ -49,14 +50,14 @@ export async function recursiveGenericFunction(item: ObjectGeneric | any[], call
|
|
|
49
50
|
if (result !== false) {
|
|
50
51
|
const addr = addr$ ? addr$ + '.' : ''
|
|
51
52
|
if (Array.isArray(item)) {
|
|
52
|
-
techFieldToAvoidCircularDependency.push(item)
|
|
53
|
+
if (config.disableCircularDependencyRemoval !== true) techFieldToAvoidCircularDependency.push(item)
|
|
53
54
|
await Promise.all(item.map(
|
|
54
|
-
(e, i) => recursiveGenericFunction(e, callback, addr + '[' + i + ']', i, item, techFieldToAvoidCircularDependency)
|
|
55
|
+
(e, i) => recursiveGenericFunction(e, callback, config, addr + '[' + i + ']', i, item, techFieldToAvoidCircularDependency)
|
|
55
56
|
))
|
|
56
57
|
} else if (isObject(item)) {
|
|
57
|
-
techFieldToAvoidCircularDependency.push(item)
|
|
58
|
+
if (config.disableCircularDependencyRemoval !== true) techFieldToAvoidCircularDependency.push(item)
|
|
58
59
|
await Promise.all(Object.entries(item).map(
|
|
59
|
-
([key, val]) => recursiveGenericFunction(val, callback, addr + key.replace(/\./g, '%'), key, item, techFieldToAvoidCircularDependency)
|
|
60
|
+
([key, val]) => recursiveGenericFunction(val, callback, config, addr + key.replace(/\./g, '%'), key, item, techFieldToAvoidCircularDependency)
|
|
60
61
|
))
|
|
61
62
|
}
|
|
62
63
|
}
|
|
@@ -80,7 +81,7 @@ export async function recursiveGenericFunction(item: ObjectGeneric | any[], call
|
|
|
80
81
|
* NOTE: will remove circular references
|
|
81
82
|
* /!\ check return values
|
|
82
83
|
*/
|
|
83
|
-
export function recursiveGenericFunctionSync(item: ObjectGeneric | any[], callback: RecursiveCallback, addr$ = '', lastElementKey: string | number = '', parent?, techFieldToAvoidCircularDependency = []) {
|
|
84
|
+
export function recursiveGenericFunctionSync(item: ObjectGeneric | any[], callback: RecursiveCallback, config?: RecursiveConfig, addr$ = '', lastElementKey: string | number = '', parent?, techFieldToAvoidCircularDependency = []) {
|
|
84
85
|
err500IfNotSet({ callback })
|
|
85
86
|
|
|
86
87
|
if (!techFieldToAvoidCircularDependency.includes(item)) {
|
|
@@ -89,11 +90,11 @@ export function recursiveGenericFunctionSync(item: ObjectGeneric | any[], callba
|
|
|
89
90
|
if (result !== false) {
|
|
90
91
|
const addr = addr$ ? addr$ + '.' : ''
|
|
91
92
|
if (Array.isArray(item)) {
|
|
92
|
-
techFieldToAvoidCircularDependency.push(item) // do not up one level
|
|
93
|
-
item.forEach((e, i) => recursiveGenericFunctionSync(e, callback, addr + '[' + i + ']', i, item, techFieldToAvoidCircularDependency))
|
|
93
|
+
if (config.disableCircularDependencyRemoval !== true) techFieldToAvoidCircularDependency.push(item) // do not up one level
|
|
94
|
+
item.forEach((e, i) => recursiveGenericFunctionSync(e, callback, config, addr + '[' + i + ']', i, item, techFieldToAvoidCircularDependency))
|
|
94
95
|
} else if (isObject(item)) {
|
|
95
|
-
techFieldToAvoidCircularDependency.push(item)
|
|
96
|
-
Object.entries(item).forEach(([key, val]) => recursiveGenericFunctionSync(val, callback, addr + key.replace(/\./g, '%'), key, item, techFieldToAvoidCircularDependency))
|
|
96
|
+
if (config.disableCircularDependencyRemoval !== true) techFieldToAvoidCircularDependency.push(item)
|
|
97
|
+
Object.entries(item).forEach(([key, val]) => recursiveGenericFunctionSync(val, callback, config, addr + key.replace(/\./g, '%'), key, item, techFieldToAvoidCircularDependency))
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
}
|