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 mixed array and object dot notation support Closes: #47 #66

Merged
merged 3 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ internals.parseKeys = function (key, val, options) {
return;
}

// Transform dot notation to bracket notation

key = key.replace(/\.(\w+)/g, '[$1]');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels too strict, \w is alphanumeric plus underscores, so it rules out plenty of valid characters. Maybe better to make it /\.([^\.\[]+)/.

I'm also curious how big of a hit this has on performance..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the regex.

What is the best way to measure perf? A few console logs show the speed to be the same as before.


// The regex chunks

var parent = /^([^\[\]]*)/;
Expand Down
41 changes: 35 additions & 6 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ 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].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('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' } });
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 } });
Expand Down Expand Up @@ -330,6 +345,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 = {
Expand Down Expand Up @@ -360,12 +395,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 = {};
Expand Down