@langchain/core
Version:
Core LangChain.js abstractions and schemas
29 lines (28 loc) • 1.02 kB
JavaScript
//#region src/load/map_keys.ts
const UPPER_TO_WORD_BOUNDARY = /([A-Z]+)([A-Z][a-z0-9]+)/g;
const LOWER_TO_UPPER_BOUNDARY = /([a-z0-9])([A-Z])/g;
const SEPARATORS = /[-_\s]+/g;
function snakeCase(key) {
return key.replace(UPPER_TO_WORD_BOUNDARY, "$1_$2").replace(LOWER_TO_UPPER_BOUNDARY, "$1_$2").replace(SEPARATORS, "_").toLowerCase();
}
function camelCase(key) {
const trimmed = key.trim();
if (!/[-_\s]/.test(trimmed)) return trimmed;
return trimmed.replace(SEPARATORS, "_").toLowerCase().replace(/_+([a-z0-9])/g, (_, char) => char.toUpperCase());
}
function keyToJson(key, map) {
return map?.[key] || snakeCase(key);
}
function keyFromJson(key, map) {
return map?.[key] || camelCase(key);
}
function mapKeys(fields, mapper, map) {
const mapped = {};
for (const key in fields) if (Object.hasOwn(fields, key)) mapped[mapper(key, map)] = fields[key];
return mapped;
}
//#endregion
exports.keyFromJson = keyFromJson;
exports.keyToJson = keyToJson;
exports.mapKeys = mapKeys;
//# sourceMappingURL=map_keys.cjs.map