diff --git a/docs/latest/api.md b/docs/latest/api.md index 2a8d261..21614b5 100644 --- a/docs/latest/api.md +++ b/docs/latest/api.md @@ -1,5 +1,4 @@ - -# API +# API

💡 Info

@@ -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 diff --git a/src/mapIf.test.ts b/src/mapIf.test.ts new file mode 100644 index 0000000..c0a68eb --- /dev/null +++ b/src/mapIf.test.ts @@ -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 }, + ]); +}); diff --git a/src/mapIf.ts b/src/mapIf.ts new file mode 100644 index 0000000..0ff4df4 --- /dev/null +++ b/src/mapIf.ts @@ -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; + +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;