Last active
February 28, 2020 07:35
-
-
Save busypeoples/b4a5513c50b8a235389a145bf23d2815 to your computer and use it in GitHub Desktop.
Diff 2 items and group into (toCreate, toUpdate, toDelete)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { find, filter, propEq, reject } from "ramda"; | |
const findFn = (key, selectedItem, items) => | |
find(item => item[key] === selectedItem[key], items); | |
const diff = (nextItems, currentItems, key, diffFn) => { | |
const toCreate = reject( | |
item => item[key] && findFn(key, item, currentItems), | |
nextItems | |
); | |
const toUpdate = filter(item => { | |
const currentItem = find(propEq(key, item[key]), currentItems); | |
return currentItem && diffFn(item, currentItem); | |
}, nextItems); | |
const toDelete = reject(item => findFn(key, item, nextItems), currentItems); | |
return { toCreate, toUpdate, toDelete }; | |
}; | |
export default diff; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { deepEqual } from "assert"; | |
import diff from "./index"; | |
const key = "testId"; | |
const diffFn = (a, b) => a.name !== b.name; | |
const item1 = { testId: 1, name: "First Item" }; | |
const item2 = { testId: 2, name: "Second Item" }; | |
const item3 = { testId: 3, name: "Third Item" }; | |
const item4 = { testId: 4, name: "Fourth Item" }; | |
const item5 = { testId: 5, name: "Fifth Item" }; | |
describe("utils/diff", () => { | |
it("should return the to be created items", () => { | |
const newItem = { name: "New Item" }; | |
const nextItems = [newItem]; | |
const previousItems = [item1, item3]; | |
const { toCreate } = diff(nextItems, previousItems, key, diffFn); | |
deepEqual(toCreate, [newItem]); | |
}); | |
it("should return the to be upated items", () => { | |
const updatedItem = { testId: 2, name: "UpdatedItem" }; | |
const nextItems = [item1, updatedItem, item3]; | |
const previousItems = [item1, item2, item3]; | |
const { toUpdate } = diff(nextItems, previousItems, key, diffFn); | |
deepEqual(toUpdate, [updatedItem]); | |
}); | |
it("should return the to be deleted items", () => { | |
const nextItems = [item1, item3]; | |
const previousItems = [item1, item2, item3]; | |
const { toDelete } = diff(nextItems, previousItems, key, diffFn); | |
deepEqual(toDelete, [item2]); | |
}); | |
it("should return all to be created, updated and deleted items", () => { | |
const updatedItem1 = { testId: 1, name: "UpdatedItem1" }; | |
const updatedItem2 = { testId: 2, name: "UpdatedItem2" }; | |
const nextItems = [updatedItem1, updatedItem2, item3, item5]; | |
const previousItems = [item1, item2, item3, item4]; | |
const { toCreate, toUpdate, toDelete } = diff( | |
nextItems, | |
previousItems, | |
key, | |
diffFn | |
); | |
deepEqual(toCreate, [item5]); | |
deepEqual(toUpdate, [updatedItem1, updatedItem2]); | |
deepEqual(toDelete, [item4]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment