import { IItem } from './types'; import { Optional } from '@/types'; import _ from 'lodash'; function getId(data: Optional, 'id'>): string { return data.id ?? data.label; } function isChild(parentId: string, childId: string): boolean { const regExpString = `^${parentId}.`.replaceAll('.', '\\.'); const regExp = new RegExp(regExpString, 'g'); return childId.match(regExp) !== null; } function getItemsIds(items: Array): Array { return _.map(items, 'id'); } function getLevel(id: string): number { const match = id.match(new RegExp('[^.]+', 'g')); return match === null ? 0 : match.length; } function getChildren(id: string | undefined, items: Array) { const level = id !== undefined ? getLevel(id) : 0; const children = _.chain(items) .filter((item) => (id !== undefined ? isChild(id, item.id) : true)) .filter((item) => getLevel(item.id) === level + 1) .value(); return children; } export { getChildren, getId, getItemsIds, getLevel, isChild };