Skip to content

Commit

Permalink
Merge pull request #7 from port-labs/jq-template-fix
Browse files Browse the repository at this point in the history
Add support for quotes outside template and scaped quotes inside template
  • Loading branch information
talsabagport authored Dec 27, 2023
2 parents 4dcf294 + 384aa6b commit c31e127
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 c31e127

Please sign in to comment.