Skip to content

Instantly share code, notes, and snippets.

@fauxsaurus
Last active July 2, 2026 23:38
Show Gist options
  • Select an option

  • Save fauxsaurus/1d071af186044aa001ee4c844b885cbe to your computer and use it in GitHub Desktop.

Select an option

Save fauxsaurus/1d071af186044aa001ee4c844b885cbe to your computer and use it in GitHub Desktop.
Object Utils
// @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