import type {Id} from '../@types/common/index.d.ts'; import {arrayPush} from './array.ts'; import { collDel, collEvery, collForEach, collHas, collIsEmpty, collSize, } from './coll.ts'; import {IdObj, objForEach, objHas, objIsEmpty, objNew, objSet} from './obj.ts'; import {ifNotUndefined, isInstanceOf, isUndefined, size} from './other.ts'; export type IdMap = Map; export type IdMap2 = IdMap>; export type IdMap3 = IdMap>; const map = Map; const isMap = (value: unknown): value is Map => isInstanceOf(value, map); export const mapNew = (entries?: [Key, Value][]): Map => new map(entries); export const weakMapNew = (): WeakMap => new WeakMap(); export const mapKeys = (map: Map | undefined): Key[] => [ ...(map?.keys() ?? []), ]; export const mapGet = ( map: Map | undefined, key: Key, ): Value | undefined => map?.get(key); export const mapForEach = ( map: Map | undefined, cb: (key: Key, value: Value) => void, ): void => collForEach(map, (value, key) => cb(key, value)); export const mapMap = ( coll: Map | undefined, cb: (value: Value, key: Key) => Return, ): Return[] => { const mapped: Return[] = []; mapForEach(coll, (key, value) => arrayPush(mapped, cb(value, key))); return mapped; }; export const mapSet = ( map: Map | undefined, key: Key, value?: Value, ): Map | undefined => isUndefined(value) ? (collDel(map, key), map) : map?.set(key, value); export const mapEquals = ( map1: Map, map2: Map, ): boolean => collSize(map1) === collSize(map2) && collEvery(map1, (value1, key) => { const value2 = mapGet(map2, key); return isMap(value1) && isMap(value2) ? mapEquals(value1, value2) : value1 === value2; }); export const mapEnsure = ( map: Map, key: Key, getDefaultValue: () => Value, hadExistingValue?: (value: Value) => void, ): Value => { let value = mapGet(map, key) as Value; if (collHas(map, key)) { hadExistingValue?.(value); } else { mapSet(map, key, (value = getDefaultValue())); } return value; }; export const mapMatch = ( map: IdMap, obj: IdObj, set: (map: IdMap, id: Id, value: ObjValue) => void, del: (map: IdMap, id: Id) => void = mapSet, ): IdMap => { objForEach(obj, (value, id) => set(map, id, value)); mapForEach(map, (id) => (objHas(obj, id) ? 0 : del(map, id))); return map; }; export const mapToObj = ( map: IdMap | undefined, valueMapper?: (mapValue: MapValue, id: Id) => ObjValue, excludeMapValue?: (mapValue: MapValue, id: Id) => boolean, excludeObjValue?: (objValue: ObjValue) => boolean, ): IdObj => { const obj = objNew(); collForEach(map, (mapValue, id) => { if (!excludeMapValue?.(mapValue, id)) { const objValue = valueMapper ? valueMapper(mapValue, id) : (mapValue as any as ObjValue); if (!excludeObjValue?.(objValue)) { objSet(obj, id, objValue); } } }); return obj; }; export const mapToObj2 = ( map: IdMap2 | undefined, valueMapper?: (mapValue: MapValue, id: Id) => ObjValue, excludeMapValue?: (mapValue: MapValue) => boolean, ) => mapToObj( map, (childMap) => mapToObj(childMap, valueMapper, excludeMapValue), collIsEmpty, objIsEmpty, ); export const mapToObj3 = ( map: IdMap3, valueMapper?: (mapValue: MapValue, id: Id) => ObjValue, excludeMapValue?: (mapValue: MapValue) => boolean, ) => mapToObj( map, (childMap) => mapToObj2(childMap, valueMapper, excludeMapValue), collIsEmpty, objIsEmpty, ); export const mapClone = ( map: IdMap | undefined, mapValue?: (mapValue: MapValue) => MapValue, ): IdMap => { const map2: IdMap = mapNew(); collForEach(map, (value, key) => map2.set(key, mapValue?.(value) ?? value)); return map2; }; export const mapClone2 = (map: IdMap2 | undefined) => mapClone(map, mapClone); export const mapClone3 = (map: IdMap3 | undefined) => mapClone(map, mapClone2); export type Node = Map | Leaf>; export const visitTree = ( node: Node, path: Path[], ensureLeaf?: () => Leaf, pruneLeaf?: (leaf: Leaf) => 1 | 0 | void, p = 0, ): Leaf | undefined => ifNotUndefined( (ensureLeaf ? mapEnsure : mapGet)( node, path[p], p > size(path) - 2 ? (ensureLeaf as () => Leaf) : mapNew, ), (nodeOrLeaf) => { if (p > size(path) - 2) { if (pruneLeaf?.(nodeOrLeaf as Leaf)) { mapSet(node, path[p]); } return nodeOrLeaf as Leaf; } const leaf = visitTree( nodeOrLeaf as Node, path, ensureLeaf, pruneLeaf, p + 1, ) as Leaf; if (collIsEmpty(nodeOrLeaf as Node)) { mapSet(node, path[p]); } return leaf; }, );