Created
October 27, 2022 09:09
-
-
Save NicmeisteR/ca3d7a81b5a5e6574cc83aedd6e7123b to your computer and use it in GitHub Desktop.
All Keys in Object to CameCase
This file contains 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
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