Skip to content

Commit

Permalink
refactor: 💡 rename comparison function
Browse files Browse the repository at this point in the history
  • Loading branch information
PetterIve committed Aug 5, 2019
1 parent 6875e13 commit 8462a59
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/useUpsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ export interface Actions<T> extends ListActions<T> {
}

const useUpsert = <T>(
itemsAreTheSame: (upsertedItem: T, existingItem: T) => boolean,
comparisonFunction: (upsertedItem: T, existingItem: T) => boolean,
initialList: T[] = []
): [T[], Actions<T>] => {
const [items, actions] = useList(initialList);

const upsert = (upsertedItem: T) => {
const itemAlreadyExists = items.find(item => itemsAreTheSame(upsertedItem, item));
const itemAlreadyExists = items.find(item => comparisonFunction(upsertedItem, item));
if (itemAlreadyExists) {
return actions.set(
items.map(existingItem => {
if (itemsAreTheSame(upsertedItem, existingItem)) {
if (comparisonFunction(upsertedItem, existingItem)) {
return upsertedItem;
}
return existingItem;
Expand Down

0 comments on commit 8462a59

Please sign in to comment.