MCPcopy Index your code
hub / github.com/loopbackio/loopback-next / resolveMap

Function resolveMap

packages/context/src/value-promise.ts:95–129  ·  view source on GitHub ↗
(
  map: MapObject<T>,
  resolver: (val: T, key: string, values: MapObject<T>) => ValueOrPromise<V>,
)

Source from the content-addressed store, hash-verified

93 * be invoked with the property value, the property name, and the source object.
94 */
95export 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

Callers 3

greetFromAllFunction · 0.90

Calls 3

isPromiseLikeFunction · 0.85
setterFunction · 0.70
resolverFunction · 0.50

Tested by

no test coverage detected