@ls-stack/utils 3.8.0 → 3.9.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.
@@ -29,9 +29,17 @@ __export(arrayUtils_exports, {
29
29
  isInArray: () => isInArray,
30
30
  rejectArrayUndefinedValues: () => rejectArrayUndefinedValues,
31
31
  rejectDuplicates: () => rejectDuplicates,
32
- sortBy: () => sortBy
32
+ sortBy: () => sortBy,
33
+ truncateArray: () => truncateArray
33
34
  });
34
35
  module.exports = __toCommonJS(arrayUtils_exports);
36
+
37
+ // src/assertions.ts
38
+ function isFunction(value) {
39
+ return typeof value === "function";
40
+ }
41
+
42
+ // src/arrayUtils.ts
35
43
  function filterAndMap(array, mapFilter) {
36
44
  const result = [];
37
45
  let i = -1;
@@ -133,6 +141,17 @@ function rejectDuplicates(array, getKey = (item) => item) {
133
141
  }
134
142
  return result;
135
143
  }
144
+ function truncateArray(array, maxLength, appendIfTruncated) {
145
+ const truncate = array.length > maxLength;
146
+ const result = truncate ? [...array.slice(0, maxLength)] : array;
147
+ if (truncate && appendIfTruncated) {
148
+ if (isFunction(appendIfTruncated)) {
149
+ return [...result, appendIfTruncated(array.length - maxLength)];
150
+ }
151
+ return [...result, appendIfTruncated];
152
+ }
153
+ return result;
154
+ }
136
155
  // Annotate the CommonJS export names for ESM import in node:
137
156
  0 && (module.exports = {
138
157
  arrayWithPrev,
@@ -144,5 +163,6 @@ function rejectDuplicates(array, getKey = (item) => item) {
144
163
  isInArray,
145
164
  rejectArrayUndefinedValues,
146
165
  rejectDuplicates,
147
- sortBy
166
+ sortBy,
167
+ truncateArray
148
168
  });
@@ -52,5 +52,6 @@ declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item:
52
52
  declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
53
53
  declare function hasDuplicates<T>(array: T[], getKey?: (item: T) => unknown): boolean;
54
54
  declare function rejectDuplicates<T>(array: T[], getKey?: (item: T) => unknown): T[];
55
+ declare function truncateArray<T>(array: T[], maxLength: number, appendIfTruncated?: T | ((truncatedCount: number) => T)): T[];
55
56
 
56
- export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy };
57
+ export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
@@ -52,5 +52,6 @@ declare function findBeforeIndex<T>(array: T[], index: number, predicate: (item:
52
52
  declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
53
53
  declare function hasDuplicates<T>(array: T[], getKey?: (item: T) => unknown): boolean;
54
54
  declare function rejectDuplicates<T>(array: T[], getKey?: (item: T) => unknown): T[];
55
+ declare function truncateArray<T>(array: T[], maxLength: number, appendIfTruncated?: T | ((truncatedCount: number) => T)): T[];
55
56
 
56
- export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy };
57
+ export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findBeforeIndex, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
package/lib/arrayUtils.js CHANGED
@@ -8,8 +8,10 @@ import {
8
8
  isInArray,
9
9
  rejectArrayUndefinedValues,
10
10
  rejectDuplicates,
11
- sortBy
12
- } from "./chunk-XMUFRC2U.js";
11
+ sortBy,
12
+ truncateArray
13
+ } from "./chunk-UTFE4P3P.js";
14
+ import "./chunk-3XCS7FVO.js";
13
15
  export {
14
16
  arrayWithPrev,
15
17
  arrayWithPrevAndIndex,
@@ -20,5 +22,6 @@ export {
20
22
  isInArray,
21
23
  rejectArrayUndefinedValues,
22
24
  rejectDuplicates,
23
- sortBy
25
+ sortBy,
26
+ truncateArray
24
27
  };
@@ -1,3 +1,7 @@
1
+ import {
2
+ isFunction
3
+ } from "./chunk-3XCS7FVO.js";
4
+
1
5
  // src/arrayUtils.ts
2
6
  function filterAndMap(array, mapFilter) {
3
7
  const result = [];
@@ -100,6 +104,17 @@ function rejectDuplicates(array, getKey = (item) => item) {
100
104
  }
101
105
  return result;
102
106
  }
107
+ function truncateArray(array, maxLength, appendIfTruncated) {
108
+ const truncate = array.length > maxLength;
109
+ const result = truncate ? [...array.slice(0, maxLength)] : array;
110
+ if (truncate && appendIfTruncated) {
111
+ if (isFunction(appendIfTruncated)) {
112
+ return [...result, appendIfTruncated(array.length - maxLength)];
113
+ }
114
+ return [...result, appendIfTruncated];
115
+ }
116
+ return result;
117
+ }
103
118
 
104
119
  export {
105
120
  filterAndMap,
@@ -111,5 +126,6 @@ export {
111
126
  findBeforeIndex,
112
127
  rejectArrayUndefinedValues,
113
128
  hasDuplicates,
114
- rejectDuplicates
129
+ rejectDuplicates,
130
+ truncateArray
115
131
  };
@@ -42,11 +42,30 @@ function isPromise(value) {
42
42
  return isObject(value) && "then" in value && isFunction(value.then);
43
43
  }
44
44
 
45
+ // src/arrayUtils.ts
46
+ function truncateArray(array, maxLength, appendIfTruncated) {
47
+ const truncate = array.length > maxLength;
48
+ const result = truncate ? [...array.slice(0, maxLength)] : array;
49
+ if (truncate && appendIfTruncated) {
50
+ if (isFunction(appendIfTruncated)) {
51
+ return [...result, appendIfTruncated(array.length - maxLength)];
52
+ }
53
+ return [...result, appendIfTruncated];
54
+ }
55
+ return result;
56
+ }
57
+
45
58
  // src/sleep.ts
46
59
  function sleep(ms) {
47
60
  return new Promise((resolve) => setTimeout(resolve, ms));
48
61
  }
49
62
 
63
+ // src/stringUtils.ts
64
+ function truncateString(str, length, ellipsis = "\u2026") {
65
+ if (str.length <= length) return str;
66
+ return str.slice(0, length - 1) + ellipsis;
67
+ }
68
+
50
69
  // src/concurrentCalls.ts
51
70
  var ConcurrentCalls = class {
52
71
  #pendingCalls = [];
@@ -266,7 +285,11 @@ var ConcurrentCallsWithMetadata = class {
266
285
  const allFailed = failedProcessing.length === total;
267
286
  const aggregatedError = failedProcessing.length > 0 ? new AggregateError(
268
287
  failedProcessing.map((f) => f.error),
269
- "One or more calls failed"
288
+ `${failedProcessing.length}/${total} calls failed: ${truncateArray(
289
+ failedProcessing.map((f) => truncateString(f.error.message, 20)),
290
+ 5,
291
+ (count) => `+${count} more`
292
+ ).join("; ")}`
270
293
  ) : null;
271
294
  return {
272
295
  succeeded: succeededProcessing,
@@ -1,6 +1,12 @@
1
+ import {
2
+ truncateString
3
+ } from "./chunk-4REIIZQY.js";
1
4
  import {
2
5
  sleep
3
6
  } from "./chunk-5DZT3Z5Z.js";
7
+ import {
8
+ truncateArray
9
+ } from "./chunk-UTFE4P3P.js";
4
10
  import {
5
11
  invariant,
6
12
  isPromise
@@ -226,7 +232,11 @@ var ConcurrentCallsWithMetadata = class {
226
232
  const allFailed = failedProcessing.length === total;
227
233
  const aggregatedError = failedProcessing.length > 0 ? new AggregateError(
228
234
  failedProcessing.map((f) => f.error),
229
- "One or more calls failed"
235
+ `${failedProcessing.length}/${total} calls failed: ${truncateArray(
236
+ failedProcessing.map((f) => truncateString(f.error.message, 20)),
237
+ 5,
238
+ (count) => `+${count} more`
239
+ ).join("; ")}`
230
240
  ) : null;
231
241
  return {
232
242
  succeeded: succeededProcessing,
package/lib/testUtils.cjs CHANGED
@@ -25,6 +25,11 @@ __export(testUtils_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(testUtils_exports);
27
27
 
28
+ // src/assertions.ts
29
+ function isObject(value) {
30
+ return typeof value === "object" && value !== null && !Array.isArray(value);
31
+ }
32
+
28
33
  // src/arrayUtils.ts
29
34
  function filterAndMap(array, mapFilter) {
30
35
  const result = [];
@@ -46,11 +51,6 @@ function arrayWithPrevAndIndex(array) {
46
51
  }));
47
52
  }
48
53
 
49
- // src/assertions.ts
50
- function isObject(value) {
51
- return typeof value === "object" && value !== null && !Array.isArray(value);
52
- }
53
-
54
54
  // src/deepEqual.ts
55
55
  var has = Object.prototype.hasOwnProperty;
56
56
  function find(iter, tar, maxDepth) {
package/lib/testUtils.js CHANGED
@@ -5,13 +5,13 @@ import {
5
5
  import {
6
6
  deepEqual
7
7
  } from "./chunk-JQFUKJU5.js";
8
- import {
9
- arrayWithPrevAndIndex,
10
- filterAndMap
11
- } from "./chunk-XMUFRC2U.js";
12
8
  import {
13
9
  clampMin
14
10
  } from "./chunk-HTCYUMDR.js";
11
+ import {
12
+ arrayWithPrevAndIndex,
13
+ filterAndMap
14
+ } from "./chunk-UTFE4P3P.js";
15
15
  import {
16
16
  isObject
17
17
  } from "./chunk-3XCS7FVO.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "3.8.0",
4
+ "version": "3.9.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "lib"