( map: MapObject<T>, resolver: (val: T, key: string, values: MapObject<T>) => ValueOrPromise<V>, )
| 93 | * be invoked with the property value, the property name, and the source object. |
| 94 | */ |
| 95 | export function resolveMap<T, V>( |
| 96 | map: MapObject<T>, |
| 97 | resolver: (val: T, key: string, values: MapObject<T>) => ValueOrPromise<V>, |
| 98 | ): ValueOrPromise<MapObject<V>> { |
| 99 | const result: MapObject<V> = {}; |
| 100 | let asyncResolvers: PromiseLike<void>[] | undefined = undefined; |
| 101 | |
| 102 | const setter = (key: string) => (val: V) => { |
| 103 | if (val !== undefined) { |
| 104 | // Only set the value if it's not undefined so that the default value |
| 105 | // for a key will be honored |
| 106 | result[key] = val; |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | for (const key in map) { |
| 111 | const valueOrPromise = resolver(map[key], key, map); |
| 112 | if (isPromiseLike(valueOrPromise)) { |
| 113 | if (!asyncResolvers) asyncResolvers = []; |
| 114 | asyncResolvers.push(valueOrPromise.then(setter(key))); |
| 115 | } else { |
| 116 | if (valueOrPromise !== undefined) { |
| 117 | // Only set the value if it's not undefined so that the default value |
| 118 | // for a key will be honored |
| 119 | result[key] = valueOrPromise; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (asyncResolvers) { |
| 125 | return Promise.all(asyncResolvers).then(() => result); |
| 126 | } else { |
| 127 | return result; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Resolve entries of an array into a new array with the same indexes. If one or |
no test coverage detected