Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit c04af1feaba1d7976ac909f3bbdd9bb87b9d6622 0 parents
@tj authored
6 .gitmodules
@@ -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
0  History.md
No changes.
7 Makefile
@@ -0,0 +1,7 @@
+
+test:
+ @./support/expresso/bin/expresso \
+ -I support \
+ -I lib
+
+.PHONY: test
29 Readme.md
@@ -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 benchmark.js
@@ -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 examples.js
@@ -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  index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/querystring');
59 lib/querystring.js
@@ -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  support/expresso
@@ -0,0 +1 @@
+Subproject commit 2c8759f147831c5ab018f764e433748f632702fc
1  support/should
@@ -0,0 +1 @@
+Subproject commit c05fac17d9894f1daeefded4db6609f962424831
82 test/querystring.test.js
@@ -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' }]
+ // });
+ // }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.