Created
September 11, 2022 18:54
-
-
Save arimariojesus/4c8fd25142b6828b985409204c89801c to your computer and use it in GitHub Desktop.
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
export const isObject = (val: unknown): boolean => | |
val instanceof Object && !Array.isArray(val); | |
export const propertiesToArray = (obj: object): string[] => { | |
const addDelimiter = (a: string, b: string) => (a ? `${a}.${b}` : b); | |
const paths = (obj: Record<string, any> = {}, head = ''): string[] => { | |
return Object.entries(obj).reduce<string[]>((product, [key, value]) => { | |
const fullPath = addDelimiter(head, key); | |
return isObject(value) | |
? product.concat(paths(value, fullPath)) | |
: product.concat(fullPath); | |
}, []); | |
}; | |
return paths(obj); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment