({
checkEquality = false,
useWeakMap = false,
}: Options = {})
| 32 | * }); // 42 |
| 33 | */ |
| 34 | export function createDeepMap<V>({ |
| 35 | checkEquality = false, |
| 36 | useWeakMap = false, |
| 37 | }: Options = {}) { |
| 38 | const MapToUse = useWeakMap ? WeakMap : Map; |
| 39 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 40 | const root = new MapToUse<any, unknown>(); |
| 41 | if (checkEquality) { |
| 42 | root.set(equalReuserSymbol, createValueReuser()); |
| 43 | } |
| 44 | |
| 45 | function getFinalTargetMap(path: unknown[]) { |
| 46 | let currentTarget = root; |
| 47 | |
| 48 | for (let part of path) { |
| 49 | if (part === undefined) part = undefinedSymbol; |
| 50 | if (checkEquality) { |
| 51 | const currentReuser = currentTarget.get( |
| 52 | equalReuserSymbol |
| 53 | ) as EqualValueReuser; |
| 54 | part = currentReuser(part); |
| 55 | } |
| 56 | |
| 57 | if (currentTarget.has(part)) { |
| 58 | currentTarget = currentTarget.get(part) as AnyMap; |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | const nestedMap = new MapToUse(); |
| 63 | |
| 64 | if (checkEquality) { |
| 65 | nestedMap.set(equalReuserSymbol, createValueReuser()); |
| 66 | } |
| 67 | |
| 68 | currentTarget.set(part, nestedMap); |
| 69 | |
| 70 | currentTarget = nestedMap; |
| 71 | } |
| 72 | |
| 73 | return currentTarget; |
| 74 | } |
| 75 | |
| 76 | function has(path: unknown[]) { |
| 77 | const targetMap = getFinalTargetMap(path); |
| 78 | |
| 79 | return targetMap.has(targetSymbol); |
| 80 | } |
| 81 | |
| 82 | function set(path: unknown[], value: V) { |
| 83 | const targetMap = getFinalTargetMap(path); |
| 84 | |
| 85 | targetMap.set(targetSymbol, value); |
| 86 | } |
| 87 | |
| 88 | function get(path: unknown[]) { |
| 89 | const targetMap = getFinalTargetMap(path); |
| 90 | |
| 91 | return targetMap.get(targetSymbol) as V | undefined; |
no test coverage detected