snakify-ts
Version:
Recursive snake casing of object property names with proper typing
33 lines • 909 B
JavaScript
import snakeCase from "lodash.snakecase";
function walk(obj, shallow = false) {
if (!obj || typeof obj !== "object")
return obj;
if (obj instanceof Date || obj instanceof RegExp)
return obj;
if (Array.isArray(obj))
return obj.map(v => {
if (!shallow) {
return walk(v);
}
if (typeof v === 'object')
return walk(v, shallow);
return v;
});
return Object.keys(obj).reduce((res, key) => {
const camel = snakeCase(key);
res[camel] = shallow ? obj[key] : walk(obj[key]);
return res;
}, {});
}
export default function snakify(
/**
* Value to be snakified
*/
obj,
/**
* If true, only the top level keys of the obj will be camel cased
*/
shallow) {
return typeof obj === "string" ? snakeCase(obj) : walk(obj, shallow);
}
//# sourceMappingURL=index.js.map