@ls-stack/utils 3.35.0 → 3.37.0
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/dist/arrayUtils.cjs +11 -1
- package/dist/arrayUtils.d.cts +20 -1
- package/dist/arrayUtils.d.ts +20 -1
- package/dist/arrayUtils.js +3 -1
- package/dist/asyncQueue.cjs +2 -2
- package/dist/asyncQueue.js +2 -2
- package/dist/{chunk-4DVTWCXL.js → chunk-27AL66CH.js} +10 -1
- package/dist/{chunk-XPOGWCUC.js → chunk-6CG6JZKB.js} +1 -1
- package/dist/concurrentCalls.js +1 -1
- package/dist/filterObjectOrArrayKeys.js +2 -2
- package/dist/serializeXML.js +1 -1
- package/dist/testUtils.cjs +44 -0
- package/dist/testUtils.d.cts +5 -1
- package/dist/testUtils.d.ts +5 -1
- package/dist/testUtils.js +46 -2
- package/docs/arrayUtils/-internal-.md +24 -0
- package/docs/arrayUtils/README.md +57 -1
- package/docs/testUtils.md +1 -1
- package/package.json +3 -3
package/dist/arrayUtils.cjs
CHANGED
|
@@ -25,6 +25,7 @@ __export(arrayUtils_exports, {
|
|
|
25
25
|
arrayWithPrevAndIndex: () => arrayWithPrevAndIndex,
|
|
26
26
|
filterAndMap: () => filterAndMap,
|
|
27
27
|
findAfterIndex: () => findAfterIndex,
|
|
28
|
+
findAndMap: () => findAndMap,
|
|
28
29
|
findBeforeIndex: () => findBeforeIndex,
|
|
29
30
|
getAscIndexOrder: () => getAscIndexOrder,
|
|
30
31
|
hasDuplicates: () => hasDuplicates,
|
|
@@ -160,11 +161,19 @@ function truncateArray(array, maxLength, appendIfTruncated) {
|
|
|
160
161
|
}
|
|
161
162
|
return result;
|
|
162
163
|
}
|
|
164
|
+
function findAndMap(array, predicate) {
|
|
165
|
+
for (const item of array) {
|
|
166
|
+
const value = predicate(item);
|
|
167
|
+
if (value !== false) return value;
|
|
168
|
+
}
|
|
169
|
+
return void 0;
|
|
170
|
+
}
|
|
163
171
|
function arrayOps(array) {
|
|
164
172
|
return {
|
|
165
173
|
filterAndMap: (mapFilter) => filterAndMap(array, mapFilter),
|
|
166
174
|
sortBy: (sortByValue, props) => sortBy(array, sortByValue, props),
|
|
167
|
-
rejectDuplicates: (getKey) => rejectDuplicates(array, getKey)
|
|
175
|
+
rejectDuplicates: (getKey) => rejectDuplicates(array, getKey),
|
|
176
|
+
findAndMap: (predicate) => findAndMap(array, predicate)
|
|
168
177
|
};
|
|
169
178
|
}
|
|
170
179
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -174,6 +183,7 @@ function arrayOps(array) {
|
|
|
174
183
|
arrayWithPrevAndIndex,
|
|
175
184
|
filterAndMap,
|
|
176
185
|
findAfterIndex,
|
|
186
|
+
findAndMap,
|
|
177
187
|
findBeforeIndex,
|
|
178
188
|
getAscIndexOrder,
|
|
179
189
|
hasDuplicates,
|
package/dist/arrayUtils.d.cts
CHANGED
|
@@ -94,7 +94,26 @@ type ArrayOps<T> = {
|
|
|
94
94
|
filterAndMap: <R>(mapFilter: (item: T, index: number) => false | R) => R[];
|
|
95
95
|
sortBy: (sortByValue: SortByValue<T>, props: SortByProps) => T[];
|
|
96
96
|
rejectDuplicates: (getKey: (item: T) => unknown) => T[];
|
|
97
|
+
findAndMap: <R>(predicate: (value: T) => R | false) => R | undefined;
|
|
97
98
|
};
|
|
99
|
+
/**
|
|
100
|
+
* Finds the first item in an array where the predicate returns a non-false value and returns that mapped value.
|
|
101
|
+
*
|
|
102
|
+
* Combines find and map operations - applies the predicate to each item until one returns
|
|
103
|
+
* a value that is not `false`, then returns that mapped value. If no item matches, returns `undefined`.
|
|
104
|
+
*
|
|
105
|
+
* @param array - The array to search through
|
|
106
|
+
* @param predicate - Function that returns a mapped value or `false` to skip the item
|
|
107
|
+
* @returns The first mapped value that is not `false`, or `undefined` if no item matches
|
|
108
|
+
* @example
|
|
109
|
+
* const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
110
|
+
*
|
|
111
|
+
* const foundName = findAndMap(users, (user) =>
|
|
112
|
+
* user.id === 2 ? user.name.toUpperCase() : false
|
|
113
|
+
* );
|
|
114
|
+
* // foundName is 'BOB'
|
|
115
|
+
*/
|
|
116
|
+
declare function findAndMap<T, R>(array: T[], predicate: (value: T) => R | false): R | undefined;
|
|
98
117
|
/**
|
|
99
118
|
* Enhance an array with extra methods
|
|
100
119
|
*
|
|
@@ -109,4 +128,4 @@ type ArrayOps<T> = {
|
|
|
109
128
|
*/
|
|
110
129
|
declare function arrayOps<T>(array: T[]): ArrayOps<T>;
|
|
111
130
|
|
|
112
|
-
export { type FilterAndMapReturn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, getAscIndexOrder, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
|
|
131
|
+
export { type FilterAndMapReturn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findAndMap, findBeforeIndex, getAscIndexOrder, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
|
package/dist/arrayUtils.d.ts
CHANGED
|
@@ -94,7 +94,26 @@ type ArrayOps<T> = {
|
|
|
94
94
|
filterAndMap: <R>(mapFilter: (item: T, index: number) => false | R) => R[];
|
|
95
95
|
sortBy: (sortByValue: SortByValue<T>, props: SortByProps) => T[];
|
|
96
96
|
rejectDuplicates: (getKey: (item: T) => unknown) => T[];
|
|
97
|
+
findAndMap: <R>(predicate: (value: T) => R | false) => R | undefined;
|
|
97
98
|
};
|
|
99
|
+
/**
|
|
100
|
+
* Finds the first item in an array where the predicate returns a non-false value and returns that mapped value.
|
|
101
|
+
*
|
|
102
|
+
* Combines find and map operations - applies the predicate to each item until one returns
|
|
103
|
+
* a value that is not `false`, then returns that mapped value. If no item matches, returns `undefined`.
|
|
104
|
+
*
|
|
105
|
+
* @param array - The array to search through
|
|
106
|
+
* @param predicate - Function that returns a mapped value or `false` to skip the item
|
|
107
|
+
* @returns The first mapped value that is not `false`, or `undefined` if no item matches
|
|
108
|
+
* @example
|
|
109
|
+
* const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
110
|
+
*
|
|
111
|
+
* const foundName = findAndMap(users, (user) =>
|
|
112
|
+
* user.id === 2 ? user.name.toUpperCase() : false
|
|
113
|
+
* );
|
|
114
|
+
* // foundName is 'BOB'
|
|
115
|
+
*/
|
|
116
|
+
declare function findAndMap<T, R>(array: T[], predicate: (value: T) => R | false): R | undefined;
|
|
98
117
|
/**
|
|
99
118
|
* Enhance an array with extra methods
|
|
100
119
|
*
|
|
@@ -109,4 +128,4 @@ type ArrayOps<T> = {
|
|
|
109
128
|
*/
|
|
110
129
|
declare function arrayOps<T>(array: T[]): ArrayOps<T>;
|
|
111
130
|
|
|
112
|
-
export { type FilterAndMapReturn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, getAscIndexOrder, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
|
|
131
|
+
export { type FilterAndMapReturn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findAndMap, findBeforeIndex, getAscIndexOrder, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
|
package/dist/arrayUtils.js
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
arrayWithPrevAndIndex,
|
|
5
5
|
filterAndMap,
|
|
6
6
|
findAfterIndex,
|
|
7
|
+
findAndMap,
|
|
7
8
|
findBeforeIndex,
|
|
8
9
|
getAscIndexOrder,
|
|
9
10
|
hasDuplicates,
|
|
@@ -12,7 +13,7 @@ import {
|
|
|
12
13
|
rejectDuplicates,
|
|
13
14
|
sortBy,
|
|
14
15
|
truncateArray
|
|
15
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-27AL66CH.js";
|
|
16
17
|
import "./chunk-C2SVCIWE.js";
|
|
17
18
|
import "./chunk-JF2MDHOJ.js";
|
|
18
19
|
export {
|
|
@@ -21,6 +22,7 @@ export {
|
|
|
21
22
|
arrayWithPrevAndIndex,
|
|
22
23
|
filterAndMap,
|
|
23
24
|
findAfterIndex,
|
|
25
|
+
findAndMap,
|
|
24
26
|
findBeforeIndex,
|
|
25
27
|
getAscIndexOrder,
|
|
26
28
|
hasDuplicates,
|
package/dist/asyncQueue.cjs
CHANGED
|
@@ -61,10 +61,10 @@ var AsyncQueue = class {
|
|
|
61
61
|
this.#signal = signal;
|
|
62
62
|
this.#taskTimeout = taskTimeout;
|
|
63
63
|
this.events.on("error", (e) => {
|
|
64
|
-
this.failures.push(e);
|
|
64
|
+
this.failures.push(e.payload);
|
|
65
65
|
});
|
|
66
66
|
this.events.on("complete", (e) => {
|
|
67
|
-
this.completions.push(e);
|
|
67
|
+
this.completions.push(e.payload);
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
#enqueue(task) {
|
package/dist/asyncQueue.js
CHANGED
|
@@ -32,10 +32,10 @@ var AsyncQueue = class {
|
|
|
32
32
|
this.#signal = signal;
|
|
33
33
|
this.#taskTimeout = taskTimeout;
|
|
34
34
|
this.events.on("error", (e) => {
|
|
35
|
-
this.failures.push(e);
|
|
35
|
+
this.failures.push(e.payload);
|
|
36
36
|
});
|
|
37
37
|
this.events.on("complete", (e) => {
|
|
38
|
-
this.completions.push(e);
|
|
38
|
+
this.completions.push(e.payload);
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
#enqueue(task) {
|
|
@@ -118,11 +118,19 @@ function truncateArray(array, maxLength, appendIfTruncated) {
|
|
|
118
118
|
}
|
|
119
119
|
return result;
|
|
120
120
|
}
|
|
121
|
+
function findAndMap(array, predicate) {
|
|
122
|
+
for (const item of array) {
|
|
123
|
+
const value = predicate(item);
|
|
124
|
+
if (value !== false) return value;
|
|
125
|
+
}
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
121
128
|
function arrayOps(array) {
|
|
122
129
|
return {
|
|
123
130
|
filterAndMap: (mapFilter) => filterAndMap(array, mapFilter),
|
|
124
131
|
sortBy: (sortByValue, props) => sortBy(array, sortByValue, props),
|
|
125
|
-
rejectDuplicates: (getKey) => rejectDuplicates(array, getKey)
|
|
132
|
+
rejectDuplicates: (getKey) => rejectDuplicates(array, getKey),
|
|
133
|
+
findAndMap: (predicate) => findAndMap(array, predicate)
|
|
126
134
|
};
|
|
127
135
|
}
|
|
128
136
|
|
|
@@ -139,5 +147,6 @@ export {
|
|
|
139
147
|
hasDuplicates,
|
|
140
148
|
rejectDuplicates,
|
|
141
149
|
truncateArray,
|
|
150
|
+
findAndMap,
|
|
142
151
|
arrayOps
|
|
143
152
|
};
|
package/dist/concurrentCalls.js
CHANGED
package/dist/serializeXML.js
CHANGED
package/dist/testUtils.cjs
CHANGED
|
@@ -1347,6 +1347,7 @@ function compactSnapshot(value, {
|
|
|
1347
1347
|
maxLineLength = 100,
|
|
1348
1348
|
showUndefined = false,
|
|
1349
1349
|
showBooleansAs = true,
|
|
1350
|
+
replaceValues,
|
|
1350
1351
|
rejectKeys,
|
|
1351
1352
|
filterKeys,
|
|
1352
1353
|
sortKeys,
|
|
@@ -1364,6 +1365,9 @@ function compactSnapshot(value, {
|
|
|
1364
1365
|
});
|
|
1365
1366
|
}
|
|
1366
1367
|
}
|
|
1368
|
+
if (replaceValues) {
|
|
1369
|
+
processedValue = applyValueReplacements(processedValue, replaceValues);
|
|
1370
|
+
}
|
|
1367
1371
|
processedValue = showBooleansAs ? replaceBooleansWithEmoji(processedValue, showBooleansAs) : processedValue;
|
|
1368
1372
|
return `
|
|
1369
1373
|
${yamlStringify(processedValue, {
|
|
@@ -1373,6 +1377,46 @@ ${yamlStringify(processedValue, {
|
|
|
1373
1377
|
...options
|
|
1374
1378
|
})}`;
|
|
1375
1379
|
}
|
|
1380
|
+
function applyValueReplacements(value, replaceValues, visited = /* @__PURE__ */ new Set(), currentPath = "") {
|
|
1381
|
+
function processValue(val, path) {
|
|
1382
|
+
const replacement = replaceValues(val, path);
|
|
1383
|
+
if (replacement !== false) {
|
|
1384
|
+
return replacement.newValue;
|
|
1385
|
+
}
|
|
1386
|
+
if (Array.isArray(val)) {
|
|
1387
|
+
if (visited.has(val)) {
|
|
1388
|
+
throw new Error("Circular reference detected in array");
|
|
1389
|
+
}
|
|
1390
|
+
visited.add(val);
|
|
1391
|
+
try {
|
|
1392
|
+
return val.map((item, index) => {
|
|
1393
|
+
const itemPath = path ? `${path}[${index}]` : `[${index}]`;
|
|
1394
|
+
return processValue(item, itemPath);
|
|
1395
|
+
});
|
|
1396
|
+
} finally {
|
|
1397
|
+
visited.delete(val);
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
if (isPlainObject(val)) {
|
|
1401
|
+
if (visited.has(val)) {
|
|
1402
|
+
throw new Error("Circular reference detected in object");
|
|
1403
|
+
}
|
|
1404
|
+
visited.add(val);
|
|
1405
|
+
try {
|
|
1406
|
+
const result = {};
|
|
1407
|
+
for (const [key, itemValue] of Object.entries(val)) {
|
|
1408
|
+
const itemPath = path ? `${path}.${key}` : key;
|
|
1409
|
+
result[key] = processValue(itemValue, itemPath);
|
|
1410
|
+
}
|
|
1411
|
+
return result;
|
|
1412
|
+
} finally {
|
|
1413
|
+
visited.delete(val);
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
return val;
|
|
1417
|
+
}
|
|
1418
|
+
return processValue(value, currentPath);
|
|
1419
|
+
}
|
|
1376
1420
|
function replaceBooleansWithEmoji(value, showBooleansAs, visited = /* @__PURE__ */ new Set()) {
|
|
1377
1421
|
if (showBooleansAs === false) {
|
|
1378
1422
|
return value;
|
package/dist/testUtils.d.cts
CHANGED
|
@@ -88,11 +88,12 @@ declare function waitController(): {
|
|
|
88
88
|
* @param options.rejectKeys - The keys to reject.
|
|
89
89
|
* @param options.filterKeys - The keys to filter.
|
|
90
90
|
* @param options.ignoreProps - The props to ignore.
|
|
91
|
+
* @param options.replaceValues - Function to replace values at specific paths. Returns `false` to keep original value or `{newValue}` to replace.
|
|
91
92
|
* @param options.sortKeys - Sort all keys by a specific order (default: `simpleValuesFirst`).
|
|
92
93
|
* @param options.sortPatterns - Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
|
|
93
94
|
* @returns The compact snapshot of the value.
|
|
94
95
|
*/
|
|
95
|
-
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
|
96
|
+
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, replaceValues, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
|
96
97
|
showBooleansAs?: boolean | {
|
|
97
98
|
props?: Record<string, {
|
|
98
99
|
trueText?: string;
|
|
@@ -102,6 +103,9 @@ declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLengt
|
|
|
102
103
|
trueText?: string;
|
|
103
104
|
falseText?: string;
|
|
104
105
|
};
|
|
106
|
+
replaceValues?: (value: unknown, path: string) => false | {
|
|
107
|
+
newValue: unknown;
|
|
108
|
+
};
|
|
105
109
|
rejectKeys?: string[] | string;
|
|
106
110
|
filterKeys?: string[] | string;
|
|
107
111
|
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
package/dist/testUtils.d.ts
CHANGED
|
@@ -88,11 +88,12 @@ declare function waitController(): {
|
|
|
88
88
|
* @param options.rejectKeys - The keys to reject.
|
|
89
89
|
* @param options.filterKeys - The keys to filter.
|
|
90
90
|
* @param options.ignoreProps - The props to ignore.
|
|
91
|
+
* @param options.replaceValues - Function to replace values at specific paths. Returns `false` to keep original value or `{newValue}` to replace.
|
|
91
92
|
* @param options.sortKeys - Sort all keys by a specific order (default: `simpleValuesFirst`).
|
|
92
93
|
* @param options.sortPatterns - Sort specific keys by pattern. Use to control the order of specific properties. The same patterns as `filterKeys` are supported.
|
|
93
94
|
* @returns The compact snapshot of the value.
|
|
94
95
|
*/
|
|
95
|
-
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
|
96
|
+
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, replaceValues, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
|
96
97
|
showBooleansAs?: boolean | {
|
|
97
98
|
props?: Record<string, {
|
|
98
99
|
trueText?: string;
|
|
@@ -102,6 +103,9 @@ declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLengt
|
|
|
102
103
|
trueText?: string;
|
|
103
104
|
falseText?: string;
|
|
104
105
|
};
|
|
106
|
+
replaceValues?: (value: unknown, path: string) => false | {
|
|
107
|
+
newValue: unknown;
|
|
108
|
+
};
|
|
105
109
|
rejectKeys?: string[] | string;
|
|
106
110
|
filterKeys?: string[] | string;
|
|
107
111
|
sortKeys?: 'asc' | 'desc' | 'simpleValuesFirst' | false;
|
package/dist/testUtils.js
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from "./chunk-JQFUKJU5.js";
|
|
12
12
|
import {
|
|
13
13
|
filterObjectOrArrayKeys
|
|
14
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-6CG6JZKB.js";
|
|
15
15
|
import {
|
|
16
16
|
defer
|
|
17
17
|
} from "./chunk-DFXNVEH6.js";
|
|
@@ -22,7 +22,7 @@ import "./chunk-KW55OTUG.js";
|
|
|
22
22
|
import {
|
|
23
23
|
arrayWithPrevAndIndex,
|
|
24
24
|
filterAndMap
|
|
25
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-27AL66CH.js";
|
|
26
26
|
import {
|
|
27
27
|
isObject
|
|
28
28
|
} from "./chunk-C2SVCIWE.js";
|
|
@@ -261,6 +261,7 @@ function compactSnapshot(value, {
|
|
|
261
261
|
maxLineLength = 100,
|
|
262
262
|
showUndefined = false,
|
|
263
263
|
showBooleansAs = true,
|
|
264
|
+
replaceValues,
|
|
264
265
|
rejectKeys,
|
|
265
266
|
filterKeys,
|
|
266
267
|
sortKeys,
|
|
@@ -278,6 +279,9 @@ function compactSnapshot(value, {
|
|
|
278
279
|
});
|
|
279
280
|
}
|
|
280
281
|
}
|
|
282
|
+
if (replaceValues) {
|
|
283
|
+
processedValue = applyValueReplacements(processedValue, replaceValues);
|
|
284
|
+
}
|
|
281
285
|
processedValue = showBooleansAs ? replaceBooleansWithEmoji(processedValue, showBooleansAs) : processedValue;
|
|
282
286
|
return `
|
|
283
287
|
${yamlStringify(processedValue, {
|
|
@@ -287,6 +291,46 @@ ${yamlStringify(processedValue, {
|
|
|
287
291
|
...options
|
|
288
292
|
})}`;
|
|
289
293
|
}
|
|
294
|
+
function applyValueReplacements(value, replaceValues, visited = /* @__PURE__ */ new Set(), currentPath = "") {
|
|
295
|
+
function processValue(val, path) {
|
|
296
|
+
const replacement = replaceValues(val, path);
|
|
297
|
+
if (replacement !== false) {
|
|
298
|
+
return replacement.newValue;
|
|
299
|
+
}
|
|
300
|
+
if (Array.isArray(val)) {
|
|
301
|
+
if (visited.has(val)) {
|
|
302
|
+
throw new Error("Circular reference detected in array");
|
|
303
|
+
}
|
|
304
|
+
visited.add(val);
|
|
305
|
+
try {
|
|
306
|
+
return val.map((item, index) => {
|
|
307
|
+
const itemPath = path ? `${path}[${index}]` : `[${index}]`;
|
|
308
|
+
return processValue(item, itemPath);
|
|
309
|
+
});
|
|
310
|
+
} finally {
|
|
311
|
+
visited.delete(val);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (isPlainObject(val)) {
|
|
315
|
+
if (visited.has(val)) {
|
|
316
|
+
throw new Error("Circular reference detected in object");
|
|
317
|
+
}
|
|
318
|
+
visited.add(val);
|
|
319
|
+
try {
|
|
320
|
+
const result = {};
|
|
321
|
+
for (const [key, itemValue] of Object.entries(val)) {
|
|
322
|
+
const itemPath = path ? `${path}.${key}` : key;
|
|
323
|
+
result[key] = processValue(itemValue, itemPath);
|
|
324
|
+
}
|
|
325
|
+
return result;
|
|
326
|
+
} finally {
|
|
327
|
+
visited.delete(val);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return val;
|
|
331
|
+
}
|
|
332
|
+
return processValue(value, currentPath);
|
|
333
|
+
}
|
|
290
334
|
function replaceBooleansWithEmoji(value, showBooleansAs, visited = /* @__PURE__ */ new Set()) {
|
|
291
335
|
if (showBooleansAs === false) {
|
|
292
336
|
return value;
|
|
@@ -63,6 +63,30 @@ const enhancedItems = arrayOps(items);
|
|
|
63
63
|
enhancedItems.filterAndMap((item) => item === 2 ? false : item);
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
+
##### findAndMap()
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
findAndMap: <R>(predicate) => R | undefined;
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Defined in: [packages/utils/src/arrayUtils.ts:275](https://github.com/lucasols/utils/blob/main/packages/utils/src/arrayUtils.ts#L275)
|
|
73
|
+
|
|
74
|
+
###### Type Parameters
|
|
75
|
+
|
|
76
|
+
###### R
|
|
77
|
+
|
|
78
|
+
`R`
|
|
79
|
+
|
|
80
|
+
###### Parameters
|
|
81
|
+
|
|
82
|
+
###### predicate
|
|
83
|
+
|
|
84
|
+
(`value`) => `R` \| `false`
|
|
85
|
+
|
|
86
|
+
###### Returns
|
|
87
|
+
|
|
88
|
+
`R` \| `undefined`
|
|
89
|
+
|
|
66
90
|
##### rejectDuplicates()
|
|
67
91
|
|
|
68
92
|
```ts
|
|
@@ -34,7 +34,7 @@ Defined in: [packages/utils/src/arrayUtils.ts:42](https://github.com/lucasols/ut
|
|
|
34
34
|
function arrayOps<T>(array): ArrayOps<T>;
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
Defined in: [packages/utils/src/arrayUtils.ts:
|
|
37
|
+
Defined in: [packages/utils/src/arrayUtils.ts:318](https://github.com/lucasols/utils/blob/main/packages/utils/src/arrayUtils.ts#L318)
|
|
38
38
|
|
|
39
39
|
Enhance an array with extra methods
|
|
40
40
|
|
|
@@ -209,6 +209,62 @@ Defined in: [packages/utils/src/arrayUtils.ts:165](https://github.com/lucasols/u
|
|
|
209
209
|
|
|
210
210
|
***
|
|
211
211
|
|
|
212
|
+
### findAndMap()
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
function findAndMap<T, R>(array, predicate): undefined | R;
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Defined in: [packages/utils/src/arrayUtils.ts:295](https://github.com/lucasols/utils/blob/main/packages/utils/src/arrayUtils.ts#L295)
|
|
219
|
+
|
|
220
|
+
Finds the first item in an array where the predicate returns a non-false value and returns that mapped value.
|
|
221
|
+
|
|
222
|
+
Combines find and map operations - applies the predicate to each item until one returns
|
|
223
|
+
a value that is not `false`, then returns that mapped value. If no item matches, returns `undefined`.
|
|
224
|
+
|
|
225
|
+
#### Type Parameters
|
|
226
|
+
|
|
227
|
+
##### T
|
|
228
|
+
|
|
229
|
+
`T`
|
|
230
|
+
|
|
231
|
+
##### R
|
|
232
|
+
|
|
233
|
+
`R`
|
|
234
|
+
|
|
235
|
+
#### Parameters
|
|
236
|
+
|
|
237
|
+
##### array
|
|
238
|
+
|
|
239
|
+
`T`[]
|
|
240
|
+
|
|
241
|
+
The array to search through
|
|
242
|
+
|
|
243
|
+
##### predicate
|
|
244
|
+
|
|
245
|
+
(`value`) => `false` \| `R`
|
|
246
|
+
|
|
247
|
+
Function that returns a mapped value or `false` to skip the item
|
|
248
|
+
|
|
249
|
+
#### Returns
|
|
250
|
+
|
|
251
|
+
`undefined` \| `R`
|
|
252
|
+
|
|
253
|
+
The first mapped value that is not `false`, or `undefined` if no item matches
|
|
254
|
+
|
|
255
|
+
#### Example
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
|
|
259
|
+
|
|
260
|
+
const foundName = findAndMap(users, (user) =>
|
|
261
|
+
user.id === 2 ? user.name.toUpperCase() : false
|
|
262
|
+
);
|
|
263
|
+
// foundName is 'BOB'
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
***
|
|
267
|
+
|
|
212
268
|
### findBeforeIndex()
|
|
213
269
|
|
|
214
270
|
```ts
|
package/docs/testUtils.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
function compactSnapshot(value, options): string;
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Defined in: [packages/utils/src/testUtils.ts:
|
|
17
|
+
Defined in: [packages/utils/src/testUtils.ts:364](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L364)
|
|
18
18
|
|
|
19
19
|
Produces a more compact and readable snapshot of a value using yaml.
|
|
20
20
|
By default booleans are shown as `✅` and `❌`, use `showBooleansAs` to disable/configure this.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ls-stack/utils",
|
|
3
3
|
"description": "Universal TypeScript utilities for browser and Node.js",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.37.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -222,7 +222,7 @@
|
|
|
222
222
|
},
|
|
223
223
|
"devDependencies": {
|
|
224
224
|
"@eslint/js": "^9.18.0",
|
|
225
|
-
"@ls-stack/extended-lint": "^0.
|
|
225
|
+
"@ls-stack/extended-lint": "^0.67.2",
|
|
226
226
|
"@types/eslint": "^9.6.1",
|
|
227
227
|
"@types/eslint__js": "^8.42.3",
|
|
228
228
|
"@types/node": "^22.10.9",
|
|
@@ -247,7 +247,7 @@
|
|
|
247
247
|
"vitest": "^3.0.4"
|
|
248
248
|
},
|
|
249
249
|
"dependencies": {
|
|
250
|
-
"evtmitter": "^0.
|
|
250
|
+
"evtmitter": "^1.0.2",
|
|
251
251
|
"t-result": "^0.3.0"
|
|
252
252
|
},
|
|
253
253
|
"scripts": {
|