51 lines
1.5 KiB
Plaintext
51 lines
1.5 KiB
Plaintext
import {htmlEscape} from 'escape-goat';
|
|
|
|
export class MissingValueError extends Error {
|
|
constructor(key) {
|
|
super(`Missing a value for ${key ? `the placeholder: ${key}` : 'a placeholder'}`, key);
|
|
this.name = 'MissingValueError';
|
|
this.key = key;
|
|
}
|
|
}
|
|
|
|
export default function pupa(template, data, {ignoreMissing = false, transform = ({value}) => value} = {}) {
|
|
if (typeof template !== 'string') {
|
|
throw new TypeError(`Expected a \`string\` in the first argument, got \`${typeof template}\``);
|
|
}
|
|
|
|
if (typeof data !== 'object') {
|
|
throw new TypeError(`Expected an \`object\` or \`Array\` in the second argument, got \`${typeof data}\``);
|
|
}
|
|
|
|
const replace = (placeholder, key) => {
|
|
let value = data;
|
|
for (const property of key.split('.')) {
|
|
value = value ? value[property] : undefined;
|
|
}
|
|
|
|
const transformedValue = transform({value, key});
|
|
if (transformedValue === undefined) {
|
|
if (ignoreMissing) {
|
|
return placeholder;
|
|
}
|
|
|
|
throw new MissingValueError(key);
|
|
}
|
|
|
|
return String(transformedValue);
|
|
};
|
|
|
|
const composeHtmlEscape = replacer => (...args) => htmlEscape(replacer(...args));
|
|
|
|
// The regex tries to match either a number inside `{{ }}` or a valid JS identifier or key path.
|
|
const doubleBraceRegex = /{{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}}/gi;
|
|
|
|
if (doubleBraceRegex.test(template)) {
|
|
template = template.replace(doubleBraceRegex, composeHtmlEscape(replace));
|
|
}
|
|
|
|
const braceRegex = /{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}/gi;
|
|
|
|
return template.replace(braceRegex, replace);
|
|
}
|