Skip to content

Commit

Permalink
chore: objectTools 4c4c0d1
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Feb 21, 2024
1 parent f87e878 commit 49f71fa
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions contract/src/objectTools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @ts-check
const { entries, fromEntries } = Object;

/** @type { <T extends Record<string, ERef<any>>>(obj: T) => Promise<{ [K in keyof T]: Awaited<T[K]>}> } */
export const allValues = async obj => {
const es = await Promise.all(
entries(obj).map(async ([k, v]) => [k, await v]),
);
return fromEntries(es);
};

/** @type { <V, U, T extends Record<string, V>>(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 {<X, Y>(xs: X[], ys: Y[]) => [X, Y][]} */
export const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);

0 comments on commit 49f71fa

Please sign in to comment.