UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 1.16 kB
{"version":3,"file":"mapKeys-C059LFk2.cjs","names":["objectFromEntries","objectEntries"],"sources":["../src/functions/mapKeys/mapKeys.ts"],"sourcesContent":["import type { CastToString } from '../../types';\nimport { objectEntries } from '../objectEntries';\nimport { objectFromEntries } from '../objectFromEntries';\n\n/**\n * Returns a new object with the same values as the given object, but with each key mapped to a new key as returned by the given mapper function.\n * @param object The object to map the keys of.\n * @param mapperFunction The function to map the keys with.\n * @returns A new object with the mapped keys.\n */\nexport function mapKeys<T extends object, K extends PropertyKey>(\n object: T,\n mapperFunction: (\n key: CastToString<keyof T>,\n value: T[keyof T],\n object: T\n ) => K\n): Record<K, T[keyof T]> {\n return objectFromEntries(\n objectEntries(object).map(([key, value]) => [\n mapperFunction(key, value, object),\n value,\n ])\n );\n}\n"],"mappings":";;;;;;;;;;AAUA,SAAgB,QACd,QACA,gBAKuB;AACvB,QAAOA,4CACLC,oCAAc,OAAO,CAAC,KAAK,CAAC,KAAK,WAAW,CAC1C,eAAe,KAAK,OAAO,OAAO,EAClC,MACD,CAAC,CACH"}