Merge branch 'predicate'
This commit is contained in:
commit
7173ae952c
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@yanick/updeep-remeda": minor
|
||||
---
|
||||
|
||||
object predicates are now shortcuts for `u.matches`
|
@ -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) {
|
||||
|
@ -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 }
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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 });
|
||||
});
|
||||
|
@ -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;
|
||||
|
12
src/mapIf.ts
12
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<string, any>;
|
||||
|
||||
export interface MapIfElse {
|
||||
(object, predicate: Predicate, trueUpdates, falseUpdates): unknown;
|
||||
(predicate: Predicate, trueUpdates, falseUpdates): (unknown) => unknown;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
12
src/predicate.ts
Normal file
12
src/predicate.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import matches from "./matches.js";
|
||||
|
||||
export type Predicate =
|
||||
| ((source: any) => boolean)
|
||||
| boolean
|
||||
| Record<string, any>;
|
||||
|
||||
export function buildPredicate(predicate) {
|
||||
if (typeof predicate === "function") return predicate;
|
||||
if (typeof predicate === "object") return matches(predicate);
|
||||
return () => !!predicate;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user