Skip to content

Commit

Permalink
stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdum committed May 7, 2015
1 parent 222f2e8 commit b89e303
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ in js file
```javascript
qs = require('query-string-object')
qs('xx=yy&zz..') // just like what is pass to the native qs.parse
qs.stringify({
fInt: 1,
fString: 'string',
fObject: {
anobject: {a:1, b:2},
anything: 'thing'
},
fArray: [1,2,"string"]
})
// fInt=1&fString=string&fObject[anobject][a]=1&fObject[anobject][b]=2&fObject[anything]=thing&fArray[]=1&fArray[]=2&fArray[]=string
```

###Tests
Expand Down
50 changes: 49 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
native_qs = require("querystring");
var native_qs = require("querystring");
var _ = require('lodash');

module.exports = function(query) {
if( ! query ) return {};
Expand Down Expand Up @@ -30,3 +31,50 @@ function parse( query ){
}
return dirty ? parse(query) : query
}

module.exports.stringify = function(obj) {
if (_.isObject(obj) && !_.isArray(obj)) {
var result = [];
_.each(obj, function(val, key){
if (_.isArray(val)) {
result.push(stringifyArray(key, val));
} else if (_.isObject(val)) {
result.push(stringifyObject(key, val));
} else {
result.push(key+'='+val);
}
});
return result.join('&');
} else {
throw new Error('Invalid Object Format');
return ;
}
}

function stringifyObject (key, obj) {
var result = [];
_.each(obj, function(val, key2){
key2 = key+'['+key2+']';
if (_.isArray(val)) {
result.push(stringifyArray(key2, val));
} else if (_.isObject(val)) {
result.push(stringifyObject(key2, val));
} else {
result.push(key2+'='+val);
}
});
return result.join('&');
}

function stringifyArray (key, obj) {
var result = [];
_.each(obj, function(val){
if (_.isObject(val)) {
// not allow any object in an array
throw new Error('Not Allow Any Object In An Array:'+key+'='+JSON.stringify(obj));
} else {
result.push(key+'[]=' + val);
}
});
return result.join('&');
}
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
{
"name": "query-string-object",
"version": "0.1.1",
"version": "0.2.0",
"desciption": "support object or array like what's php did: ?a[a]=1&a[b]=2 -> a={a:1,b:2}",
"keywords": "querystring, object, array, php, query, extend, string, rest, restful",
"homepage": "https://github.com/ericdum/querystring/",
"bugs": "https://github.com/ericdum/querystring/issues",
"author": "木酱 <[email protected]> (http://mujiang.info/)",
"repository": {"type":"git", "url": "https://github.com/ericdum/querystring"},
"repository": {
"type": "git",
"url": "https://github.com/ericdum/querystring"
},
"scripts": {
"test": "./node_modules/.bin/mocha --reporter tap test.js"
},
"devDependencies" : {
"mocha" : "*",
"expect.js" : "*"
"devDependencies": {
"mocha": "*",
"expect.js": "*"
},
"dependencies": {
"lodash": "~3.8.0"
}
}
64 changes: 64 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,67 @@ describe("querystring", function(){
});
})
});
describe("querystring.stringify", function(){
it("normal: ?a=1&b=2", function(){
expect(qs.stringify({
a:1,
b:2
})).to.be.eql("a=1&b=2");
})
it("array: ?a=1&b[]=1&b[]=2&c=3", function(){
expect(qs.stringify( {
a:1,
b:[1,2],
c:3
})).to.be.eql( "a=1&b[]=1&b[]=2&c=3" );
})
it("array with single value: ?a=1&b[]=1&c=3", function(){
expect(qs.stringify( {
a:1,
b:[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.stringify( {
a:1,
b:{1:1},
c:{3:2}
})).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.stringify( {
a:1,
b:{
eric:"dum"
},
c:{
stive:"jobs"
}
})).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.stringify( {
a:1,
b:{
eric:"dum",
stive:"jobs"
},
c:3
})).to.be.eql( "a=1&b[eric]=dum&b[stive]=jobs&c=3" );
})
it("array in object: ?a=1&b[eric][]=dum&b[stive][]=jobs&b[stive][]=fans&c=3", function(){
expect(qs.stringify( {
a:1,
b:{
eric:["dum"],
stive: [
"jobs",
"fans"
]
},
c:3
})).to.be.eql( "a=1&b[eric][]=dum&b[stive][]=jobs&b[stive][]=fans&c=3");
})
//*/
});

0 comments on commit b89e303

Please sign in to comment.