@tim-code/my-util 0.4.4 → 0.4.5

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.4",
3
+ "version": "0.4.5",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/find.js CHANGED
@@ -279,61 +279,31 @@ export function findMax(array, { key, cutoff = -Infinity } = {}) {
279
279
  * If string, then accesses each element at that key to get value.
280
280
  * If function, then calls the callback on each element to get value.
281
281
  * @param {number=} $1.from Numeric index to start from: inclusive, defaults to 0
282
- * @param {number=} $1.to Numeric index to end at: inclusive, defaults to `array.length - 1`
282
+ * @param {number=} $1.until Numeric index to end at: exclusive, defaults to `array.length`
283
283
  * @returns {T}
284
284
  */
285
- export function findTruthy(array, { key, from = 0, to = array.length - 1 }) {
285
+ export function findTruthy(array, { key, from = 0, until = array.length } = {}) {
286
286
  if (typeof key === "function") {
287
- if (from <= to) {
288
- for (let i = from; i <= to; i++) {
289
- const element = array[i]
290
- const value = key(element, i, array)
291
- if (value) {
292
- return element
293
- }
294
- }
295
- } else {
296
- for (let i = from; i >= to; i--) {
297
- const element = array[i]
298
- const value = key(element, i, array)
299
- if (value) {
300
- return element
301
- }
287
+ for (let i = from; i < until; i++) {
288
+ const element = array[i]
289
+ const value = key(element, i, array)
290
+ if (value) {
291
+ return element
302
292
  }
303
293
  }
304
294
  } else if (typeof key === "number" || typeof key === "string") {
305
- if (from <= to) {
306
- for (let i = from; i <= to; i++) {
307
- const element = array[i]
308
- const value = element[key]
309
- if (value) {
310
- return element
311
- }
312
- }
313
- } else {
314
- for (let i = from; i >= to; i--) {
315
- const element = array[i]
316
- const value = element[key]
317
- if (value) {
318
- return element
319
- }
295
+ for (let i = from; i < until; i++) {
296
+ const element = array[i]
297
+ const value = element[key]
298
+ if (value) {
299
+ return element
320
300
  }
321
301
  }
322
302
  } else {
323
- // eslint-disable-next-line no-lonely-if
324
- if (from <= to) {
325
- for (let i = from; i <= to; i++) {
326
- const value = array[i]
327
- if (value) {
328
- return value
329
- }
330
- }
331
- } else {
332
- for (let i = from; i >= to; i--) {
333
- const value = array[i]
334
- if (value) {
335
- return value
336
- }
303
+ for (let i = from; i < until; i++) {
304
+ const value = array[i]
305
+ if (value) {
306
+ return value
337
307
  }
338
308
  }
339
309
  }
package/src/find.test.js CHANGED
@@ -288,53 +288,39 @@ describe("findMax", () => {
288
288
  })
289
289
 
290
290
  describe("findTruthy", () => {
291
- it("returns the first truthy value in a simple array", () => {
292
- expect(findTruthy([0, false, null, 2, 3], {})).toBe(2)
293
- expect(findTruthy([0, false, null, undefined], {})).toBeUndefined()
291
+ it("returns the first truthy value in array", () => {
292
+ expect(findTruthy([0, null, false, 2, 3], {})).toBe(2)
294
293
  })
295
294
 
296
- it("returns the first truthy value in a range [from, to]", () => {
297
- expect(findTruthy([0, 1, 2, 0, 3, 0], { from: 2, to: 5 })).toBe(2)
298
- expect(findTruthy([0, 0, 0, 4, 0], { from: 1, to: 3 })).toBe(4)
299
- expect(findTruthy([0, 0, 0, 0], { from: 1, to: 2 })).toBeUndefined()
300
- })
301
-
302
- it("returns the first truthy value when searching backwards (from > to)", () => {
303
- expect(findTruthy([0, 1, 2, 0, 3, 0], { from: 4, to: 1 })).toBe(3)
304
- expect(findTruthy([0, 0, 0, 4, 0], { from: 3, to: 0 })).toBe(4)
305
- expect(findTruthy([0, 0, 0, 0], { from: 2, to: 0 })).toBeUndefined()
295
+ it("returns undefined if no truthy value", () => {
296
+ expect(findTruthy([0, null, false], {})).toBeUndefined()
306
297
  })
307
298
 
308
299
  it("supports key as function", () => {
309
300
  const arr = [{ v: 0 }, { v: 2 }, { v: 0 }]
310
301
  expect(findTruthy(arr, { key: (e) => e.v })).toEqual({ v: 2 })
311
- expect(findTruthy(arr, { key: (e) => e.v, from: 2, to: 0 })).toEqual({ v: 2 })
312
- expect(findTruthy(arr, { key: (e) => e.v, from: 0, to: 0 })).toBeUndefined()
313
302
  })
314
303
 
315
304
  it("supports key as string", () => {
316
305
  const arr = [{ x: 0 }, { x: 2 }, { x: 0 }]
317
306
  expect(findTruthy(arr, { key: "x" })).toEqual({ x: 2 })
318
- expect(findTruthy(arr, { key: "x", from: 2, to: 0 })).toEqual({ x: 2 })
319
- expect(findTruthy(arr, { key: "x", from: 0, to: 0 })).toBeUndefined()
320
307
  })
321
308
 
322
309
  it("supports key as number", () => {
323
310
  const arr = [[0], [2], [0]]
324
311
  expect(findTruthy(arr, { key: 0 })).toEqual([2])
325
- expect(findTruthy(arr, { key: 0, from: 2, to: 0 })).toEqual([2])
326
- expect(findTruthy(arr, { key: 0, from: 0, to: 0 })).toBeUndefined()
327
312
  })
328
313
 
329
- it("returns undefined for empty array", () => {
330
- expect(findTruthy([], {})).toBeUndefined()
331
- expect(findTruthy([], { from: 0, to: 0 })).toBeUndefined()
314
+ it("respects from and until (forward)", () => {
315
+ const arr = [0, 1, 2, 3]
316
+ expect(findTruthy(arr, { from: 2, until: 4 })).toBe(2)
317
+ expect(findTruthy(arr, { from: 1, until: 3 })).toBe(1)
318
+ expect(findTruthy(arr, { from: 3, until: 4 })).toBe(3)
319
+ expect(findTruthy(arr, { from: 3, until: 3 })).toBeUndefined()
332
320
  })
333
321
 
334
- it("returns undefined if from/to out of bounds", () => {
335
- expect(findTruthy([1, 2, 3], { from: 10, to: 12 })).toBeUndefined()
336
- expect(findTruthy([1, 2, 3], { from: -5, to: -1 })).toBeUndefined()
337
- expect(findTruthy([1, 2, 3], { from: 2, to: 10 })).toBe(3)
338
- expect(findTruthy([1, 2, 3], { from: 10, to: 2 })).toBe(3)
322
+ it("returns undefined if from >= until", () => {
323
+ expect(findTruthy([0, 1, 2], { from: 2, until: 2 })).toBeUndefined()
324
+ expect(findTruthy([0, 1, 2], { from: 3, until: 2 })).toBeUndefined()
339
325
  })
340
326
  })