39 lines
1003 B
JavaScript
39 lines
1003 B
JavaScript
import camelCase from 'lodash.camelcase';
|
|
import snakeCase from 'lodash.snakecase';
|
|
|
|
export function modifyObjectKeys(object, modify) {
|
|
// If the passed in object is not an object, return it.
|
|
if (
|
|
object === undefined ||
|
|
object === null ||
|
|
(typeof object !== 'object' && !Array.isArray(object))
|
|
) {
|
|
return object;
|
|
}
|
|
|
|
if (Array.isArray(object)) {
|
|
return object.map(value => modifyObjectKeys(value, modify));
|
|
}
|
|
|
|
// Otherwise, process all its keys.
|
|
const result = {};
|
|
Object.entries(object).forEach(([key, value]) => {
|
|
result[modify(key)] = modifyObjectKeys(value, modify);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
export function camelCaseObject(object) {
|
|
return modifyObjectKeys(object, camelCase);
|
|
}
|
|
|
|
export function snakeCaseObject(object) {
|
|
return modifyObjectKeys(object, snakeCase);
|
|
}
|
|
|
|
export function convertKeyNames(object, nameMap) {
|
|
const transformer = key => (nameMap[key] === undefined ? key : nameMap[key]);
|
|
|
|
return modifyObjectKeys(object, transformer);
|
|
}
|