import {drop, identity, includedIn, is, isArray, isNot, on, take} from 'tsfun'; import {findInTree, ITEMNAMEPATH, TreeList} from './tree-list'; import {Named, onName} from './named'; import {Name} from '../constants'; export function findInNamedTreeList(match: Name, t: TreeList): N|undefined { const result = findInTree(t, onName( is(match))); return result ? result.item : undefined; } export function filterTrees(t: TreeList, match: Name, ...moreMatches: Name[]): TreeList; export function filterTrees(match: Name, ...moreMatches: Name[]): (t: TreeList) => TreeList; export function filterTrees(a: any, ...bs: any[]): any { return _filterTrees(false, a, bs); } export function removeTrees(t: TreeList, match: Name, ...moreMatches: Name[]): TreeList; export function removeTrees(match: Name, ...moreMatches: Name[]): (t: TreeList) => TreeList; export function removeTrees(a: any, ...bs: any[]): any { return _filterTrees(true, a, bs); } export function isTopLevelItemOrChildThereof(t: TreeList, match: Name, firstLevelItem: Name, ...moreFirstLevelItems: Name[]): boolean { const filtered = filterTrees(t, firstLevelItem, ...moreFirstLevelItems); return findInNamedTreeList(match, filtered) !== undefined; } function _filterTrees(invert: boolean, a: any, bs: any[]): TreeList { const $ = (t: any, match: any, moreMatches: any) => t.filter( on(ITEMNAMEPATH, ((invert ? isNot : identity)(includedIn([match].concat(moreMatches)))) )); return isArray(a) ? $(a, take(1, bs)[0], drop(1, bs)) : (t: any) => $(t, a, bs); }