Comparing version 1.1.0 to 1.2.0
@@ -9,2 +9,3 @@ // Load modules | ||
var internals = { | ||
delimiter: '&', | ||
depth: 5, | ||
@@ -16,6 +17,8 @@ arrayLimit: 20, | ||
internals.parseValues = function (str) { | ||
internals.parseValues = function (str, delimiter) { | ||
delimiter = typeof delimiter === 'undefined' ? internals.delimiter : delimiter; | ||
var obj = {}; | ||
var parts = str.split('&').slice(0, internals.parametersLimit); | ||
var parts = str.split(delimiter, internals.parametersLimit); | ||
@@ -84,4 +87,2 @@ for (var i = 0, il = parts.length; i < il; ++i) { | ||
depth = typeof depth === 'undefined' ? internals.depth : depth; | ||
// The regex chunks | ||
@@ -130,3 +131,3 @@ | ||
module.exports = function (str, depth) { | ||
module.exports = function (str, depth, delimiter) { | ||
@@ -140,7 +141,12 @@ if (str === '' || | ||
var tempObj = typeof str === 'string' ? internals.parseValues(str) : Utils.clone(str); | ||
if (typeof depth !== 'number') { | ||
delimiter = depth; | ||
depth = internals.depth; | ||
} | ||
var tempObj = typeof str === 'string' ? internals.parseValues(str, delimiter) : Utils.clone(str); | ||
var obj = {}; | ||
// Iterate over the keys and setup the new object | ||
// | ||
for (var key in tempObj) { | ||
@@ -147,0 +153,0 @@ if (tempObj.hasOwnProperty(key)) { |
@@ -6,3 +6,5 @@ // Load modules | ||
var internals = {}; | ||
var internals = { | ||
delimiter: '&' | ||
}; | ||
@@ -41,4 +43,6 @@ | ||
module.exports = function (obj) { | ||
module.exports = function (obj, delimiter) { | ||
delimiter = typeof delimiter === 'undefined' ? internals.delimiter : delimiter; | ||
var keys = []; | ||
@@ -52,3 +56,3 @@ | ||
return keys.join('&'); | ||
return keys.join(delimiter); | ||
}; |
@@ -13,4 +13,3 @@ // Load modules | ||
for (var i = 0, il = source.length; i < il; ++i) { | ||
if (source[i] !== undefined && | ||
source[i] !== null) { | ||
if (typeof source[i] !== 'undefined') { | ||
@@ -58,4 +57,9 @@ obj[i] = source[i]; | ||
for (var i = 0, il = source.length; i < il; ++i) { | ||
if (source[i] !== undefined) { | ||
obj[i] = source[i]; | ||
if (typeof source[i] !== 'undefined') { | ||
if (typeof obj[i] === 'object') { | ||
obj[i] = exports.merge(obj[i], source[i]); | ||
} | ||
else { | ||
obj[i] = source[i]; | ||
} | ||
} | ||
@@ -119,5 +123,3 @@ } | ||
for (var i = 0, l = obj[key].length; i < l; i++) { | ||
if (obj[key].hasOwnProperty(i) && | ||
obj[key][i] !== null) { | ||
if (typeof obj[key][i] !== 'undefined') { | ||
compacted[key].push(obj[key][i]); | ||
@@ -124,0 +126,0 @@ } |
{ | ||
"name": "qs", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A querystring parser that supports nesting and arrays, with a depth limit", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/hapijs/qs", |
@@ -22,2 +22,6 @@ # qs | ||
```javascript | ||
Qs.parse(string, [depth], [delimiter]); | ||
``` | ||
**qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`. | ||
@@ -83,2 +87,9 @@ For example, the string `'foo[bar]=baz'` converts to: | ||
An optional delimiter can also be passed: | ||
```javascript | ||
Qs.parse('a=b;c=d', ';'); | ||
// { a: 'b', c: 'd' } | ||
``` | ||
### Parsing Arrays | ||
@@ -142,2 +153,6 @@ | ||
```javascript | ||
Qs.stringify(object, [delimiter]); | ||
``` | ||
When stringifying, **qs** always URI encodes output. Objects are stringified as you would expect: | ||
@@ -174,1 +189,7 @@ | ||
``` | ||
The delimiter may be overridden with stringify as well: | ||
```javascript | ||
Qs.stringify({ a: 'b', c: 'd' }, ';'); | ||
// 'a=b;c=d' |
@@ -141,2 +141,3 @@ // Load modules | ||
expect(Qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } }); | ||
expect(Qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb')).to.deep.equal({foo: [ {a: 'a', b: 'b'}, {a: 'aa', b: 'bb'} ]}); | ||
done(); | ||
@@ -240,6 +241,17 @@ }); | ||
Object.prototype.crash = ''; | ||
expect(Qs.parse.bind(null, 'test')).to.not.throw(); | ||
Array.prototype.crash = ''; | ||
expect(Qs.parse.bind(null, 'a=b')).to.not.throw(); | ||
expect(Qs.parse('a=b')).to.deep.equal({ a: 'b' }); | ||
expect(Qs.parse.bind(null, 'a[][b]=c')).to.not.throw(); | ||
expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }); | ||
delete Object.prototype.crash; | ||
delete Array.prototype.crash; | ||
done(); | ||
}); | ||
it('parses a string with an alternative delimiter', function (done) { | ||
expect(Qs.parse('a=b;c=d', ';')).to.deep.equal({ a: 'b', c: 'd' }); | ||
done(); | ||
}); | ||
}); |
@@ -123,2 +123,8 @@ // Load modules | ||
}); | ||
it('stringifies an object using an alternative delimiter', function (done) { | ||
expect(Qs.stringify({ a: 'b', c: 'd' }, ';')).to.equal('a=b;c=d'); | ||
done(); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26946
531
192