@tim-code/my-util 0.4.0 → 0.4.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/find.js CHANGED
@@ -153,6 +153,7 @@ export function findClosestGTE(array, desired, { key, cutoff = Infinity } = {})
153
153
 
154
154
  /**
155
155
  * Find the closest element in an array. If there is a tie, then returns the first matching element by order in the array.
156
+ * If some values are undefined or null, they will be ignored. If no element is found, returns undefined.
156
157
  * If using for strings, need to specify different values for "cutoff" and "comparator".
157
158
  * "~" and "" are good cutoff string values for gt/gte and lt/lte respectively.
158
159
  * @template T, V
package/src/promise.js CHANGED
@@ -70,10 +70,15 @@ export async function sleep(ms) {
70
70
  * @param {Function=} $1.limiter A function awaited after a group of parallel calls is processed.
71
71
  * It is called with the number of parallel calls processed. Could be as simple as `() => sleep(10000)` if you wanted to wait 10 seconds between.
72
72
  * @param {boolean=} $1.flatten Flattens values before returning; useful if promises return arrays
73
+ * @param {boolean=} $1.abort If true, will return early if there are errors.
74
+ * If false (default), will process all elements in the array (like Promise.allSettled()).
73
75
  * @param {Function} callback
74
76
  * @returns {Object} {results, values, returned, errors}
75
77
  */
76
- export async function allSettled({ array, limit, limiter, flatten = false }, callback) {
78
+ export async function allSettled(
79
+ { array, limit, limiter, flatten = false, abort = false },
80
+ callback
81
+ ) {
77
82
  const results = []
78
83
  let returned = []
79
84
  let values = []
@@ -92,6 +97,9 @@ export async function allSettled({ array, limit, limiter, flatten = false }, cal
92
97
  errors.push(reason)
93
98
  }
94
99
  }
100
+ if (abort && errors.length) {
101
+ break
102
+ }
95
103
  await limiter?.(elements.length)
96
104
  }
97
105
  if (flatten) {
@@ -2,7 +2,15 @@
2
2
  /* eslint-disable prefer-promise-reject-errors */
3
3
  import { jest } from "@jest/globals"
4
4
 
5
- import { alert, allSettled, poll, PollError, sleep, throwFirstReject, intervalLimiter } from "./promise.js"
5
+ import {
6
+ alert,
7
+ allSettled,
8
+ intervalLimiter,
9
+ poll,
10
+ PollError,
11
+ sleep,
12
+ throwFirstReject,
13
+ } from "./promise.js"
6
14
 
7
15
  describe("poll", () => {
8
16
  it("resolves immediately if callback returns a non-undefined/null/false value", async () => {
@@ -182,6 +190,22 @@ describe("allSettled", () => {
182
190
  expect(limiter).toHaveBeenCalledTimes(3)
183
191
  expect(limiterCalls).toEqual([2, 2, 1])
184
192
  })
193
+
194
+ it("returns early if abort=true and any error occurs", async () => {
195
+ // Should process only up to the first chunk with a rejection, then stop
196
+ const arr = [1, 2, 3, 4, 5, 6]
197
+ const cb = jest
198
+ .fn()
199
+ .mockImplementation((x) => (x === 2 || x === 4 ? Promise.reject(`fail${x}`) : x))
200
+ // limit=2 so chunks: [1,2], [3,4], [5,6]
201
+ const result = await allSettled({ array: arr, limit: 2, abort: true }, cb)
202
+ // The first chunk: [1,2] => 1 fulfilled, 1 rejected
203
+ // Should stop after first chunk with error
204
+ expect(result.values.length).toBe(2)
205
+ expect(result.errors).toEqual(["fail2"])
206
+ expect(cb).toHaveBeenCalledTimes(2)
207
+ // Should not process [3,4] or [5,6]
208
+ })
185
209
  })
186
210
 
187
211
  describe("intervalLimiter", () => {