@ls-stack/utils 3.36.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.
@@ -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) {
@@ -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) {
@@ -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;
@@ -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;
@@ -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
@@ -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;
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:363](https://github.com/lucasols/utils/blob/main/packages/utils/src/testUtils.ts#L363)
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.36.0",
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.20.1",
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.3.3",
250
+ "evtmitter": "^1.0.2",
251
251
  "t-result": "^0.3.0"
252
252
  },
253
253
  "scripts": {