kitcn
Version:
kitcn - React Query integration and CLI tools for Convex
84 lines (83 loc) • 2.6 kB
JavaScript
//#region src/internal/upstream/index.ts
/** biome-ignore-all lint: vendored upstream helper source */
/**
* Vendored from upstream helper repository at commit c5e52c8.
* Source path: packages/convex_helpers/index.ts
*/
/**
* asyncMap returns the results of applying an async function over an list.
*
* The list can even be a promise, or an iterable like a Set.
* @param list - Iterable object of items, e.g. an Array, Set, Object.keys
* @param asyncTransform
* @returns
*/
async function asyncMap(list, asyncTransform) {
const promises = [];
let index = 0;
list = await list;
for (const item of list) {
promises.push(asyncTransform(item, index));
index += 1;
}
return Promise.all(promises);
}
/**
* pick helps you pick keys from an object more concisely.
*
* e.g. `pick({a: v.string(), b: v.number()}, ["a"])` is equivalent to
* `{a: v.string()}`
* The alternative could be something like:
* ```js
* const obj = { a: v.string(), b: v.number() };
* // pick does the following
* const { a } = obj;
* const onlyA = { a };
* ```
*
* @param obj The object to pick from. Often like { a: v.string() }
* @param keys The keys to pick from the object.
* @returns A new object with only the keys you picked and their values.
*/
function pick(obj, keys) {
return Object.fromEntries(Object.entries(obj).filter(([k]) => keys.includes(k)));
}
/**
* omit helps you omit keys from an object more concisely.
*
* e.g. `omit({a: v.string(), b: v.number()}, ["a"])` is equivalent to
* `{b: v.number()}`
*
* The alternative could be something like:
* ```js
* const obj = { a: v.string(), b: v.number() };
* // omit does the following
* const { a, ...rest } = obj;
* const withoutA = rest;
* ```
*
* @param obj The object to return a copy of without the specified keys.
* @param keys The keys to omit from the object.
* @returns A new object with the keys you omitted removed.
*/
function omit(obj, keys) {
return Object.fromEntries(Object.entries(obj).filter(([k]) => !keys.includes(k)));
}
/**
* A utility to validate truthiness at runtime, providing a type guard
*
* @example
* ```ts
* const x: string | null = getValue();
* assert(x);
* // x is now of type string
* ```
* You can also provide a function, to avoid doing expensive string templating.
* @param arg A value to assert the truthiness of.
* @param message An optional message to throw if the value is not truthy, or a function to generate the message.
*/
function assert(value, message) {
if (!value) throw new Error(typeof message === "function" ? message() : message);
}
//#endregion
export { pick as i, asyncMap as n, omit as r, assert as t };