Skip to content

Instantly share code, notes, and snippets.

@arimariojesus
Created September 11, 2022 18:54
Show Gist options
  • Save arimariojesus/4c8fd25142b6828b985409204c89801c to your computer and use it in GitHub Desktop.
Save arimariojesus/4c8fd25142b6828b985409204c89801c to your computer and use it in GitHub Desktop.
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