Skip to content

Commit

Permalink
Add support for dashes in keys (sindresorhus#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
peteruithoven authored and naveen-solink committed Aug 25, 2022
1 parent 9e000b6 commit 1ba448a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ module.exports = (template, data, {ignoreMissing = false, transform = ({value})
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$_][a-z\d$_]*?(?:\.[a-z\d$_]*?)*?)}}/gi;
const doubleBraceRegex = /{{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}}/gi;

if (doubleBraceRegex.test(template)) {
template = template.replace(doubleBraceRegex, composeHtmlEscape(replace));
}

const braceRegex = /{(\d+|[a-z$_][a-z\d$_]*?(?:\.[a-z\d$_]*?)*?)}/gi;
const braceRegex = /{(\d+|[a-z$_][\w\-$]*?(?:\.[\w\-$]*?)*?)}/gi;

return template.replace(braceRegex, replace);
};
Expand Down
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test('main', t => {
t.is(pupa('{foo}', {foo: '!'}), '!');
t.is(pupa('{foo}', {foo: 10}), '10');
t.is(pupa('{foo}', {foo: 0}), '0');
t.is(pupa('{fo-o}', {'fo-o': 0}), '0');
t.is(pupa('{foo}{foo}', {foo: '!'}), '!!');
t.is(pupa('{foo}{bar}{foo}', {foo: '!', bar: '#'}), '!#!');
t.is(pupa('yo {foo} lol {bar} sup', {foo: '🦄', bar: '🌈'}), 'yo 🦄 lol 🌈 sup');
Expand Down

0 comments on commit 1ba448a

Please # to comment.