case-converters
Version:
Change case functions for all cases in TypeScript and JavaScript. Combined version of all [`case-converters`](https://github.com/cvchauhan/case-converter) methods, so you do not need to install them separately. ESM and CJS bundles are included, also bac
27 lines (26 loc) • 1.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertKeysToCase = convertKeysToCase;
const getcaseconverter_1 = require("./getcaseconverter");
function convertKeysToCase(input, caseType) {
const caseConverter = (0, getcaseconverter_1.getCaseConverter)(caseType);
if (typeof input === "string") {
// Recursively process each element if it's an array
return caseConverter(input);
}
else if (Array.isArray(input)) {
// Recursively process each element if it's an array
return input.map((item) => convertKeysToCase(item, caseType));
}
else if (input !== null && typeof input === "object") {
// Recursively process each key-value pair if it's an object
return Object.keys(input).reduce((acc, key) => {
const changeKey = caseConverter(key);
acc[changeKey] = input[key]; // Do not modify the value, only change the key
if (typeof input[key] === "object" && input[key] !== null) {
acc[changeKey] = convertKeysToCase(input[key], caseType); // Recursively handle nested objects/arrays
}
return acc;
}, {});
}
}
;