add mapIf and mapIfElse
This commit is contained in:
parent
a9ee54cb06
commit
c6723868d9
@ -1,5 +1,4 @@
|
||||
|
||||
# API
|
||||
# API
|
||||
|
||||
<div class="info">
|
||||
<h4>💡 Info</h4>
|
||||
@ -13,7 +12,7 @@ All functions are curried, Remeda-style, so if you see `f(dataIn, ...others)`, i
|
||||
`updeep-remeda` exports a default function that is an alias to `u.update` and
|
||||
has all the other functions available as props.
|
||||
|
||||
```
|
||||
```
|
||||
import u from '@yanick/updeep-remeda';
|
||||
|
||||
const foo = u({a:1}, { a: x => x + 1 });
|
||||
@ -23,12 +22,12 @@ const bar = u.updateIn({ a: { b: 2 } }, 'a.b', 3 );
|
||||
|
||||
Or you can import the functions piecemeal:
|
||||
|
||||
```
|
||||
```
|
||||
import { updateIn, omit } from '@yanick/updeep-remeda';
|
||||
```
|
||||
|
||||
|
||||
## `u(dataIn, updates)`
|
||||
|
||||
## `u.update(dataIn, updates)`
|
||||
|
||||
Update as many values as you want, as deeply as you want. The `updates` parameter can either be an object, a function, or a value. Everything returned from `u` is frozen recursively.
|
||||
@ -294,6 +293,18 @@ Essentially the same as their Remeda counterparts. The difference being
|
||||
that if the transformation results in no change, the original object/array is
|
||||
returned.
|
||||
|
||||
## `u.map(objectIn, updates)`
|
||||
|
||||
Applies the updates on all entries of `objectIn`.
|
||||
|
||||
## `u.mapIf(objectIn, predicate, updates)`
|
||||
|
||||
Shorthand for `u.map( objectIn, u.if(predicate,updates) )`.
|
||||
|
||||
## `u.mapIfElse(objectIn, predicate, updates, updatesElse)`
|
||||
|
||||
Shorthand for `u.map( objectIn, u.ifElse(predicate,updates,updatesElse) )`.
|
||||
|
||||
## `u.matches(dataIn, condition)`
|
||||
|
||||
Do a deep comparison with `condition`, and returns
|
||||
|
23
src/mapIf.test.ts
Normal file
23
src/mapIf.test.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { expect, test, describe } from "vitest";
|
||||
|
||||
import mapIf, { mapIfElse } from "./mapIf.js";
|
||||
|
||||
test("does updates with the if and else", () => {
|
||||
const object = [
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 2, y: 0 },
|
||||
{ x: 3, y: 0 },
|
||||
];
|
||||
|
||||
expect(mapIfElse(object, { x: 2 }, { y: 2 }, { y: 3 })).toEqual([
|
||||
{ x: 1, y: 3 },
|
||||
{ x: 2, y: 2 },
|
||||
{ x: 3, y: 3 },
|
||||
]);
|
||||
|
||||
expect(mapIf(object, { x: 2 }, { y: 2 })).toEqual([
|
||||
{ x: 1, y: 0 },
|
||||
{ x: 2, y: 2 },
|
||||
{ x: 3, y: 0 },
|
||||
]);
|
||||
});
|
36
src/mapIf.ts
Normal file
36
src/mapIf.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import wrap from "./wrap.js";
|
||||
import matches from "./matches.js";
|
||||
import map from "./map.js";
|
||||
import ifElse from "./ifElse.js";
|
||||
|
||||
function _mapIfElse(object, predicate, trueUpdates, falseUpdates) {
|
||||
const test =
|
||||
typeof predicate === "function"
|
||||
? predicate
|
||||
: typeof predicate === "object"
|
||||
? matches(predicate)
|
||||
: predicate;
|
||||
|
||||
const updates = test ? trueUpdates : falseUpdates;
|
||||
|
||||
return map(object, ifElse(test, trueUpdates, falseUpdates));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export interface MapIf {
|
||||
(object, predicate: Predicate, trueUpdates): unknown;
|
||||
(predicate: Predicate, trueUpdates): (unknown) => unknown;
|
||||
}
|
||||
|
||||
export default wrap(mapIf) as MapIf;
|
||||
export const mapIfElse = _mapIfElse as any as MapIfElse;
|
Loading…
Reference in New Issue
Block a user