typedash
Version:
modern, type-safe collection of utility functions
1 lines • 1.35 kB
Source Map (JSON)
{"version":3,"file":"mapValues-xS7C28iZ.cjs","names":["objectFromEntries","objectEntries"],"sources":["../src/functions/mapValues/mapValues.ts"],"sourcesContent":["import type { CastToString, KeysOfUnion } from '../../types';\nimport { objectEntries } from '../objectEntries';\nimport { objectFromEntries } from '../objectFromEntries';\n\n/**\n * Returns a new object with the same keys as the given object, but with each value mapped to a new value as returned by the given mapper function.\n * @param object The object to map.\n * @param mapperFunction The function used to map each value.\n * @returns A new object with the same keys as the given object, but with each value mapped to a new value as returned by the given mapper function.\n * @example\n * ```ts\n * mapValues({ a: 1, b: 2, c: 3 }, (value) => value * 2) // { a: 2, b: 4, c: 6 }\n * ```\n */\nexport function mapValues<T extends object, V>(\n object: T,\n mapperFunction: (value: T[keyof T], key: CastToString<KeysOfUnion<T>>) => V\n): Record<keyof T, V> {\n return objectFromEntries(\n objectEntries(object).map(([key, value]) => [\n key,\n mapperFunction(value, key),\n ])\n ) as Record<keyof T, V>;\n}\n"],"mappings":";;;;;;;;;;;;;;AAcA,SAAgB,UACd,QACA,gBACoB;AACpB,QAAOA,4CACLC,oCAAc,OAAO,CAAC,KAAK,CAAC,KAAK,WAAW,CAC1C,KACA,eAAe,OAAO,IAAI,CAC3B,CAAC,CACH"}