/
spectree
/
mstroy
Обзор
Документация
Войти
/
spectree
/
mstroy
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/services/TreeStore.ts
117 строк
4 KB
Alastrr
feature: реализация тестового задания согласно тз
16 апр 2026, 23:22
16 апр 2026, 23:22
55ff14c
Код
Авторство
О чём код?
import type { GridItemInterface, IdItem } from '@/types/index' export class TreeStore { private gridItems: GridItemInterface[] private gridItemsMap: Map<number | string, GridItemInterface> private childrenMap: Map<number | string, GridItemInterface[]> constructor(gridItems: GridItemInterface[]) { this.gridItems = gridItems this.gridItemsMap = new Map( gridItems.map((el) => [el.id as number | string, el]) ) this.childrenMap = new Map() gridItems.forEach((el) => { if (el.parent === null) return const parent = el.parent as number | string const children = this.childrenMap.get(parent) ?? [] children.push(el) this.childrenMap.set(parent, children) }) } getAll(): GridItemInterface[] { return this.gridItems } getItem(id: IdItem): GridItemInterface | undefined { if (id === null) return undefined return this.gridItemsMap.get(id) } getChildren(id: IdItem): GridItemInterface[] { if (id === null) return [] return this.childrenMap.get(id as number | string) ?? [] } getAllChildren(id: IdItem): GridItemInterface[] { if (id === null) return [] const result: GridItemInterface[] = [] const traverse = (currentId: number | string) => { const children = this.childrenMap.get(currentId) ?? [] children.forEach((child) => { result.push(child) traverse(child.id as number | string) }) } traverse(id as number | string) return result } getAllParents(id: IdItem): GridItemInterface[] { if (id === null) return [] const result: GridItemInterface[] = [] let current = this.gridItemsMap.get(id as number | string) while (current) { result.push(current) if (current.parent === null) break current = this.gridItemsMap.get(current.parent as number | string) } return result } addItem(item: GridItemInterface): void { this.gridItems.push(item) this.gridItemsMap.set(item.id as number | string, item) if (item.parent !== null) { const children = this.childrenMap.get(item.parent as number | string) ?? [] children.push(item) this.childrenMap.set(item.parent as number | string, children) } } removeItem(id: IdItem): void { if (id === null) return const toRemove = new Set<number | string>([ id as number | string, ...this.getAllChildren(id).map((el) => el.id as number | string), ]) const item = this.gridItemsMap.get(id as number | string) const toKeep = this.gridItems.filter( (el) => !toRemove.has(el.id as number | string) ) this.gridItems.length = 0 this.gridItems.push(...toKeep) toRemove.forEach((removeId) => { this.gridItemsMap.delete(removeId) this.childrenMap.delete(removeId) }) if (item && item.parent !== null) { const siblings = this.childrenMap.get(item.parent as number | string) ?? [] this.childrenMap.set( item.parent as number | string, siblings.filter((el) => el.id !== id) ) } } updateItem(item: GridItemInterface): void { const existing = this.gridItemsMap.get(item.id as number | string) if (!existing) return Object.assign(existing, item) } }