diff --git a/contract/src/objectTools.js b/contract/src/objectTools.js new file mode 100644 index 00000000..b3f83ff5 --- /dev/null +++ b/contract/src/objectTools.js @@ -0,0 +1,22 @@ +// @ts-check +const { entries, fromEntries } = Object; + +/** @type { >>(obj: T) => Promise<{ [K in keyof T]: Awaited}> } */ +export const allValues = async obj => { + const es = await Promise.all( + entries(obj).map(async ([k, v]) => [k, await v]), + ); + return fromEntries(es); +}; + +/** @type { >(obj: T, f: (v: V) => U) => { [K in keyof T]: U }} */ +export const mapValues = (obj, f) => + fromEntries( + entries(obj).map(([p, v]) => { + const entry = [p, f(v)]; + return entry; + }), + ); + +/** @type {(xs: X[], ys: Y[]) => [X, Y][]} */ +export const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);