feat: useList allow pushing multiple items (#621)

This commit is contained in:
Ward 2019-10-14 07:38:04 +11:00 committed by GitHub
parent a38f026beb
commit a624364d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -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);

View File

@ -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;
@ -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]),