snakify-ts
Version:
Recursive snake casing of object property names with proper typing
26 lines • 1 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_snakecase_1 = __importDefault(require("lodash.snakecase"));
const lodash_isdate_1 = __importDefault(require("lodash.isdate"));
const lodash_isregexp_1 = __importDefault(require("lodash.isregexp"));
function walk(obj) {
if (!obj || typeof obj !== "object")
return obj;
if (lodash_isdate_1.default(obj) || lodash_isregexp_1.default(obj))
return obj;
if (Array.isArray(obj))
return obj.map(walk);
return Object.keys(obj).reduce((res, key) => {
const camel = lodash_snakecase_1.default(key);
res[camel] = walk(obj[key]);
return res;
}, {});
}
function snakify(obj) {
return typeof obj === "string" ? lodash_snakecase_1.default(obj) : walk(obj);
}
exports.default = snakify;
//# sourceMappingURL=index.js.map