@langchain/core
Version:
Core LangChain.js abstractions and schemas
27 lines (26 loc) • 971 B
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
export { keyFromJson, keyToJson, mapKeys };
//# sourceMappingURL=map_keys.js.map