import { useMemo, useRef } from 'react'; import useUpdate from './useUpdate'; import { resolveHookState } from './misc/hookState'; function useList(initialList) { if (initialList === void 0) { initialList = []; } var list = useRef(resolveHookState(initialList)); var update = useUpdate(); var actions = useMemo(function () { var a = { set: function (newList) { list.current = resolveHookState(newList, list.current); update(); }, push: function () { var items = []; for (var _i = 0; _i < arguments.length; _i++) { items[_i] = arguments[_i]; } items.length && actions.set(function (curr) { return curr.concat(items); }); }, updateAt: function (index, item) { actions.set(function (curr) { var arr = curr.slice(); arr[index] = item; return arr; }); }, insertAt: function (index, item) { actions.set(function (curr) { var arr = curr.slice(); index > arr.length ? (arr[index] = item) : arr.splice(index, 0, item); return arr; }); }, update: function (predicate, newItem) { actions.set(function (curr) { return curr.map(function (item) { return (predicate(item, newItem) ? newItem : item); }); }); }, updateFirst: function (predicate, newItem) { var index = list.current.findIndex(function (item) { return predicate(item, newItem); }); index >= 0 && actions.updateAt(index, newItem); }, upsert: function (predicate, newItem) { var index = list.current.findIndex(function (item) { return predicate(item, newItem); }); index >= 0 ? actions.updateAt(index, newItem) : actions.push(newItem); }, sort: function (compareFn) { actions.set(function (curr) { return curr.slice().sort(compareFn); }); }, filter: function (callbackFn, thisArg) { actions.set(function (curr) { return curr.slice().filter(callbackFn, thisArg); }); }, removeAt: function (index) { actions.set(function (curr) { var arr = curr.slice(); arr.splice(index, 1); return arr; }); }, clear: function () { actions.set([]); }, reset: function () { actions.set(resolveHookState(initialList).slice()); }, }; /** * @deprecated Use removeAt method instead */ a.remove = a.removeAt; return a; }, []); return [list.current, actions]; } export default useList;