Comparing version 0.2.0 to 0.3.0
var old = require('querystring') | ||
, qs = require('./') | ||
, times = 100000; | ||
var qs = require('./'); | ||
var start = new Date | ||
var times = 100000 | ||
, start = new Date | ||
, n = times; | ||
while (n--) old.parse('foo=bar'); | ||
console.log('old simple: %dms', new Date - start); | ||
console.log('times: %d', times); | ||
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); | ||
console.log('simple: %dms', new Date - start); | ||
@@ -32,2 +17,2 @@ var start = new Date | ||
while (n--) qs.parse('user[name][first]=tj&user[name][last]=holowaychuk'); | ||
console.log('new nested: %dms', new Date - start); | ||
console.log('nested: %dms', new Date - start); |
@@ -39,2 +39,11 @@ | ||
var obj = qs.parse('user[name][first]=tj&user[name][first]=TJ'); | ||
console.log(obj) | ||
console.log(obj) | ||
var obj = qs.parse('user[0]=tj&user[1]=TJ'); | ||
console.log(obj) | ||
var obj = qs.parse('user[0]=tj&user[]=TJ'); | ||
console.log(obj) | ||
var obj = qs.parse('user[0]=tj&user[foo]=TJ'); | ||
console.log(obj) |
0.3.0 / 2011-07-19 | ||
================== | ||
* Allow `array[index]` and `object[property]` syntaxes [Aria Stewart] | ||
0.2.0 / 2011-06-29 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -12,3 +12,3 @@ | ||
exports.version = '0.2.0'; | ||
exports.version = '0.3.0'; | ||
@@ -31,3 +31,12 @@ /** | ||
if (str == undefined || str == '') return {}; | ||
var notint = /[^0-9]/ | ||
function promote(parent, key) { | ||
if(parent[key].length == 0) return parent[key] = {}; | ||
var t = {}; | ||
for(var i in parent[key]) t[i] = parent[key][i]; | ||
parent[key] = t; | ||
return t; | ||
} | ||
return String(str) | ||
@@ -42,3 +51,3 @@ .split('&') | ||
, val = val.substr(val.indexOf('=') + 1, val.length) | ||
, obj = ret; | ||
, parent = ret; | ||
@@ -54,3 +63,3 @@ // ?foo | ||
function parse(obj, parts, parent, key) { | ||
function parse(parts, parent, key) { | ||
var part = parts.shift(); | ||
@@ -64,2 +73,4 @@ | ||
parent[key] = val; | ||
} else if ('undefined' == typeof parent[key]) { | ||
parent[key] = val; | ||
} else { | ||
@@ -69,25 +80,38 @@ parent[key] = [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); | ||
obj = parent[key] = parent[key] || []; | ||
if (']' == part) { | ||
if(Array.isArray(obj)) { | ||
if ('' != val) obj.push(val); | ||
} else if ('object' == typeof obj) { | ||
obj[Object.keys(obj).length] = val; | ||
} else { | ||
obj = parent[key] = [parent[key], val]; | ||
} | ||
// prop | ||
} else if (~part.indexOf(']')) { | ||
part = part.substr(0, part.length - 1); | ||
if(notint.test(part) && Array.isArray(obj)) obj = promote(parent, key); | ||
parse(parts, obj, part); | ||
// key | ||
} else { | ||
if(notint.test(part) && Array.isArray(obj)) obj = promote(parent, key); | ||
parse(parts, obj, part); | ||
} | ||
} | ||
} | ||
parse(obj, parts); | ||
parse(parts, parent, 'base'); | ||
// optimize | ||
} else { | ||
set(obj, key, val); | ||
if(notint.test(key) && Array.isArray(parent.base)) { | ||
var t = {}; | ||
for(var k in parent.base) t[k] = parent.base[k]; | ||
parent.base = t; | ||
} | ||
set(parent.base, key, val); | ||
} | ||
return ret; | ||
}, {}); | ||
}, {base: []}).base; | ||
}; | ||
@@ -94,0 +118,0 @@ |
{ | ||
"name": "qs", | ||
"description": "querystring parser", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"repository": {}, | ||
@@ -6,0 +6,0 @@ "author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", |
@@ -120,2 +120,10 @@ | ||
qs.parse(null).should.eql({}); | ||
}, | ||
'test arrays with indexes': function(){ | ||
qs.parse('foo[0]=bar&foo[1]=baz').should.eql({"foo":["bar","baz"]}); | ||
}, | ||
'test arrays becoming objects': function(){ | ||
qs.parse('foo[0]=bar&foo[bad]=baz').should.eql({"foo":{"0":"bar","bad":"baz"}}); | ||
} | ||
@@ -122,0 +130,0 @@ |
180188
1612