From 6ee5df78cc8c1adaa3928f2ccb4be401d952fa61 Mon Sep 17 00:00:00 2001 From: Alex Meah Date: Tue, 13 Jan 2015 17:15:02 +0000 Subject: [PATCH 1/3] Add mixed array and object dot notation support Closes: #47 --- lib/parse.js | 4 ++++ test/parse.js | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 4e7d02a1..cdbf863a 100755 --- a/lib/parse.js +++ b/lib/parse.js @@ -83,6 +83,10 @@ internals.parseKeys = function (key, val, options) { return; } + // Transform dot notation to bracket notation + + key = key.replace(/\.(\w+)/, '[$1]'); + // The regex chunks var parent = /^([^\[\]]*)/; diff --git a/test/parse.js b/test/parse.js index 6c20cc1b..5dd693bf 100755 --- a/test/parse.js +++ b/test/parse.js @@ -160,6 +160,20 @@ describe('parse()', function () { done(); }); + it('transforms arrays to objects (dot notation)', function (done) { + + expect(Qs.parse('foo[0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: 'baz' } }); + expect(Qs.parse('foo[0][0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [[ { baz: 'bar'} ]], fool: { bad: 'baz' } }); + expect(Qs.parse('roomInfoList[0].childrenAges[0]=15&roomInfoList[0].numberOfAdults=2')).to.deep.equal({roomInfoList: [{childrenAges: ['15'],numberOfAdults: '2'}]}); + expect(Qs.parse('roomInfoList[0].childrenAges[0]=15&roomInfoList[0].childrenAges[1]=16&roomInfoList[0].numberOfAdults=2')).to.deep.equal({roomInfoList: [{childrenAges: ['15','16'],numberOfAdults: '2'}]}); + expect(Qs.parse('foo.bad=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); + expect(Qs.parse('foo.bad=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); + expect(Qs.parse('foo[]=bar&foo.bad=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }); + expect(Qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } }); + expect(Qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb')).to.deep.equal({foo: [ {a: 'a', b: 'b'}, {a: 'aa', b: 'bb'} ]}); + done(); + }); + it('can add keys to objects', function (done) { expect(Qs.parse('a[b]=c&a=d')).to.deep.equal({ a: { b: 'c', d: true } }); @@ -330,6 +344,26 @@ describe('parse()', function () { done(); }); + it('parses an object in dot notation', function (done) { + + var input = { + 'user.name': {'pop[bob]': 3}, + 'user.email.': null + }; + + var expected = { + 'user': { + 'name': {'pop[bob]': 3}, + 'email': null + } + }; + + var result = Qs.parse(input); + + expect(result).to.deep.equal(expected); + done(); + }); + it('parses an object and not child values', function (done) { var input = { @@ -360,12 +394,6 @@ describe('parse()', function () { done(); }); - it('does not crash when using invalid dot notation', function (done) { - - expect(Qs.parse('roomInfoList[0].childrenAges[0]=15&roomInfoList[0].numberOfAdults=2')).to.deep.equal({ roomInfoList: [['15', '2']] }); - done(); - }); - it('does not crash when parsing circular references', function (done) { var a = {}; From eb75effd0e358343a321c08cd12660799c89150e Mon Sep 17 00:00:00 2001 From: Alex Meah Date: Tue, 13 Jan 2015 18:27:17 +0000 Subject: [PATCH 2/3] Fixes key.key.key=value for dot notation --- lib/parse.js | 2 +- test/parse.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index cdbf863a..39736ce5 100755 --- a/lib/parse.js +++ b/lib/parse.js @@ -85,7 +85,7 @@ internals.parseKeys = function (key, val, options) { // Transform dot notation to bracket notation - key = key.replace(/\.(\w+)/, '[$1]'); + key = key.replace(/\.(\w+)/g, '[$1]'); // The regex chunks diff --git a/test/parse.js b/test/parse.js index 5dd693bf..09bd87f6 100755 --- a/test/parse.js +++ b/test/parse.js @@ -163,9 +163,10 @@ describe('parse()', function () { it('transforms arrays to objects (dot notation)', function (done) { expect(Qs.parse('foo[0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: 'baz' } }); + expect(Qs.parse('foo[0].baz=bar&fool.bad.boo=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: { boo: 'baz' } } }); expect(Qs.parse('foo[0][0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [[ { baz: 'bar'} ]], fool: { bad: 'baz' } }); - expect(Qs.parse('roomInfoList[0].childrenAges[0]=15&roomInfoList[0].numberOfAdults=2')).to.deep.equal({roomInfoList: [{childrenAges: ['15'],numberOfAdults: '2'}]}); - expect(Qs.parse('roomInfoList[0].childrenAges[0]=15&roomInfoList[0].childrenAges[1]=16&roomInfoList[0].numberOfAdults=2')).to.deep.equal({roomInfoList: [{childrenAges: ['15','16'],numberOfAdults: '2'}]}); + expect(Qs.parse('foo[0].baz[0]=15&foo[0].bar=2')).to.deep.equal({foo: [{baz: ['15'],bar: '2'}]}); + expect(Qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2')).to.deep.equal({foo: [{baz: ['15','16'],bar: '2'}]}); expect(Qs.parse('foo.bad=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); expect(Qs.parse('foo.bad=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); expect(Qs.parse('foo[]=bar&foo.bad=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }); From 2b89a6be2f9e1ba0f46651c349ad86755923cb77 Mon Sep 17 00:00:00 2001 From: Alex Meah Date: Tue, 13 Jan 2015 19:58:18 +0000 Subject: [PATCH 3/3] Update dot notation transform regex to be less restrictive --- lib/parse.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parse.js b/lib/parse.js index 39736ce5..0fa6699e 100755 --- a/lib/parse.js +++ b/lib/parse.js @@ -85,7 +85,7 @@ internals.parseKeys = function (key, val, options) { // Transform dot notation to bracket notation - key = key.replace(/\.(\w+)/g, '[$1]'); + key = key.replace(/\.([^\.\[]+)/g, '[$1]'); // The regex chunks