From c6723868d9469f32bfeb209c9e93b8cf4db90971 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 27 Feb 2024 09:13:01 -0500 Subject: [PATCH 1/2] add mapIf and mapIfElse --- docs/latest/api.md | 21 ++++++++++++++++----- src/mapIf.test.ts | 23 +++++++++++++++++++++++ src/mapIf.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 src/mapIf.test.ts create mode 100644 src/mapIf.ts 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; From 190bb7b80ab39c2d4976782b1619e079d705128a Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 27 Feb 2024 12:10:40 -0500 Subject: [PATCH 2/2] changelog --- .../2024-02-27T12:09:48-paradoxically-precepts-baseball.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/2024-02-27T12:09:48-paradoxically-precepts-baseball.md diff --git a/.changeset/2024-02-27T12:09:48-paradoxically-precepts-baseball.md b/.changeset/2024-02-27T12:09:48-paradoxically-precepts-baseball.md new file mode 100644 index 0000000..e575984 --- /dev/null +++ b/.changeset/2024-02-27T12:09:48-paradoxically-precepts-baseball.md @@ -0,0 +1,5 @@ +--- +"@yanick/updeep-remeda": minor +--- + +add functions mapIf and mapIfElse