diff --git a/.changeset/2024-02-27T13:25:15-dayton-prearrangements-ute.md b/.changeset/2024-02-27T13:25:15-dayton-prearrangements-ute.md new file mode 100644 index 0000000..ddc1128 --- /dev/null +++ b/.changeset/2024-02-27T13:25:15-dayton-prearrangements-ute.md @@ -0,0 +1,5 @@ +--- +"@yanick/updeep-remeda": minor +--- + +object predicates are now shortcuts for `u.matches` diff --git a/README.md b/README.md index c5b406c..32e5c2f 100644 --- a/README.md +++ b/README.md @@ -357,8 +357,10 @@ expect(alwaysFour(32)).to.eql(4); ### `u.if(dataIn, predicate, updates)` -Apply `updates` if `predicate` is truthy, or if `predicate` is a function. -It evaluates to truthy when called with `object`. +Apply `updates` if `predicate` evaluates to true. The `predicate` can +be a boolean, or a function taking in `dataIn` and returning a boolean, +or an object, in which case it'll be treated as a shortcut for +`u.matches(predicate)`. ```js function isEven(x) { diff --git a/Taskfile.yaml b/Taskfile.yaml index 85ab53b..1cae561 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -30,7 +30,7 @@ tasks: integrate: deps: [prettier, test, build] cmds: - - changeset status + - git diff-ls main | grep .changeset/ - sh: git branch | grep '* {{.PARENT_BRANCH}}' msg: not on target - { task: checkout-clean } diff --git a/src/filter.ts b/src/filter.ts index 8db4048..d8ec76f 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -1,11 +1,12 @@ import { filter as _filter } from "remeda"; +import { buildPredicate } from "./predicate.js"; import wrap from "./wrap.js"; const sizeOf = (obj) => obj.length; function filter(dataIn, predicate) { - const result = _filter.indexed(dataIn, predicate); + const result = _filter.indexed(dataIn, buildPredicate(predicate)); return sizeOf(result) === sizeOf(dataIn) ? dataIn : result; } diff --git a/src/if.test.ts b/src/if.test.ts index adc0df6..4fd79d3 100644 --- a/src/if.test.ts +++ b/src/if.test.ts @@ -5,7 +5,7 @@ import _if from "./if.js"; describe("if", () => { test("does not update if the predicate is false", () => { const object = { a: 0 }; - let result = _if(false, { b: 1 }, object); + let result = _if(object, false, { b: 1 }); expect(result).to.eql(object); result = _if(0, false, 1); @@ -21,7 +21,7 @@ describe("if", () => { test("will use the result of a function passed as a predicate", () => { const object = { a: 0 }; const aIsThree = (x) => x.a === 3; - const result = _if(aIsThree, { b: 1 }, object); + const result = _if(aIsThree, { b: 1 })(object); expect(result).to.eql({ a: 0 }); }); diff --git a/src/ifElse.ts b/src/ifElse.ts index ea71ec8..8280271 100644 --- a/src/ifElse.ts +++ b/src/ifElse.ts @@ -1,16 +1,15 @@ import update from "./update.js"; import wrap from "./wrap.js"; +import { Predicate, buildPredicate } from "./predicate.js"; function updateIfElse(object, predicate, trueUpdates, falseUpdates) { - const test = typeof predicate === "function" ? predicate(object) : predicate; + const test = buildPredicate(predicate)(object); const updates = test ? trueUpdates : falseUpdates; return update(object, updates); } -type Predicate = ((source: any) => boolean) | boolean; - export interface IfElse { (object, predicate: Predicate, trueUpdates, falseUpdates): unknown; (predicate: Predicate, trueUpdates, falseUpdates): (unknown) => unknown; diff --git a/src/mapIf.ts b/src/mapIf.ts index 0ff4df4..a2c0a20 100644 --- a/src/mapIf.ts +++ b/src/mapIf.ts @@ -2,16 +2,10 @@ import wrap from "./wrap.js"; import matches from "./matches.js"; import map from "./map.js"; import ifElse from "./ifElse.js"; +import { Predicate, buildPredicate } from "./predicate.js"; function _mapIfElse(object, predicate, trueUpdates, falseUpdates) { - const test = - typeof predicate === "function" - ? predicate - : typeof predicate === "object" - ? matches(predicate) - : predicate; - - const updates = test ? trueUpdates : falseUpdates; + const test = buildPredicate(predicate); return map(object, ifElse(test, trueUpdates, falseUpdates)); } @@ -20,8 +14,6 @@ function mapIf(object, predicate, trueUpdates) { return _mapIfElse(object, predicate, trueUpdates, (x) => x); } -type Predicate = ((source: any) => boolean) | boolean | Record; - export interface MapIfElse { (object, predicate: Predicate, trueUpdates, falseUpdates): unknown; (predicate: Predicate, trueUpdates, falseUpdates): (unknown) => unknown; diff --git a/src/omitBy.ts b/src/omitBy.ts index 915852c..9c0bc63 100644 --- a/src/omitBy.ts +++ b/src/omitBy.ts @@ -1,11 +1,12 @@ import { omitBy as _omitBy } from "remeda"; +import { buildPredicate } from "./predicate.js"; import wrap from "./wrap.js"; const sizeOf = (obj) => Object.keys(obj).length; function omitBy(dataIn, predicate) { - const result = _omitBy(dataIn, predicate); + const result = _omitBy(dataIn, buildPredicate(predicate)); return sizeOf(result) === sizeOf(dataIn) ? dataIn : result; } diff --git a/src/pickBy.ts b/src/pickBy.ts index 5c75ee6..87d9eb4 100644 --- a/src/pickBy.ts +++ b/src/pickBy.ts @@ -1,11 +1,12 @@ import { pickBy as _pick } from "remeda"; +import { buildPredicate } from "./predicate.js"; import wrap from "./wrap.js"; const sizeOf = (obj) => Object.keys(obj).length; function pickBy(dataIn, predicate) { - const result = _pick(dataIn, predicate); + const result = _pick(dataIn, buildPredicate(predicate)); return sizeOf(result) === sizeOf(dataIn) ? dataIn : result; } diff --git a/src/predicate.ts b/src/predicate.ts new file mode 100644 index 0000000..1a3f527 --- /dev/null +++ b/src/predicate.ts @@ -0,0 +1,12 @@ +import matches from "./matches.js"; + +export type Predicate = + | ((source: any) => boolean) + | boolean + | Record; + +export function buildPredicate(predicate) { + if (typeof predicate === "function") return predicate; + if (typeof predicate === "object") return matches(predicate); + return () => !!predicate; +} diff --git a/src/reject.ts b/src/reject.ts index 8cd614b..24d495b 100644 --- a/src/reject.ts +++ b/src/reject.ts @@ -1,11 +1,12 @@ import { reject as _reject } from "remeda"; +import { buildPredicate } from "./predicate.js"; import wrap from "./wrap.js"; const sizeOf = (obj) => obj.length; function reject(dataIn, predicate) { - const result = _reject.indexed(dataIn, predicate); + const result = _reject.indexed(dataIn, buildPredicate(predicate)); return sizeOf(result) === sizeOf(dataIn) ? dataIn : result; }