Merge branch 'map'

This commit is contained in:
Yanick Champoux 2023-03-22 17:12:52 -04:00
commit 96e98cecfb
8 changed files with 148 additions and 119 deletions

View File

@ -4,6 +4,15 @@
## v2.1.1, 2023-02-22
* add map to the exports
### Statistics
* code churn: 4 files changed, 134 insertions(+), 121 deletions(-)
## v2.1.0, 2023-00-13 ## v2.1.0, 2023-00-13
* add function 'matches' * add function 'matches'

View File

@ -6,16 +6,22 @@ project:
ticket_url: ~ ticket_url: ~
with_stats: true with_stats: true
releases: releases:
- version: v2.1.1
date: 2023-02-22
changes:
- desc: add map to the exports
- desc: 'code churn: 4 files changed, 134 insertions(+), 121 deletions(-)'
type: stats
- version: v2.1.0 - version: v2.1.0
date: 2023-00-13 date: 2023-00-13
changes: changes:
- desc: add function 'matches' - desc: add function 'matches'
- desc: "code churn: 7 files changed, 66 insertions(+), 174 deletions(-)" - desc: 'code churn: 7 files changed, 66 insertions(+), 174 deletions(-)'
type: stats type: stats
- version: v2.0.0 - version: v2.0.0
date: ~ date: ~
changes: changes:
- desc: "Fork from the [mothership](github.com/substantial/updeep), v1.2.1" - desc: 'Fork from the [mothership](github.com/substantial/updeep), v1.2.1'
change_types: change_types:
- keywords: - keywords:
- feat - feat

View File

@ -13,6 +13,7 @@ tasks:
generates: [./CHANGELOG.md] generates: [./CHANGELOG.md]
cmds: cmds:
- changelord print > CHANGELOG.md - changelord print > CHANGELOG.md
test: vitest run test: vitest run
build: build:

View File

@ -1,7 +1,7 @@
{ {
"type": "module", "type": "module",
"name": "@yanick/updeep-remeda", "name": "@yanick/updeep-remeda",
"version": "2.1.0", "version": "2.1.1",
"description": "Easily update nested frozen objects and arrays in a declarative and immutable manner.", "description": "Easily update nested frozen objects and arrays in a declarative and immutable manner.",
"homepage": "https://git.babyl.ca/yanick/updeep-remeda", "homepage": "https://git.babyl.ca/yanick/updeep-remeda",
"repository": { "repository": {

View File

@ -12,23 +12,25 @@ import pickBy from "./pickBy.js";
import filter from "./filter.js"; import filter from "./filter.js";
import reject from "./reject.js"; import reject from "./reject.js";
import matches from "./matches.js"; import matches from "./matches.js";
import myMap from "./map.js";
const functions = { const functions = {
constant,
filter, filter,
reject, freeze,
pickBy, ifElse,
pick, if: _if,
is,
map: myMap,
matches,
omit, omit,
omitBy, omitBy,
constant, pick,
if: _if, pickBy,
ifElse, reject,
is, skip,
freeze,
update, update,
updateIn, updateIn,
skip,
matches,
}; };
export { export {
@ -47,6 +49,7 @@ export {
updateIn, updateIn,
skip, skip,
matches, matches,
myMap as map,
}; };
const merged = update; const merged = update;

View File

@ -21,7 +21,7 @@ function map(object, iteratee) {
return equal ? object : newObject; return equal ? object : newObject;
} }
interface Map { export interface Map {
(object, iteratee): any; (object, iteratee): any;
(iteratee): (object) => any; (iteratee): (object) => any;
} }

View File

@ -15,9 +15,15 @@ describe("update", () => {
expect(result).toBe(orig); expect(result).toBe(orig);
}); });
test.only("with u.skip", () => { test("with u.skip", () => {
const orig = { a: 1 }; const orig = { a: 1 };
const result = u(orig, { b: u.skip }); const result = u(orig, { b: u.skip });
expect(result).toBe(orig); expect(result).toBe(orig);
}); });
test("found in the wild", () => {
const x = { a: { c: 3 }, b: 2 };
const res = u(x, { a: { c: 3 } });
expect(res).toBe(x);
});
}); });

View File

@ -1,61 +1,61 @@
import { expect, it, describe } from "vitest"; import { expect, test, describe } from "vitest";
import u from "./index.js"; import u from "./index.js";
it("does not change anything if no updates are specified", () => { test("does not change anything if no updates are specified", () => {
const object = { foo: 3, bar: [7, 5] }; const object = { foo: 3, bar: [7, 5] };
const result = u(object, {}); const result = u(object, {});
expect(result).to.equal(object); expect(result).to.equal(object);
}); });
it("can update with fixed values", () => { test("can update with fixed values", () => {
const object = { foo: 3, bar: [7, 5] }; const object = { foo: 3, bar: [7, 5] };
const result = u(object, { foo: 4 }); const result = u(object, { foo: 4 });
expect(result).to.deep.equal({ foo: 4, bar: [7, 5] }); expect(result).to.deep.equal({ foo: 4, bar: [7, 5] });
}); });
it("returns the same instance if an update doesn't make changes", () => { test("returns the same instance if an update doesn't make changes", () => {
const object = { foo: 3 }; const object = { foo: 3 };
const result = u({ foo: 3 })(object); const result = u({ foo: 3 })(object);
expect(result).to.equal(object); expect(result).to.equal(object);
}); });
it("can update a nested structure", () => { test("can update a nested structure", () => {
const object = { foo: { bar: 7, bam: 3 }, baz: 32 }; const object = { foo: { bar: 7, bam: 3 }, baz: 32 };
const result = u({ foo: { bar: 8 } })(object); const result = u({ foo: { bar: 8 } })(object);
expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 }); expect(result).to.deep.equal({ foo: { bar: 8, bam: 3 }, baz: 32 });
}); });
it("can update arrays", () => { test("can update arrays", () => {
const object = [1, 2, 3]; const object = [1, 2, 3];
const result = u({ 1: 7 })(object); const result = u({ 1: 7 })(object);
expect(result).to.deep.equal([1, 7, 3]); expect(result).to.deep.equal([1, 7, 3]);
}); });
it("replaces the object outright if updates are a constant", () => { test("replaces the object outright if updates are a constant", () => {
expect(u(3)({})).to.equal(3); expect(u(3)({})).to.equal(3);
expect(u(null)({})).to.be.null; expect(u(null)({})).to.be.null;
}); });
it("can add an element to an array", () => { test("can add an element to an array", () => {
const object = []; const object = [];
const result = u({ 0: 3 })(object); const result = u({ 0: 3 })(object);
expect(result).to.eql([3]); expect(result).to.eql([3]);
}); });
it("can update nested arrays", () => { test("can update nested arrays", () => {
const object = { foo: [1, 2, 3], bar: 9 }; const object = { foo: [1, 2, 3], bar: 9 };
const result = u({ foo: { 1: 7 } })(object); const result = u({ foo: { 1: 7 } })(object);
expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 }); expect(result).to.deep.equal({ foo: [1, 7, 3], bar: 9 });
}); });
it("can use functions to update values", () => { test("can use functions to update values", () => {
const inc = (i) => i + 1; const inc = (i) => i + 1;
const object = { foo: 3, bar: 4, baz: 7 }; const object = { foo: 3, bar: 4, baz: 7 };
const result = u({ foo: inc, bar: inc })(object); const result = u({ foo: inc, bar: inc })(object);
@ -63,7 +63,7 @@ it("can use functions to update values", () => {
expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 }); expect(result).to.deep.equal({ foo: 4, bar: 5, baz: 7 });
}); });
it("can be partially applied", () => { test("can be partially applied", () => {
const inc = (i) => i + 1; const inc = (i) => i + 1;
const object = { foo: 3 }; const object = { foo: 3 };
const incFoo = u({ foo: inc }); const incFoo = u({ foo: inc });
@ -73,37 +73,37 @@ it("can be partially applied", () => {
expect(result).to.deep.equal({ foo: 4 }); expect(result).to.deep.equal({ foo: 4 });
}); });
it("can update if the value is an array", () => { test("can update if the value is an array", () => {
const object = {}; const object = {};
const result = u({ foo: [0, 1] })(object); const result = u({ foo: [0, 1] })(object);
expect(result).to.deep.equal({ foo: [0, 1] }); expect(result).to.deep.equal({ foo: [0, 1] });
}); });
it("can update when original object is undefined", () => { test("can update when original object is undefined", () => {
const result = u({ foo: [0, 1] })(undefined); const result = u({ foo: [0, 1] })(undefined);
expect(result).to.deep.equal({ foo: [0, 1] }); expect(result).to.deep.equal({ foo: [0, 1] });
}); });
it("can take a function as the updater", () => { test("can take a function as the updater", () => {
const result = u((i) => i + 1)(7); const result = u((i) => i + 1)(7);
expect(result).to.eql(8); expect(result).to.eql(8);
}); });
it("deeply freezes the result", () => { test("deeply freezes the result", () => {
const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } }); const result = u({ foo: { bar: 3 } }, { foo: { bar: 0 } });
expect(Object.isFrozen(result)).to.be.true; expect(Object.isFrozen(result)).to.be.true;
expect(Object.isFrozen(result.foo)).to.be.true; expect(Object.isFrozen(result.foo)).to.be.true;
}); });
it("assigns null values", () => { test("assigns null values", () => {
expect(u({ isNull: null }, {})).to.eql({ isNull: null }); expect(u({ isNull: null }, {})).to.eql({ isNull: null });
}); });
it("defaults to an empty object when null or undefined", () => { test("defaults to an empty object when null or undefined", () => {
let result = u({ a: { b: 0 } })({ a: null }); let result = u({ a: { b: 0 } })({ a: null });
expect(result).to.eql({ a: { b: 0 } }); expect(result).to.eql({ a: { b: 0 } });
@ -114,12 +114,12 @@ it("defaults to an empty object when null or undefined", () => {
expect(result).to.eql({ a: { b: 0 } }); expect(result).to.eql({ a: { b: 0 } });
}); });
it("preserves empty objects when empty updates are specified", () => { test("preserves empty objects when empty updates are specified", () => {
const result = u({ a: {} })({}); const result = u({ a: {} })({});
expect(result).to.eql({ a: {} }); expect(result).to.eql({ a: {} });
}); });
it("works with date objects", () => { test("works with date objects", () => {
const date = new Date(); const date = new Date();
const result = u({ created: date })({}); const result = u({ created: date })({});
expect(result).toEqual({ created: date }); expect(result).toEqual({ created: date });
@ -129,11 +129,11 @@ const expectU = (update, orig, expected) =>
expect(update(orig)).to.eql(expected); expect(update(orig)).to.eql(expected);
describe("u.skip", () => { describe("u.skip", () => {
it("omit properties via u.skip", () => { test("omit properties via u.skip", () => {
expectU(u({ a: u.skip, b: (i) => i + 1 }), { a: 1, b: 2 }, { b: 3 }); expectU(u({ a: u.skip, b: (i) => i + 1 }), { a: 1, b: 2 }, { b: 3 });
}); });
it("omit array and object properties", () => { test("omit array and object properties", () => {
expectU( expectU(
u({ a: u.skip, b: "stuff", c: u.skip }), u({ a: u.skip, b: "stuff", c: u.skip }),
{ a: [1, 2, 3], b: "orig", c: { z: "bar" } }, { a: [1, 2, 3], b: "orig", c: { z: "bar" } },
@ -141,7 +141,7 @@ describe("u.skip", () => {
); );
}); });
it("deep omit", () => { test("deep omit", () => {
expectU( expectU(
u({ a: { b: u.skip, c: "stuff" } }), u({ a: { b: u.skip, c: "stuff" } }),
{ a: { b: "foo", z: "bar" } }, { a: { b: "foo", z: "bar" } },
@ -149,19 +149,23 @@ describe("u.skip", () => {
); );
}); });
it("omitting an array item filters it out", () => { test("omitting an array item filters it out", () => {
expectU(u({ 1: u.skip }), ["a", "b", "c"], ["a", "c"]); expectU(u({ 1: u.skip }), ["a", "b", "c"], ["a", "c"]);
}); });
it("doesn't change the obj if nothing is omitted", () => { test("doesn't change the obj if nothing is omitted", () => {
const orig = { a: 1 }; const orig = { a: 1 };
const result = u(orig, { b: u.skip }); const result = u(orig, { b: u.skip });
expect(result).to.be.equal(orig); expect(result).to.be.equal(orig);
}); });
it("doesn't change the array if nothing is omitted", () => { test("doesn't change the array if nothing is omitted", () => {
const orig = [1, 2, 3]; const orig = [1, 2, 3];
const result = u({ 4: u.skip })(orig); const result = u({ 4: u.skip })(orig);
expect(result).to.be.equal(orig); expect(result).to.be.equal(orig);
}); });
}); });
test("has map", () => {
expect(u).toHaveProperty("map");
});