Comparing version 0.1.0 to 0.2.0
@@ -9,32 +9,32 @@ | ||
var obj = qs.parse('foo'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('foo=bar=baz'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('users[]'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('name=tj&email=tj@vision-media.ca'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('users[]=tj&users[]=tobi&users[]=jane'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('user[name][first]=tj&user[name][last]=holowaychuk'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('users[][name][first]=tj&users[][name][last]=holowaychuk'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('a=a&a=b&a=c'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('user[tj]=tj&user[tj]=TJ'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('user[names]=tj&user[names]=TJ&user[names]=Tyler'); | ||
require('inspect')(obj) | ||
console.log(obj) | ||
var obj = qs.parse('user[name][first]=tj&user[name][first]=TJ'); | ||
require('inspect')(obj) | ||
console.log(obj) |
0.2.0 / 2011-06-29 | ||
================== | ||
* Added `qs.stringify()` [Cory Forsyth] | ||
0.1.0 / 2011-04-13 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -12,5 +12,11 @@ | ||
exports.version = '0.1.0'; | ||
exports.version = '0.2.0'; | ||
/** | ||
* Object#toString() ref for stringify(). | ||
*/ | ||
var toString = Object.prototype.toString; | ||
/** | ||
* Parse the given query `str`, returning an object. | ||
@@ -23,3 +29,3 @@ * | ||
exports.parse = function(str) { | ||
exports.parse = function(str){ | ||
if (str == undefined || str == '') return {}; | ||
@@ -86,2 +92,76 @@ | ||
/** | ||
* Turn the given `obj` into a query string | ||
* | ||
* @param {Object} obj | ||
* @return {String} | ||
* @api public | ||
*/ | ||
var stringify = exports.stringify = function(obj, prefix) { | ||
if (Array.isArray(obj)) { | ||
return stringifyArray(obj, prefix); | ||
} else if ('[object Object]' == toString.call(obj)) { | ||
return stringifyObject(obj, prefix); | ||
} else if ('string' == typeof obj) { | ||
return stringifyString(obj, prefix); | ||
} else { | ||
return prefix; | ||
} | ||
}; | ||
/** | ||
* Stringify the given `str`. | ||
* | ||
* @param {String} str | ||
* @param {String} prefix | ||
* @return {String} | ||
* @api private | ||
*/ | ||
function stringifyString(str, prefix) { | ||
if (!prefix) throw new TypeError('stringify expects an object'); | ||
return prefix + '=' + encodeURIComponent(str); | ||
} | ||
/** | ||
* Stringify the given `arr`. | ||
* | ||
* @param {Array} arr | ||
* @param {String} prefix | ||
* @return {String} | ||
* @api private | ||
*/ | ||
function stringifyArray(arr, prefix) { | ||
var ret = []; | ||
if (!prefix) throw new TypeError('stringify expects an object'); | ||
for (var i = 0; i < arr.length; i++) { | ||
ret.push(stringify(arr[i], prefix + '[]')); | ||
} | ||
return ret.join('&'); | ||
} | ||
/** | ||
* Stringify the given `obj`. | ||
* | ||
* @param {Object} obj | ||
* @param {String} prefix | ||
* @return {String} | ||
* @api private | ||
*/ | ||
function stringifyObject(obj, prefix) { | ||
var ret = [] | ||
, keys = Object.keys(obj) | ||
, key; | ||
for (var i = 0, len = keys.length; i < len; ++i) { | ||
key = keys[i]; | ||
ret.push(stringify(obj[key], prefix | ||
? prefix + '[' + encodeURIComponent(key) + ']' | ||
: encodeURIComponent(key))); | ||
} | ||
return ret.join('&'); | ||
} | ||
/** | ||
* Set `obj`'s `key` to `val` respecting | ||
@@ -88,0 +168,0 @@ * the weird and wonderful syntax of a qs, |
{ | ||
"name": "qs", | ||
"description": "querystring parser", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"repository": {}, | ||
@@ -6,0 +6,0 @@ "author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", |
@@ -1,2 +0,1 @@ | ||
# node-querystring | ||
@@ -10,7 +9,19 @@ | ||
## Examples | ||
require('querystring').parse('user[name][first]=tj&user[email]=tj'); | ||
// => { user: { name: { first: 'tj' }}} | ||
require('qs').parse('user[name][first]=tj&user[email]=tj'); | ||
// => { user: { name: { first: 'tj' }, email: 'tj' } } | ||
## Testing | ||
Update git submodules: | ||
$ git submodule init | ||
$ git submodule update | ||
and execute: | ||
$ make test | ||
## License | ||
@@ -17,0 +28,0 @@ |
179072
42
1585
49