@itsmworkbench/utils
Version:
The usual utility functions
23 lines (22 loc) • 1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformKeysToCamelCase = exports.transformKeys = exports.isJsonPrimitive = void 0;
const strings_1 = require("./strings");
function isJsonPrimitive(x) {
return typeof x === 'string' || typeof x === 'number' || typeof x === 'boolean' || x === null;
}
exports.isJsonPrimitive = isJsonPrimitive;
const transformKeys = (fn) => (jsonValue) => {
if (jsonValue === null || jsonValue === undefined || isJsonPrimitive(jsonValue))
return jsonValue;
if (Array.isArray(jsonValue))
return jsonValue.map((0, exports.transformKeys)(fn));
const transformedObject = {};
Object.entries(jsonValue).forEach(([key, value]) => {
const newKey = fn(key);
transformedObject[newKey] = (0, exports.transformKeys)(fn)(value);
});
return transformedObject;
};
exports.transformKeys = transformKeys;
exports.transformKeysToCamelCase = (0, exports.transformKeys)(strings_1.toCamelCase);