Skip to content

Commit

Permalink
Add support for quotes outside template and scaped quotes inside temp…
Browse files Browse the repository at this point in the history
…late
  • Loading branch information
talsabagport committed Dec 27, 2023
1 parent 4dcf294 commit 384aa6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ const findInsideDoubleBracesIndices = (input) => {
for (let i = 0; i < input.length; i += 1) {
const char = input[i];

if (char === '"' || char === "'") {
// If inside quotes, ignore braces
if (insideDoubleBracesStart && char === '\\') {
// If next character is escaped, skip it
i += 1;
}
if (insideDoubleBracesStart && (char === '"' || char === "'")) {
// If inside double braces and inside quotes, ignore braces
if (!wrappingQuote) {
wrappingQuote = char;
} else if (wrappingQuote === char) {
Expand Down
13 changes: 13 additions & 0 deletions test/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,18 @@ describe('template', () => {
expect(render([{'{{.bar}}': [false, '/foo/{{.foo + .bar}}']}])).toEqual([{foo: [false, '/foo/barfoo']}]);
expect(render({foo: [{bar: '{{1}}'}, '{{empty}}']})).toEqual({foo: [{bar: 1}, undefined]});
});
it('should accept quotes outside of template', () => {
const json = { foo: 'bar', bar: 'foo' };
const render = (input) => jq.renderRecursively(json, input);

expect(render('"{{.foo}}"')).toEqual('"bar"');
expect(render('\'{{.foo}}\'')).toEqual('\'bar\'');
});
it('should accept escaped quotes inside jq template', () => {
const json = { foo: 'bar', bar: 'foo' };
const render = (input) => jq.renderRecursively(json, input);

expect(render('{{"\\"foo\\""}}')).toEqual('"foo"');
});
})

0 comments on commit 384aa6b

Please sign in to comment.