import { Func } from '@rhom6us/func'; import { fromEntries } from '@rhom6us/obj'; import { Cast } from './cast'; import { Inc } from './counter'; import { DeepDictionary, DeepDictionaryItem } from './deep-record'; type _flattenMap, prefix extends string = '', CurrentDepth extends number = 0> = CurrentDepth extends 10 ? never : T extends DeepDictionary ? { [K in keyof T]: _flattenMap}`, Inc> }[keyof T] : [prefix, T] export type flattenMap> = fromEntries<_flattenMap>; export function flattenMap>(map: T): flattenMap { const result: any = {}; const stack = Object.entries(map); while (stack.length) { const [prefix, mapOrFun] = stack.pop()!; if (typeof mapOrFun === 'function') { result[prefix] = mapOrFun; } else { for (const [key, p] of Object.entries(mapOrFun)) { stack.push([join(prefix, key), p]); } } } return result; } function join(...args: string[]): string { return args.filter(Boolean).join("."); }