Skip to content

Commit

Permalink
feat: useList allow pushing multiple items (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
wardoost authored Oct 13, 2019
1 parent a38f026 commit a624364
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/useList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ it('should push duplicated element at the end of the list', () => {
expect(result.current[0]).not.toBe(initList); // checking immutability
});

it('should push multiple elements at the end of the list', () => {
const initList = [1, 2, 3];
const { result } = setUp(initList);
const [, utils] = result.current;

act(() => {
utils.push(4, 5, 6);
});

expect(result.current[0]).toEqual([1, 2, 3, 4, 5, 6]);
expect(result.current[0]).not.toBe(initList); // checking immutability
});

it('should filter current list by provided function', () => {
const initList = [1, -1, 2, -2, 3, -3];
const { result } = setUp(initList);
Expand Down
4 changes: 2 additions & 2 deletions src/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface Actions<T> {
clear: () => void;
updateAt: (index: number, item: T) => void;
remove: (index: number) => void;
push: (item: T) => void;
push: (...items: T[]) => void;
filter: (fn: (value: T) => boolean) => void;
sort: (fn?: (a: T, b: T) => number) => void;
reset: () => void;
Expand All @@ -21,7 +21,7 @@ const useList = <T>(initialList: T[] = []): [T[], Actions<T>] => {
updateAt: (index, entry) =>
set(currentList => [...currentList.slice(0, index), entry, ...currentList.slice(index + 1)]),
remove: index => set(currentList => [...currentList.slice(0, index), ...currentList.slice(index + 1)]),
push: entry => set(currentList => [...currentList, entry]),
push: (...entry) => set(currentList => [...currentList, ...entry]),
filter: fn => set(currentList => currentList.filter(fn)),
sort: (fn?) => set(currentList => [...currentList].sort(fn)),
reset: () => set([...initialList]),
Expand Down

0 comments on commit a624364

Please sign in to comment.