ClientX/_src/template.js

15 lines
584 B
JavaScript
Raw Normal View History

2025-05-06 19:21:55 +00:00
"use strict";
/**
* expandTemplate: simple variable substitution in {{key}} placeholders.
* @param {string} template - The template string with {{key}} tokens.
* @param {object} context - Mapping of key -> replacement value.
* @returns {string} - The template with keys replaced by context values.
*/
export function expandTemplate(template, context) {
if (typeof template !== 'string') return '';
return template.replace(/\{\{\s*(\w+)\s*\}\}/g, (_match, key) => {
return Object.prototype.hasOwnProperty.call(context, key)
? String(context[key])
: '';
});
}