Comparing version 1.0.2 to 1.1.0
@@ -17,2 +17,5 @@ // Load modules | ||
} | ||
else if (obj === null) { | ||
obj = ''; | ||
} | ||
@@ -23,9 +26,5 @@ if (typeof obj === 'string' || | ||
return [prefix + '=' + encodeURIComponent(obj)]; | ||
return [encodeURIComponent(prefix) + '=' + encodeURIComponent(obj)]; | ||
} | ||
if (obj === null) { | ||
return [prefix]; | ||
} | ||
var values = []; | ||
@@ -35,3 +34,3 @@ | ||
if (obj.hasOwnProperty(key)) { | ||
values = values.concat(internals.stringify(obj[key], prefix + '[' + encodeURIComponent(key) + ']')); | ||
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']')); | ||
} | ||
@@ -50,3 +49,3 @@ } | ||
if (obj.hasOwnProperty(key)) { | ||
keys = keys.concat(internals.stringify(obj[key], encodeURIComponent(key))); | ||
keys = keys.concat(internals.stringify(obj[key], key)); | ||
} | ||
@@ -53,0 +52,0 @@ } |
@@ -118,3 +118,3 @@ // Load modules | ||
if (obj[key].hasOwnProperty(i) && | ||
obj[key][i]) { | ||
obj[key][i] !== null) { | ||
@@ -121,0 +121,0 @@ compacted[key].push(obj[key][i]); |
{ | ||
"name": "qs", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A querystring parser that supports nesting and arrays, with a depth limit", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/hapijs/qs", |
@@ -9,3 +9,3 @@ # qs | ||
The **qs** module was original created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring). | ||
The **qs** module was originally created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring). | ||
@@ -21,3 +21,3 @@ ## Usage | ||
### Objects | ||
### Parsing Objects | ||
@@ -35,2 +35,9 @@ **qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`. | ||
URI encoded strings work too: | ||
```javascript | ||
Qs.parse('a%5Bb%5D=c'); | ||
// { a: { b: 'c' } } | ||
``` | ||
You can also nest your objects, like `'foo[bar][baz]=foobarbaz'`: | ||
@@ -78,3 +85,3 @@ | ||
### Arrays | ||
### Parsing Arrays | ||
@@ -104,2 +111,11 @@ **qs** can also parse arrays using a similar `[]` notation: | ||
Note that an empty string is also a value, and will be preserved: | ||
```javascript | ||
Qs.parse('a[]=&a[]=b'); | ||
// { a: ['', 'b'] } | ||
Qs.parse('a[0]=b&a[1]=&a[2]=c'); | ||
// { a: ['b', '', 'c'] } | ||
``` | ||
**qs** will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will | ||
@@ -125,2 +141,36 @@ instead be converted to an object with the index as the key: | ||
// { a: [{ b: 'c' }] } | ||
``` | ||
``` | ||
### Stringifying | ||
When stringifying, **qs** always URI encodes output. Objects are stringified as you would expect: | ||
```javascript | ||
Qs.stringify({ a: 'b' }); | ||
// 'a=b' | ||
Qs.stringify({ a: { b: 'c' } }); | ||
// 'a%5Bb%5D=c' | ||
``` | ||
Examples beyond this point will be shown as though the output is not URI encoded for clarity. Please note that the return values in these cases *will* be URI encoded during real usage. | ||
When arrays are stringified, they are always given explicit indices: | ||
```javascript | ||
Qs.stringify({ a: ['b', 'c', 'd'] }); | ||
// 'a[0]=b&a[1]=c&a[2]=d' | ||
``` | ||
Empty strings and null values will omit the value, but the equals sign (=) remains in place: | ||
```javascript | ||
Qs.stringify({ a: '' }); | ||
// 'a=' | ||
``` | ||
Properties that are set to `undefined` will be omitted entirely: | ||
```javascript | ||
Qs.stringify({ a: null, b: undefined }); | ||
// 'a=' | ||
``` |
@@ -187,2 +187,9 @@ // Load modules | ||
it('allows for empty strings in arrays', function (done) { | ||
expect(Qs.parse('a[]=b&a[]=&a[]=c')).to.deep.equal({ a: ['b', '', 'c'] }); | ||
expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]=')).to.deep.equal({ a: ['b', '', 'c', ''] }); | ||
done(); | ||
}); | ||
it('should compact sparse arrays', function (done) { | ||
@@ -189,0 +196,0 @@ |
@@ -33,4 +33,4 @@ // Load modules | ||
expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a[b]=c'); | ||
expect(Qs.stringify({ a: { b: { c: { d: 'e' } } } })).to.equal('a[b][c][d]=e'); | ||
expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c'); | ||
expect(Qs.stringify({ a: { b: { c: { d: 'e' } } } })).to.equal('a%5Bb%5D%5Bc%5D%5Bd%5D=e'); | ||
done(); | ||
@@ -41,3 +41,3 @@ }); | ||
expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a[0]=b&a[1]=c&a[2]=d'); | ||
expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d'); | ||
done(); | ||
@@ -48,3 +48,3 @@ }); | ||
expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a[b][0]=c&a[b][1]=d'); | ||
expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d'); | ||
done(); | ||
@@ -55,4 +55,4 @@ }); | ||
expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a[0][b]=c'); | ||
expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a[0][b][c][0]=1'); | ||
expect(Qs.stringify({ a: [{ b: 'c' }] })).to.equal('a%5B0%5D%5Bb%5D=c'); | ||
expect(Qs.stringify({ a: [{ b: { c: [1] } }] })).to.equal('a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1'); | ||
done(); | ||
@@ -63,3 +63,3 @@ }); | ||
expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a[b]=c&a[d]=e'); | ||
expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e'); | ||
done(); | ||
@@ -72,4 +72,4 @@ }); | ||
expect(Qs.stringify({ a: '', b: '' })).to.equal('a=&b='); | ||
expect(Qs.stringify({ a: null })).to.equal('a'); | ||
expect(Qs.stringify({ a: { b: null } })).to.equal('a[b]'); | ||
expect(Qs.stringify({ a: null })).to.equal('a='); | ||
expect(Qs.stringify({ a: { b: null } })).to.equal('a%5Bb%5D='); | ||
done(); | ||
@@ -81,3 +81,3 @@ }); | ||
expect(Qs.stringify({ a: undefined })).to.equal(''); | ||
expect(Qs.stringify({ a: { b: undefined, c: null } })).to.equal('a[c]'); | ||
expect(Qs.stringify({ a: { b: undefined, c: null } })).to.equal('a%5Bc%5D='); | ||
done(); | ||
@@ -110,3 +110,3 @@ }); | ||
expect(Qs.stringify({ a: 'b'})).to.equal('a=b'); | ||
expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a[b]=c'); | ||
expect(Qs.stringify({ a: { b: 'c' } })).to.equal('a%5Bb%5D=c'); | ||
delete Object.prototype.crash; | ||
@@ -119,5 +119,5 @@ done(); | ||
expect(Qs.stringify({ a: true })).to.equal('a=true'); | ||
expect(Qs.stringify({ a: { b: true } })).to.equal('a[b]=true'); | ||
expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true'); | ||
expect(Qs.stringify({ b: false })).to.equal('b=false'); | ||
expect(Qs.stringify({ b: { c: false } })).to.equal('b[c]=false'); | ||
expect(Qs.stringify({ b: { c: false } })).to.equal('b%5Bc%5D=false'); | ||
done(); | ||
@@ -129,5 +129,5 @@ }); | ||
expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test'); | ||
expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a[b]=test'); | ||
expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test'); | ||
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
25413
505
171