Add update func

This commit is contained in:
dominik-zeglen 2019-12-16 21:23:58 +01:00
parent 3fbaaf0783
commit ba5dac7405
3 changed files with 53 additions and 6 deletions

View file

@ -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`] = ` exports[`Properly calculates output arrays Updates at index 1`] = `
Array [ Array [
"lorem", "lorem",

View file

@ -6,10 +6,12 @@ import {
remove, remove,
removeAtIndex, removeAtIndex,
toggle, toggle,
update,
updateAtIndex updateAtIndex
} from "./lists"; } from "./lists";
const initialArray = ["lorem", "ipsum", "dolor"]; const initialArray = ["lorem", "ipsum", "dolor"];
const compare = (a, b) => a === b;
describe("Properly calculates output arrays", () => { describe("Properly calculates output arrays", () => {
it("Adds", () => { it("Adds", () => {
@ -20,12 +22,28 @@ describe("Properly calculates output arrays", () => {
expect(addAtIndex("sit", initialArray, 2)).toMatchSnapshot(); 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", () => { it("Updates at index", () => {
expect(updateAtIndex("amet", initialArray, 1)).toMatchSnapshot(); expect(updateAtIndex("amet", initialArray, 1)).toMatchSnapshot();
}); });
it("Removes", () => { it("Removes", () => {
expect(remove("ipsum", initialArray, (a, b) => a === b)).toMatchSnapshot(); expect(remove("ipsum", initialArray, compare)).toMatchSnapshot();
}); });
it("Removes at index", () => { it("Removes at index", () => {
@ -33,16 +51,16 @@ describe("Properly calculates output arrays", () => {
}); });
it("Matches", () => { it("Matches", () => {
expect(isSelected("lorem", initialArray, (a, b) => a === b)).toBe(true); expect(isSelected("lorem", initialArray, compare)).toBe(true);
expect(isSelected("sit", initialArray, (a, b) => a === b)).toBe(false); expect(isSelected("sit", initialArray, compare)).toBe(false);
}); });
it("Toggles", () => { it("Toggles", () => {
expect(toggle("lorem", initialArray, (a, b) => a === b)).toMatchSnapshot(); expect(toggle("lorem", initialArray, compare)).toMatchSnapshot();
expect(toggle("sit", initialArray, (a, b) => a === b)).toMatchSnapshot(); expect(toggle("sit", initialArray, compare)).toMatchSnapshot();
}); });
it("Moves", () => { it("Moves", () => {
expect(move("lorem", initialArray, (a, b) => a === b, 1)).toMatchSnapshot(); expect(move("lorem", initialArray, compare, 1)).toMatchSnapshot();
}); });
}); });

View file

@ -30,6 +30,14 @@ export function move<TData>(
return addAtIndex(data, remove(data, list, compare), index); return addAtIndex(data, remove(data, list, compare), index);
} }
export function update<TData>(
data: TData,
list: List<TData>,
compare: Compare<TData>
) {
return add(data, remove(data, list, compare));
}
export function updateAtIndex<TData>( export function updateAtIndex<TData>(
data: TData, data: TData,
list: List<TData>, list: List<TData>,