Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 4, 2011
0 parents commit c04af1f
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "support/expresso"]
path = support/expresso
url = git://github.com/visionmedia/expresso.git
[submodule "support/should"]
path = support/should
url = git://github.com/visionmedia/should.js.git
Empty file added History.md
Empty file.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@

test:
@./support/expresso/bin/expresso \
-I support \
-I lib

.PHONY: test
29 changes: 29 additions & 0 deletions Readme.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@

# node-querystring

query string parser for node supporting nesting, as it was removed from `0.3.x`, so this library provides the previous and commonly desired behaviour.

## License

(The MIT License)

Copyright (c) 2010 TJ Holowaychuk <tj@vision-media.ca>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions benchmark.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,32 @@

var old = require('querystring')
, qs = require('./')
, times = 100000;

var start = new Date
, n = times;

while (n--) old.parse('foo=bar');
console.log('old simple: %dms', new Date - start);

var start = new Date
, n = times;

while (n--) old.parse('user[name][first]=tj&user[name][last]=holowaychuk');
console.log('old nested: %dms', new Date - start);


console.log();


var start = new Date
, n = times;

while (n--) qs.parse('foo=bar');
console.log('new simple: %dms', new Date - start);

var start = new Date
, n = times;

while (n--) qs.parse('user[name][first]=tj&user[name][last]=holowaychuk');
console.log('new nested: %dms', new Date - start);
21 changes: 21 additions & 0 deletions examples.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,21 @@

/**
* Module dependencies.
*/

var qs = require('./');

var obj = qs.parse('users[]');
require('inspect')(obj)

var obj = qs.parse('name=tj&email=tj@vision-media.ca');
require('inspect')(obj)

var obj = qs.parse('users[]=tj&users[]=tobi&users[]=jane');
require('inspect')(obj)

var obj = qs.parse('user[name][first]=tj&user[name][last]=holowaychuk');
require('inspect')(obj)

var obj = qs.parse('users[][name][first]=tj&users[][name][last]=holowaychuk');
require('inspect')(obj)
2 changes: 2 additions & 0 deletions index.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = require('./lib/querystring');
59 changes: 59 additions & 0 deletions lib/querystring.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,59 @@

/*!
* querystring
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/

/**
* Library version.
*/

exports.version = '0.0.1';

exports.parse = function(str) {
return String(str)
.split('&')
.reduce(function(ret, pair){
var parts = decodeURIComponent(pair).split('=')
, obj = ret
, key = parts.shift()
, val = parts.join('=');

// nested
if (~key.indexOf(']')) {
var parts = key.split('[')
, len = parts.length
, last = len - 1;

function parse(obj, parts, parent, key) {
var part = parts.shift();

// end
if (!part) {
parent[key] = val;
// array
} else if (']' == part) {
obj = parent[key] = Array.isArray(parent[key])
? parent[key]
: [];
if ('' != val) obj.push(val);
// prop
} else if (~part.indexOf(']')) {
part = part.substr(0, part.length - 1);
parse(obj[part] = obj[part] || {}, parts, obj, part);
// key
} else {
parse(obj[part] = obj[part] || {}, parts, obj, part);
}
}

parse(obj, parts);
// optimize
} else {
obj[key] = val;
}

return ret;
}, {});
};
1 change: 1 addition & 0 deletions support/expresso
Submodule expresso added at 2c8759
1 change: 1 addition & 0 deletions support/should
Submodule should added at c05fac
82 changes: 82 additions & 0 deletions test/querystring.test.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,82 @@

/**
* Module dependencies.
*/

var qs = require('../')
, should = require('should');

module.exports = {
'test basics': function(){
qs.parse('foo')
.should.eql({ foo: '' });

qs.parse('foo=bar')
.should.eql({ foo: 'bar' });

qs.parse('foo%3Dbar=baz')
.should.eql({ foo: 'bar=baz' });

qs.parse(' foo = bar = baz ')
.should.eql({ ' foo ': ' bar = baz ' });

qs.parse('foo=bar=baz')
.should.eql({ foo: 'bar=baz' });

qs.parse('foo=bar&bar=baz')
.should.eql({ foo: 'bar', bar: 'baz' });

qs.parse('foo=bar&baz')
.should.eql({ foo: 'bar', baz: '' });

qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World')
.should.eql({
cht: 'p3'
, chd: 't:60,40'
, chs: '250x100'
, chl: 'Hello|World'
});
},

'test nesting': function(){
qs.parse('user[name]=tj')
.should.eql({ user: { name: 'tj' }});

qs.parse('user[name][first]=tj&user[name][last]=holowaychuk')
.should.eql({ user: { name: { first: 'tj', last: 'holowaychuk' }}});
},

'test escaping': function(){
qs.parse('foo=foo%20bar')
.should.eql({ foo: 'foo bar' });
},

'test arrays': function(){
qs.parse('images[]')
.should.eql({ images: [] });

qs.parse('user[]=tj')
.should.eql({ user: ['tj'] });

qs.parse('user[]=tj&user[]=tobi&user[]=jane')
.should.eql({ user: ['tj', 'tobi', 'jane'] });

qs.parse('user[names][]=tj&user[names][]=tyler')
.should.eql({ user: { names: ['tj', 'tyler'] }});

qs.parse('user[names][]=tj&user[names][]=tyler&user[email]=tj@vision-media.ca')
.should.eql({ user: { names: ['tj', 'tyler'], email: 'tj@vision-media.ca' }});
},

// 'test complex': function(){
// qs.parse('users[][name][first]=tj&users[foo]=bar')
// .should.eql({
// users: [ { name: 'tj' }, { name: 'tobi' }, { foo: 'bar' }]
// });
//
// qs.parse('users[][name][first]=tj&users[][name][first]=tobi')
// .should.eql({
// users: [ { name: 'tj' }, { name: 'tobi' }]
// });
// }
};

0 comments on commit c04af1f

Please sign in to comment.