UNPKG

case-converters

Version:

A modern TypeScript utility to convert strings and object keys between various case styles like camelCase, snake_case, PascalCase, kebab-case, spongeCase, and more.

29 lines (28 loc) 1.1 kB
"use strict"; 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") { return caseConverter(input); } if (Array.isArray(input)) { return input.map((item) => convertKeysToCase(item, caseType)); } if (input !== null && typeof input === "object") { return Object.entries(input).reduce((acc, [key, value]) => { const changedKey = caseConverter(key); // Only recursively convert if value is object or array if (typeof value === "object" && value !== null) { acc[changedKey] = convertKeysToCase(value, caseType); } else { acc[changedKey] = value; } return acc; }, {}); } // If input is primitive (number, boolean, null, undefined, symbol, etc.) return input; }