From ba5dac74055c99e57d2268ce3f95530b1cb40808 Mon Sep 17 00:00:00 2001 From: dominik-zeglen Date: Mon, 16 Dec 2019 21:23:58 +0100 Subject: [PATCH] Add update func --- .../lists/__snapshots__/lists.test.ts.snap | 21 +++++++++++++ src/utils/lists/lists.test.ts | 30 +++++++++++++++---- src/utils/lists/lists.ts | 8 +++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/utils/lists/__snapshots__/lists.test.ts.snap b/src/utils/lists/__snapshots__/lists.test.ts.snap index 998d4c2e4..c849a90e0 100644 --- a/src/utils/lists/__snapshots__/lists.test.ts.snap +++ b/src/utils/lists/__snapshots__/lists.test.ts.snap @@ -56,6 +56,27 @@ Array [ ] `; +exports[`Properly calculates output arrays Updates 1`] = ` +Array [ + Object { + "name": "lorem", + "value": 0, + }, + Object { + "name": "ipsum", + "value": 1, + }, + Object { + "name": "dolor", + "value": 2, + }, + Object { + "name": "amet", + "value": 32, + }, +] +`; + exports[`Properly calculates output arrays Updates at index 1`] = ` Array [ "lorem", diff --git a/src/utils/lists/lists.test.ts b/src/utils/lists/lists.test.ts index dc6976b1c..99488dc2f 100644 --- a/src/utils/lists/lists.test.ts +++ b/src/utils/lists/lists.test.ts @@ -6,10 +6,12 @@ import { remove, removeAtIndex, toggle, + update, updateAtIndex } from "./lists"; const initialArray = ["lorem", "ipsum", "dolor"]; +const compare = (a, b) => a === b; describe("Properly calculates output arrays", () => { it("Adds", () => { @@ -20,12 +22,28 @@ describe("Properly calculates output arrays", () => { expect(addAtIndex("sit", initialArray, 2)).toMatchSnapshot(); }); + it("Updates", () => { + expect( + update( + { + name: "amet", + value: 32 + }, + initialArray.map((el, index) => ({ + name: el, + value: index + })), + (a, b) => a.name === b.name + ) + ).toMatchSnapshot(); + }); + it("Updates at index", () => { expect(updateAtIndex("amet", initialArray, 1)).toMatchSnapshot(); }); it("Removes", () => { - expect(remove("ipsum", initialArray, (a, b) => a === b)).toMatchSnapshot(); + expect(remove("ipsum", initialArray, compare)).toMatchSnapshot(); }); it("Removes at index", () => { @@ -33,16 +51,16 @@ describe("Properly calculates output arrays", () => { }); it("Matches", () => { - expect(isSelected("lorem", initialArray, (a, b) => a === b)).toBe(true); - expect(isSelected("sit", initialArray, (a, b) => a === b)).toBe(false); + expect(isSelected("lorem", initialArray, compare)).toBe(true); + expect(isSelected("sit", initialArray, compare)).toBe(false); }); it("Toggles", () => { - expect(toggle("lorem", initialArray, (a, b) => a === b)).toMatchSnapshot(); - expect(toggle("sit", initialArray, (a, b) => a === b)).toMatchSnapshot(); + expect(toggle("lorem", initialArray, compare)).toMatchSnapshot(); + expect(toggle("sit", initialArray, compare)).toMatchSnapshot(); }); it("Moves", () => { - expect(move("lorem", initialArray, (a, b) => a === b, 1)).toMatchSnapshot(); + expect(move("lorem", initialArray, compare, 1)).toMatchSnapshot(); }); }); diff --git a/src/utils/lists/lists.ts b/src/utils/lists/lists.ts index 21005bacc..067fc959c 100644 --- a/src/utils/lists/lists.ts +++ b/src/utils/lists/lists.ts @@ -30,6 +30,14 @@ export function move( return addAtIndex(data, remove(data, list, compare), index); } +export function update( + data: TData, + list: List, + compare: Compare +) { + return add(data, remove(data, list, compare)); +} + export function updateAtIndex( data: TData, list: List,