@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
29 lines (28 loc) • 994 B
JavaScript
//#region src/interpreter/splitAndJoinInsertion.ts
/**
* Check if a value is a complex object (not a primitive)
* Used to determine if values need to be wrapped in fragments
*/
const isComplexValue = (value) => value != null && typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean";
const insertionRegex = /\{\{\s*(.*?)\s*\}\}/g;
const splitInsertionTemplate = (template, values = {}) => {
if (!Object.values(values).some(isComplexValue)) return {
isSimple: true,
parts: template.replace(insertionRegex, (_, key) => (values[key.trim()] ?? "").toString())
};
const chunks = template.split(insertionRegex);
const parts = [];
for (let i = 0; i < chunks.length; i++) if (i % 2 === 0) {
if (chunks[i]) parts.push(chunks[i]);
} else {
const val = values[chunks[i].trim()];
if (val != null) parts.push(val);
}
return {
isSimple: false,
parts
};
};
//#endregion
export { splitInsertionTemplate };
//# sourceMappingURL=splitAndJoinInsertion.mjs.map