From 104dc579e43189a0476fb98eeec8ff524e1a0dee Mon Sep 17 00:00:00 2001 From: Anton Kosykh Date: Fri, 7 Jul 2023 19:39:12 +0300 Subject: [PATCH] feat: `grabItems` method --- packages/core/src/grabItems.ts | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 packages/core/src/grabItems.ts diff --git a/packages/core/src/grabItems.ts b/packages/core/src/grabItems.ts new file mode 100644 index 0000000..170270e --- /dev/null +++ b/packages/core/src/grabItems.ts @@ -0,0 +1,65 @@ +import { NoInfer, combine } from "effector"; +import { Compute } from "ts-toolbelt/out/Any/Compute"; +import { createMap } from "./createMap"; +import { ListApi, Mapping, PossibleKey, Selection } from "./types"; + +type ValueByType = { + [P in U]: { type: P; value: T[P] } +}[U] + +export const grabItems = ( + kv: ListApi | Selection | Mapping, + config: { + source: { + [ShapeKey in keyof Shape]: + | ListApi + | Selection + | Mapping + } + filter: NoInfer<{ + [ShapeKey in keyof Shape]: (entry: Shape[ShapeKey], item: Item) => boolean + }> + orderBy?: + | { + key: keyof Shape[keyof Shape] + type: 'asc' | 'desc' + } + | ((item: Item) => { + key: keyof Shape[keyof Shape] + type: 'asc' | 'desc' + }) + }, +) => { + return createMap(kv, { + source: combine( + Object.fromEntries(Object.entries(config.source).map(([key, kv]) => [key, kv.state.store])), + ), + fn: (item, source) => { + const children = [] as Compute[], 'flat'> + for (const shapeKey in source) { + children.push( + // @ts-expect-error ... + ...Object.values(source[shapeKey].ref) + // @ts-expect-error ... + .filter((shapeItem) => config.filter[shapeKey](shapeItem, item)) + .map((value) => ({ type: shapeKey, value })), + ) + } + const orderBy = !config.orderBy + ? null + : typeof config.orderBy === 'function' + ? config.orderBy(item) + : config.orderBy + return { + item, + children: orderBy + ? children.sort((a, b) => { + return orderBy.type === 'asc' + ? +a.value[orderBy.key] - +b.value[orderBy.key] + : +b.value[orderBy.key] - +a.value[orderBy.key] + }) + : children, + } + }, + }) +}