diff --git a/src/filter.js b/src/filter.js index 444859200..0e7810a43 100644 --- a/src/filter.js +++ b/src/filter.js @@ -36,7 +36,9 @@ module.exports = function (options) { while ((match = valueRE.exec(argList.trim()))) { var v = match[0] var re = new RegExp(`${v}\\s*:`, 'g') - re.test(match.input) ? args.push(`'${v}'`) : args.push(v) + var keyMatch = re.exec(match.input) + var currentMatchIsKey = keyMatch && keyMatch.index === match.index + currentMatchIsKey ? args.push(`'${v}'`) : args.push(v) } this.name = name diff --git a/test/filter.js b/test/filter.js index 3af7a9894..97c178ba7 100644 --- a/test/filter.js +++ b/test/filter.js @@ -54,4 +54,40 @@ describe('filter', function () { filter.construct('foo: 33').render('foo', scope) expect(spy).to.have.been.calledWith('foo', 33) }) + + it('should support arguments as named key/values', function () { + filter.register('foo', x => x) + var f = filter.construct('foo: key1: "literal1", key2: value2'); + expect(f.name).to.equal('foo') + expect(f.args).to.deep.equal([ '\'key1\'', '"literal1"', '\'key2\'', 'value2' ]); + }) + + it('should support arguments as named key/values with inline literals', function () { + filter.register('foo', x => x) + var f = filter.construct('foo: "test0", key1: "literal1", key2: value2'); + expect(f.name).to.equal('foo') + expect(f.args).to.deep.equal([ '"test0"', '\'key1\'', '"literal1"', '\'key2\'', 'value2' ]); + }) + + it('should support arguments as named key/values with inline values', function () { + filter.register('foo', x => x) + var f = filter.construct('foo: test0, key1: "literal1", key2: value2'); + expect(f.name).to.equal('foo') + expect(f.args).to.deep.equal([ 'test0', '\'key1\'', '"literal1"', '\'key2\'', 'value2' ]); + }) + + + it('should support argument values named same as keys', function () { + filter.register('foo', x => x) + var f = filter.construct('foo: a: a'); + expect(f.name).to.equal('foo') + expect(f.args).to.deep.equal(['\'a\'', 'a']) + }) + + it('should support argument literals named same as keys', function () { + filter.register('foo', x => x) + var f = filter.construct('foo: a: "a"'); + expect(f.name).to.equal('foo') + expect(f.args).to.deep.equal(['\'a\'', '"a"']) + }) }) diff --git a/test/filters.js b/test/filters.js index 0ee245b8b..c432423bb 100644 --- a/test/filters.js +++ b/test/filters.js @@ -394,6 +394,7 @@ describe('filters', function () { liquid.registerFilter('obj_test', function () { return Array.prototype.slice.call(arguments).join(',') }) - it('should support object', () => test('{{ "a" | obj_test: k1: "v1", k2: "v2" }}', 'a,k1,v1,k2,v2')) + it('should support object', () => test(`{{ "a" | obj_test: k1: "v1", k2: foo }}`, 'a,k1,v1,k2,bar')) + it('should support mixed object', () => test(`{{ "a" | obj_test: "something", k1: "v1", k2: foo }}`, 'a,something,k1,v1,k2,bar')) }) })