-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapKeys.ts
31 lines (28 loc) · 903 Bytes
/
mapKeys.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Map the keys of an object using a mapping function
* @param obj the object to map the keys of
* @param mapFn the mapping function
* @returns `obj` with its keys mapped using `mapFn`
*
* @example
* const capitalNames = mapKeys({ john: 10, alice: 20 }, k => k.toUpperCase())
* capitalNames === { JOHN: 10, ALICE: 20 }
*/
export const mapKeys = <
TMapped extends PropertyKey,
TObj extends Record<PropertyKey, any>
>(
obj: TObj,
mapFn: (t: keyof TObj) => TMapped
): { [k in keyof TObj as TMapped]: TObj[k] } => {
const entries = Object.entries(obj).map(([k, v]) => [mapFn(k), v])
return Object.fromEntries(entries)
}
// Tests
if (import.meta.vitest) {
const { expect, it } = import.meta.vitest
it('works for example 1', () => {
const capitalNames = mapKeys({ john: 10, alice: 20 }, k => k.toUpperCase())
expect(capitalNames).toEqual({ JOHN: 10, ALICE: 20 })
})
}