UNPKG

@augmentcode/ag-redux-toolkit

Version:

Redux + Saga state management for Svelte apps — designed for AI agents. Includes runtime TypeScript source, AI skills, and reference documentation for robust, observable, high-performance state management.

292 lines (291 loc) • 9.18 kB
import { shallowEqual } from "fast-equals"; //#region src/utils/collections/collection-utils.ts function isString(value) { if (typeof value !== "string") return false; return true; } function createCollection(idFieldName, items) { if (!items) return { idField: idFieldName, ids: [], map: {}, refsCount: {} }; return { idField: idFieldName, ids: Array.from(new Set(items.map((item) => item[idFieldName]))), map: items.reduce((acc, item) => { const id = item[idFieldName]; return { ...acc, [id]: item }; }, {}), refsCount: {} }; } var collectionFieldsSet = new Set(Object.keys(createCollection("id", []))); function isCollection(item) { const record = item; if (typeof record.idField !== "string") return false; if (!Array.isArray(record.ids)) return false; if (typeof record.map !== "object" || record.map === null || Array.isArray(record.map)) return false; if (typeof record.refsCount !== "object" || record.refsCount === null || Array.isArray(record.refsCount)) return false; const keys = Object.keys(item); for (const itemKey of keys) if (!collectionFieldsSet.has(itemKey)) return false; return true; } function purgeCollection(collection) { return { ...collection, ids: [], map: {}, refsCount: {} }; } function addItem(collection, newItem) { return addItems(collection, [newItem]); } /** * Batch add function * @param collection - The collection to add items to * @param newItems - Array of items to add * @returns New collection with items added (or original if no items were added) */ function addItems(collection, newItems) { if (newItems.length === 0) return collection; const { idField } = collection; const newIdsToAdd = []; const newMapEntries = {}; const existingIdsSet = new Set(collection.ids); for (const item of newItems) { const id = item[idField]; if (!isString(id)) continue; const existsInMap = !!collection.map[id]; const existsInIds = existingIdsSet.has(id); if (existsInMap && existsInIds) continue; if (!existsInIds) { newIdsToAdd.push(id); existingIdsSet.add(id); } newMapEntries[id] = item; } if (newIdsToAdd.length === 0 && Object.keys(newMapEntries).length === 0) return collection; const newIds = newIdsToAdd.length > 0 ? [...collection.ids, ...newIdsToAdd] : collection.ids; const newMap = Object.keys(newMapEntries).length > 0 ? { ...collection.map, ...newMapEntries } : collection.map; return { ...collection, ids: newIds, map: newMap }; } function addItemAt(collection, index, newItem) { const { idField } = collection; const id = newItem[idField]; if (!isString(id)) return collection; if (collection.map[id]) return collection; const idsWithoutCurrent = collection.ids.filter((itemId) => itemId !== id); let ids = []; if (index < 0) ids = [id].concat(idsWithoutCurrent); else { ids = [...idsWithoutCurrent]; ids.splice(index, 0, id); } return { ...collection, ids, map: { ...collection.map, [id]: newItem } }; } function addItemAndCountRef(collection, newItem, index) { const { idField } = collection; const id = newItem[idField]; if (!isString(id)) return collection; return increaseRefsCount(index !== void 0 ? addItemAt(collection, index, newItem) : addItem(collection, newItem), id); } function upsertItem(collection, updatedItem) { const id = updatedItem[collection.idField]; if (!isString(id)) return collection; if (getItem(collection, id)) return updateItem(collection, updatedItem); return addItem(collection, updatedItem); } function upsertItemAndCountRef(collection, newItem) { const { idField } = collection; const id = newItem[idField]; if (!isString(id)) return collection; return increaseRefsCount(upsertItem(collection, newItem), id); } function updateItem(collection, updatedItem) { const { idField } = collection; const id = updatedItem[idField]; if (!id || !isString(id) || !collection.map[id]) return collection; const newItem = { ...collection.map[id], ...updatedItem }; if (shallowEqual(newItem, collection.map[id])) return collection; return { ...collection, map: { ...collection.map, [id]: newItem } }; } function replaceItem(collection, originalItemId, updatedItem) { return replaceItems(collection, [[originalItemId, updatedItem]]); } function replaceItems(collection, replaceTuples) { const idsIndexMap = {}; const newItemsMap = {}; for (const tuple of replaceTuples) { const [originalItemId, updatedItem] = tuple; const originalIndex = collection.ids.indexOf(originalItemId); if (originalIndex < 0) continue; const newId = updatedItem[collection.idField]; if (!isString(newId)) continue; idsIndexMap[originalIndex] = newId; newItemsMap[newId] = updatedItem; } if (Object.keys(idsIndexMap).length === 0) return collection; const newIds = [...collection.ids]; const newMap = { ...collection.map, ...newItemsMap }; const newRefCounts = { ...collection.refsCount }; const idsToRemove = /* @__PURE__ */ new Set(); for (let index = 0; index < collection.ids.length; index++) { const newId = idsIndexMap[index]; const oldId = collection.ids[index]; if (!newId) continue; if (oldId !== newId) { delete newMap[oldId]; const existingIndex = collection.ids.indexOf(newId); if (existingIndex >= 0 && existingIndex !== index) idsToRemove.add(newId); } newIds[index] = newId; newRefCounts[newId] = newRefCounts[oldId]; if (oldId !== newId) delete newRefCounts[oldId]; } const finalIds = newIds.filter((id, index, list) => { return !(idsToRemove.has(id) && list.indexOf(id) !== index); }); return { ...collection, ids: finalIds, map: { ...newMap }, refsCount: newRefCounts }; } function removeItem(collection, itemId) { if (!isString(itemId)) return collection; const ids = collection.ids.filter((id) => { return id !== itemId; }); if (ids.length === collection.ids.length && !collection.map[itemId]) return collection; const { [itemId]: _item, ...newMap } = collection.map; const { [itemId]: _count, ...newRefsCount } = collection.refsCount; return { ...collection, ids, map: newMap, refsCount: newRefsCount }; } function increaseRefsCount(collection, itemId) { if (!isString(itemId)) return collection; return { ...collection, refsCount: { ...collection.refsCount, [itemId]: (collection.refsCount[itemId] || 0) + 1 } }; } function decreaseRefsCount(collection, itemId) { if (!isString(itemId)) return collection; const newRefsCount = (collection.refsCount[itemId] || 0) - 1; if (newRefsCount <= 0) { const { [itemId]: _count, ...newRefsCount } = collection.refsCount; return removeItem({ ...collection, refsCount: newRefsCount }, itemId); } return { ...collection, refsCount: { ...collection.refsCount, [itemId]: newRefsCount } }; } function filterCollection(collection, filterFunction) { const items = collection.ids.map((id) => { return collection.map[id]; }).filter((item) => item !== void 0).filter(filterFunction); if (items.length === collection.ids.length) return collection; return createCollection(collection.idField, items); } function deduplicateCollection(collection) { const ids = collection.ids.filter((id, index, list) => { return list.indexOf(id) === index; }); if (ids.length === collection.ids.length) return collection; return { ...collection, ids }; } function getItem(collection, itemId) { return collection.map[itemId]; } function getLastItem(collection) { const lastItemId = collection.ids[collection.ids.length - 1]; return getItem(collection, lastItemId); } function getItemIndex(collection, itemId) { return collection.ids.indexOf(itemId); } function getItems(collection) { const list = []; for (const id of collection.ids) if (collection.map[id]) list.push(collection.map[id]); return list; } function findItem(collection, findFunction) { const foundId = collection.ids.find((id) => { const collectionItem = getItem(collection, id); if (!collectionItem) return false; return findFunction(collectionItem); }); if (!foundId) return; return getItem(collection, foundId); } function findLastItem(collection, findFunction) { const foundId = [...collection.ids].reverse().find((id) => { const collectionItem = getItem(collection, id); if (!collectionItem) return false; return findFunction(collectionItem); }); if (!foundId) return; return getItem(collection, foundId); } function filterItems(collection, filterFunction) { return collection.ids.map((id) => { return collection.map[id]; }).filter((item) => item !== void 0).filter(filterFunction); } function getRefsCount(collection, itemId) { if (!collection.refsCount[itemId]) return collection.map[itemId] ? 1 : 0; return collection.refsCount[itemId]; } //#endregion export { addItem, addItemAndCountRef, addItemAt, addItems, collectionFieldsSet, createCollection, decreaseRefsCount, deduplicateCollection, filterCollection, filterItems, findItem, findLastItem, getItem, getItemIndex, getItems, getLastItem, getRefsCount, increaseRefsCount, isCollection, purgeCollection, removeItem, replaceItem, replaceItems, updateItem, upsertItem, upsertItemAndCountRef }; //# sourceMappingURL=collection-utils.js.map