Last active
July 2, 2026 23:38
-
-
Save fauxsaurus/1d071af186044aa001ee4c844b885cbe to your computer and use it in GitHub Desktop.
Object Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @note the object equivalent of `array.map()` (uses `FN` util type from `fauxsaurus/FN.ts`) | |
| export const objectMap = <O extends Record<string, unknown>, R>( | |
| object: O, | |
| map: FN<[O[keyof O], keyof O, O], R> | |
| ): Record<string, R> => { | |
| const entries = Object.entries(object) as [keyof O, O[keyof O]][] | |
| const entriesMapped = entries.map<[keyof O, R]>(([key, value]) => [ | |
| key, | |
| map(value, key, object), | |
| ]) | |
| return Object.fromEntries(entriesMapped) | |
| } | |
| const renameObjectKey = <T extends Record<string, any>>( | |
| oldKey: string, | |
| newKey: string, | |
| {[oldKey]: value, ...obj}: T, | |
| ) => Object.assign({[newKey]: value}, obj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment