UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

46 lines (45 loc) 1.16 kB
import { mapObject } from './map-entries.js'; /** * Maps all values of an enum as keys in an object where each value is the callback's output for * that key. * * @category Object * @category Package : @augment-vir/common * @example * * ```ts * import {mapEnumToObject} from '@augment-vir/common'; * * enum MyEnum { * A = 'a', * B = 'b', * } * * mapEnumToObject(MyEnum, (enumValue) => { * return `value-${enumValue}`; * }); * // output is `{[MyEnum.A]: 'value-a', [MyEnum.B]: 'value-b'}` * ``` * * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function mapEnumToObject(enumInput, callback) { return mapObject(enumInput, (enumKey, enumValue) => { const key = enumValue; const value = callback(enumValue, enumInput); if (value instanceof Promise) { return value.then((resolvedValue) => { return { key, value: resolvedValue, }; }); } else { return { key, value, }; } }); }