@yuxilabs/gptp-core
Version:
Core validation, formatting and execution logic for the GPTP file format.
35 lines (34 loc) • 1.02 kB
JavaScript
/**
* interpolation.ts
* Simple variable interpolation utility for GPTP string templates.
* Replaces {{variable}} with values from context.
*/
/**
* Interpolates a single string.
*/
export function interpolate(template, context) {
return template.replace(/{{\s*([\w\d_-]+)\s*}}/g, (match, key) => {
const value = context[key];
return value !== undefined ? String(value) : match;
});
}
/**
* Recursively interpolates values inside strings, arrays, and objects.
* This is the full-fat version.
*/
export function interpolateVariables(input, context) {
if (typeof input === 'string') {
return interpolate(input, context);
}
if (Array.isArray(input)) {
return input.map((item) => interpolateVariables(item, context));
}
if (input && typeof input === 'object') {
const result = {};
for (const key in input) {
result[key] = interpolateVariables(input[key], context);
}
return result;
}
return input;
}