@tim-code/my-util 0.4.2 → 0.4.3

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.2",
3
+ "version": "0.4.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
package/src/find.js CHANGED
@@ -337,3 +337,14 @@ export function findLastFrom(array, fromIndex, callback) {
337
337
  }
338
338
  return array[foundIndex]
339
339
  }
340
+
341
+ /**
342
+ * Returns truthy if argument is truthy and falsy otherwise.
343
+ * Meant to used with find(): `array.find(isTruthy)`
344
+ * @template T
345
+ * @param {T} anything
346
+ * @returns {T}
347
+ */
348
+ export function isTruthy(anything) {
349
+ return anything
350
+ }
package/src/find.test.js CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-restricted-syntax */
1
2
  import { describe, expect, it } from "@jest/globals"
2
3
 
3
4
  const {
@@ -13,6 +14,7 @@ const {
13
14
  findFrom,
14
15
  findLastIndexFrom,
15
16
  findLastFrom,
17
+ isTruthy,
16
18
  } = await import("./find.js")
17
19
 
18
20
  describe("findClosestAbs", () => {
@@ -402,3 +404,27 @@ describe("findLastFrom", () => {
402
404
  expect(called[0][2]).toBe(arr)
403
405
  })
404
406
  })
407
+
408
+ describe("isTruthy", () => {
409
+ it("returns the argument if it is truthy", () => {
410
+ expect(isTruthy(1)).toBe(1)
411
+ expect(isTruthy("foo")).toBe("foo")
412
+ expect(isTruthy({})).toEqual({})
413
+ expect(isTruthy([])).toEqual([])
414
+ expect(isTruthy(true)).toBe(true)
415
+ })
416
+
417
+ it("returns the argument if it is falsy", () => {
418
+ expect(isTruthy(0)).toBe(0)
419
+ expect(isTruthy("")).toBe("")
420
+ expect(isTruthy(null)).toBe(null)
421
+ expect(isTruthy(undefined)).toBe(undefined)
422
+ expect(isTruthy(false)).toBe(false)
423
+ expect(isTruthy(NaN)).toBe(NaN)
424
+ })
425
+
426
+ it("can be used with Array.prototype.find to filter out falsy values", () => {
427
+ const arr = [0, null, undefined, false, "", "hello", 42]
428
+ expect(arr.find(isTruthy)).toBe("hello")
429
+ })
430
+ })