Skip to content

Commit

Permalink
Made the JSON extractor more forgiving.
Browse files Browse the repository at this point in the history
Change the regular expression to just match everything between curly braces.

This makes a template of `{{data|json}}` to match across multi-line JSON work (test added).

Part of #17. Part of #45.
  • Loading branch information
jkomoros committed Jul 22, 2023
1 parent 4d6df87 commit f09991d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const VALUE_PATTERNS : {[t in TemplateVarType]: string} = {
'float': '-?\\d+?(\\.\\d+?)?',
'boolean': [...Object.keys(TRUE_LITERALS), ...Object.keys(FALSE_LITERALS)].join('|'),
'whitespace': '\\s+',
'json': String.raw`((\[[^\}]{3,})?\{s*[^\}\{]{3,}?:.*\}([^\{]+\])?)`
'json': '\\{([^}]*)\\}'
};

//templates with a var of IGNORE_VAR should be ignored for rendering and
Expand Down
23 changes: 23 additions & 0 deletions test/template/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,29 @@ describe('template.extract', () => {
assert.deepStrictEqual(actual, golden);
});

it('json modifier full text multi-line', () => {
const template = '{{foo|json}}';
const input = `{
"a": 1,
"b":[
2,
3
]
}`;
const t = new Template(template);
const actual = t.extract(input);
const golden = {
foo: {
a: 1,
b: [
2,
3
]
}
};
assert.deepStrictEqual(actual, golden);
});

it('json modifier', () => {
const template = 'foo {{foo|json}} bar';
const input = 'foo {"a": 1, "b":[2,3]} bar';
Expand Down

0 comments on commit f09991d

Please sign in to comment.