UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

33 lines (32 loc) 1.37 kB
const require_limitAsync = require("../promise/limitAsync.js"); //#region src/object/mapKeysAsync.ts /** * Creates a new object with the same values as the given object, but with keys generated * by running each own enumerable property of the object through the async iteratee function. * * @template T - The type of the object. * @template K - The type of the new keys generated by the iteratee function. * * @param {T} object - The object to iterate over. * @param {(value: T[keyof T], key: keyof T, object: T) => Promise<K>} getNewKey - The async function invoked per own enumerable property. * @param {MapKeysAsyncOptions} [options] Optional configuration object. * @returns {Promise<Record<K, T[keyof T]>>} - A promise that resolves to the new mapped object. * * @example * // Example usage: * const obj = { a: 1, b: 2 }; * const result = await mapKeysAsync(obj, async (value, key) => key + value); * console.log(result); // { a1: 1, b2: 2 } */ async function mapKeysAsync(object, getNewKey, options) { const result = {}; const keys = Object.keys(object); if (options?.concurrency) getNewKey = require_limitAsync.limitAsync(getNewKey, options.concurrency); await Promise.all(keys.map(async (key) => { const value = object[key]; result[await getNewKey(value, key, object)] = value; })); return result; } //#endregion exports.mapKeysAsync = mapKeysAsync;