Last active
February 28, 2020 07:35
-
-
Save busypeoples/b4a5513c50b8a235389a145bf23d2815 to your computer and use it in GitHub Desktop.
Revisions
-
busypeoples revised this gist
Nov 16, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,8 +10,8 @@ const diff = (nextItems, currentItems, key, diffFn) => { ); 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 }; -
busypeoples revised this gist
Sep 23, 2018 . No changes.There are no files selected for viewing
-
busypeoples created this gist
Sep 23, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ 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 diffFn(item, currentItem); }, filter(item => findFn(key, item, currentItems), 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ 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]); }); });