Skip to content
Open
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ output

####a=1&b[]=1&b[]=2&c=3

*object and arrays with encoded brackets like %5B%5D*

output

```
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var _ = require('lodash');
module.exports = function(query) {
if( ! query ) return {};
if( typeof query == 'string' ) {
if( ! query.match(/\[\w*\]/) ) return native_qs.parse(query);
if( ! query.match(/(\[|\%5B)\w*(\]|\%5D)/) ) return native_qs.parse(query);
query = native_qs.parse(query);
}
return parse( query )
Expand All @@ -19,7 +19,7 @@ function parse( query ){
for( var key in query ) {
var value = query[key];
if( typeof value == 'string' && !isNaN(1*value) ) value = 1*value;
if( matches = key.match(/^(.+)(\[[^\]]*\])$/) ){
if( matches = key.match(/^(.+)((\[|\%5B)[^\]]*(\]|\%5D))$/) ){
var parent = matches[1];
var child = matches[2];
if( child.match(/^\[\s*\]$/) ) {
Expand Down Expand Up @@ -47,7 +47,7 @@ module.exports.stringify = function(obj) {
result.push(stringifyArray(key, val));
} else if (_.isObject(val)) {
result.push(stringifyObject(key, val));
} else if (val) {
} else if (val || val === 0 || val === false) {
result.push(key+'='+native_qs.escape(val));
}
});
Expand Down
209 changes: 209 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"test": "./node_modules/.bin/mocha --reporter tap test.js"
},
"devDependencies": {
"mocha": "*",
"expect.js": "*"
"expect.js": "*",
"mocha": "^4.0.1"
},
"dependencies": {
"lodash": "~3.8.0",
"lodash": "^4.17.4",
"querystring-browser": "^1.0.4"
}
}
81 changes: 80 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,41 @@ describe("querystring", function(){
c:3
});
})
it("array encoded: ?a=1&b%5B%5D=1&b%5B%5D=2&c=3", function(){
expect(qs("a=1&b%5B%5D=1&b%5B%5D=2&c=3")).to.be.eql({
a:1,
b:[1,2],
c:3
});
})
it("array with single value: ?a=1&b[]=1&c=3", function(){
expect(qs("a=1&b[]=1&c=3")).to.be.eql({
a:1,
b:[1],
c:3
});
})
it("array with single value encoded: ?a=1&b%5B%5D=1&c=3", function(){
expect(qs("a=1&b%5B%5D=1&c=3")).to.be.eql({
a:1,
b:[1],
c:3
});
})
it("object of number key: ?a=1&b[1]=1&c[3]=2&c=3", function(){
expect(qs("a=1&b[1]=1&c[3]=2&c=3")).to.be.eql({
a:1,
b:{1:1},
c:{3:2}
});
})
it("object of number key encoded: ?a=1&b%5B1%5D=1&c%5B3%5D=2&c=3", function(){
expect(qs("a=1&b%5B1%5D=1&c%5B3%5D=2&c=3")).to.be.eql({
a:1,
b:{1:1},
c:{3:2}
});
})
it("object with single key: ?a=1&b[eric]=dum&c[stive]=jobs&c=3", function(){
expect(qs("a=1&b[eric]=dum&c[stive]=jobs&c=3")).to.be.eql({
a:1,
Expand All @@ -44,6 +65,17 @@ describe("querystring", function(){
}
});
})
it("object with single key encoded: ?a=1&b%5Beric%5D=dum&c%5Bstive%5D=jobs&c=3", function(){
expect(qs("a=1&b%5Beric%5D=dum&c%5Bstive%5D=jobs&c=3")).to.be.eql({
a:1,
b:{
eric:"dum"
},
c:{
stive:"jobs"
}
});
})
it("object with multiple key: ?a=1&b[eric]=dum&b[stive]=jobs&c=3", function(){
expect(qs("a=1&b[eric]=dum&b[stive]=jobs&c=3")).to.be.eql({
a:1,
Expand All @@ -54,6 +86,16 @@ describe("querystring", function(){
c:3
});
})
it("object with multiple key encoded: ?a=1&b%5Beric%5D=dum&b%5Bstive%5D=jobs&c=3", function(){
expect(qs("a=1&b%5Beric%5D=dum&b%5Bstive%5D=jobs&c=3")).to.be.eql({
a:1,
b:{
eric:"dum",
stive:"jobs"
},
c:3
});
})
it("array in object: ?a=1&b[eric][]=dum&b[stive][]=jobs&b[stive][]=fans&c=3", function(){
expect(qs("a=1&b[eric][]=dum&b[stive][]=jobs&b[stive][]=fans&c=3")).to.be.eql({
a:1,
Expand All @@ -67,6 +109,19 @@ describe("querystring", function(){
c:3
});
})
it("array in object encoded: ?a=1&b%5Beric%5D%5B%5D=dum&b%5Bstive%5D%5B%5D=jobs&b%5Bstive%5D%5B%5D=fans&c=3", function(){
expect(qs("a=1&b%5Beric%5D%5B%5D=dum&b%5Bstive%5D%5B%5D=jobs&b%5Bstive%5D%5B%5D=fans&c=3")).to.be.eql({
a:1,
b:{
eric:["dum"],
stive: [
"jobs",
"fans"
]
},
c:3
});
})
it("x.y[c][$d]=3m&x.y[a][]=c&x.y[b][]=1&x2.y2[c][$d]=3m&x.y3[c][$d]=3m&x.y3[a]=c&x.y3[b][]=1", function(){
expect(qs("x.y[c][$d]=3m&x.y[a][]=c&x.y[b][]=1&x2.y2[c][$d]=3m&x.y3[c][$d]=3m&x.y3[a]=c&x.y3[b][]=1")).to.be.eql({
'x.y': {
Expand All @@ -90,6 +145,29 @@ describe("querystring", function(){
}
});
})
it("x.y%5Bc%5D%5B$d%5D=3m&x.y%5Ba%5D%5B%5D=c&x.y%5Bb%5D%5B%5D=1&x2.y2%5Bc%5D%5B$d%5D=3m&x.y3%5Bc%5D%5B$d%5D=3m&x.y3%5Ba%5D=c&x.y3%5Bb%5D%5B%5D=1 encoded", function(){
expect(qs("x.y%5Bc%5D%5B$d%5D=3m&x.y%5Ba%5D%5B%5D=c&x.y%5Bb%5D%5B%5D=1&x2.y2%5Bc%5D%5B$d%5D=3m&x.y3%5Bc%5D%5B$d%5D=3m&x.y3%5Ba%5D=c&x.y3%5Bb%5D%5B%5D=1")).to.be.eql({
'x.y': {
c: {
'$d': '3m'
},
a: ['c'],
b: [1]
},
'x2.y2': {
c: {
'$d': '3m'
}
},
'x.y3': {
c: {
'$d': '3m'
},
a: 'c',
b: [1]
}
});
})
});
describe("querystring.stringify", function(){
it("normal: ?a=1&b=2", function(){
Expand Down Expand Up @@ -189,8 +267,9 @@ describe("querystring.stringify", function(){
},
"zzz": {},
"www": '',
z:0,
x:1,
y:2
})).to.be.eql("x=1&y=2");
})).to.be.eql("z=0&x=1&y=2");
})
});