@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 +1 -1
- package/src/find.js +16 -46
- package/src/find.test.js +13 -27
package/package.json
CHANGED
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.
|
|
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,
|
|
285
|
+
export function findTruthy(array, { key, from = 0, until = array.length } = {}) {
|
|
286
286
|
if (typeof key === "function") {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
|
292
|
-
expect(findTruthy([0,
|
|
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
|
|
297
|
-
expect(findTruthy([0,
|
|
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("
|
|
330
|
-
|
|
331
|
-
expect(findTruthy(
|
|
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
|
|
335
|
-
expect(findTruthy([1, 2
|
|
336
|
-
expect(findTruthy([1, 2
|
|
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
|
})
|