Skip to content

Instantly share code, notes, and snippets.

@NicmeisteR
Created October 27, 2022 09:09
Show Gist options
  • Save NicmeisteR/ca3d7a81b5a5e6574cc83aedd6e7123b to your computer and use it in GitHub Desktop.
Save NicmeisteR/ca3d7a81b5a5e6574cc83aedd6e7123b to your computer and use it in GitHub Desktop.
All Keys in Object to CameCase
public lowerCaseObjectKeys<T>(object: any) {
// Helper function for detection objects
const isObject = (obj) =>
Object.prototype.toString.call(obj) === '[object Object]';
// The entry point for recursion, iterates and maps object properties
const lowerCaseObjectKeys = (obj) =>
Object.fromEntries(Object.entries(obj).map(objectKeyMapper));
// Converts keys to lowercase, detects object values
// and sends them off for further conversion
const objectKeyMapper = ([key, val]) => [
key.charAt(0).toLowerCase() + key.slice(1),
isObject(val) ? lowerCaseObjectKeys(val) : val,
];
return lowerCaseObjectKeys(object) as T;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment