Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for quotes outside template and scaped quotes inside template #7

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"');
});
})