From 211ba0fee27550af7d9baa70959f256ac38258f2 Mon Sep 17 00:00:00 2001 From: Nathan LaFreniere Date: Fri, 22 May 2015 10:55:59 -0700 Subject: [PATCH] use plain objects, closes #74 closes #80 --- README.md | 9 +- lib/parse.js | 17 ++-- lib/stringify.js | 2 +- lib/utils.js | 2 +- test/parse.js | 214 +++++++++++++++++++++++------------------------ 5 files changed, 118 insertions(+), 126 deletions(-) diff --git a/README.md b/README.md index 5431fec9..be68b10a 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ var str = Qs.stringify(obj); // 'a=c' Qs.parse(string, [options]); ``` -**qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`. +**qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`, or prefixing the sub-key with a dot `.`. For example, the string `'foo[bar]=baz'` converts to: ```javascript @@ -34,6 +34,13 @@ For example, the string `'foo[bar]=baz'` converts to: } ``` +The parsed value is returned as a plain object, created via `Object.create(null)` and as such you should be aware that prototype methods do not exist on it and a user may set those names to whatever value they like: + +```javascript +Qs.parse('a.hasOwnProperty=b'); +// { a: { hasOwnProperty: 'b' } } +``` + URI encoded strings work too: ```javascript diff --git a/lib/parse.js b/lib/parse.js index 8c0f5488..a9a564a2 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -55,12 +55,13 @@ internals.parseObject = function (chain, val, options) { var root = chain.shift(); - var obj = {}; + var obj; if (root === '[]') { obj = []; obj = obj.concat(internals.parseObject(chain, val, options)); } else { + obj = Object.create(null); var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root; var index = parseInt(cleanRoot, 10); var indexString = '' + index; @@ -101,12 +102,6 @@ internals.parseKeys = function (key, val, options) { var segment = parent.exec(key); - // Don't allow them to overwrite object prototype properties - - if (Object.prototype.hasOwnProperty(segment[1])) { - return; - } - // Stash the parent if it exists var keys = []; @@ -120,9 +115,7 @@ internals.parseKeys = function (key, val, options) { while ((segment = child.exec(key)) !== null && i < options.depth) { ++i; - if (!Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) { - keys.push(segment[1]); - } + keys.push(segment[1]); } // If there's a remainder, just add whatever is left @@ -141,7 +134,7 @@ module.exports = function (str, options) { str === null || typeof str === 'undefined') { - return {}; + return Object.create(null); } options = options || {}; @@ -153,7 +146,7 @@ module.exports = function (str, options) { var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str; - var obj = {}; + var obj = Object.create(null); // Iterate over the keys and setup the new object diff --git a/lib/stringify.js b/lib/stringify.js index b88ed0a3..fffa2ba2 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -38,7 +38,7 @@ internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHand } else if (obj === null) { if (strictNullHandling) { - return encodeURIComponent(prefix); + return Utils.fixedEncodeURIComponent(prefix); } obj = ''; diff --git a/lib/utils.js b/lib/utils.js index f92277fa..95e2dd98 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,7 +8,7 @@ var internals = {}; exports.arrayToObject = function (source) { - var obj = {}; + var obj = Object.create(null); for (var i = 0, il = source.length; i < il; ++i) { if (typeof source[i] !== 'undefined') { diff --git a/test/parse.js b/test/parse.js index c78291f2..c8f4ff57 100644 --- a/test/parse.js +++ b/test/parse.js @@ -23,195 +23,187 @@ describe('parse()', function () { it('parses a simple string', function (done) { - expect(Qs.parse('0=foo')).to.deep.equal({ '0': 'foo' }); - expect(Qs.parse('foo=c++')).to.deep.equal({ foo: 'c ' }); - expect(Qs.parse('a[>=]=23')).to.deep.equal({ a: { '>=': '23' } }); - expect(Qs.parse('a[<=>]==23')).to.deep.equal({ a: { '<=>': '=23' } }); - expect(Qs.parse('a[==]=23')).to.deep.equal({ a: { '==': '23' } }); - expect(Qs.parse('foo', {strictNullHandling: true})).to.deep.equal({ foo: null }); - expect(Qs.parse('foo' )).to.deep.equal({ foo: '' }); - expect(Qs.parse('foo=')).to.deep.equal({ foo: '' }); - expect(Qs.parse('foo=bar')).to.deep.equal({ foo: 'bar' }); - expect(Qs.parse(' foo = bar = baz ')).to.deep.equal({ ' foo ': ' bar = baz ' }); - expect(Qs.parse('foo=bar=baz')).to.deep.equal({ foo: 'bar=baz' }); - expect(Qs.parse('foo=bar&bar=baz')).to.deep.equal({ foo: 'bar', bar: 'baz' }); - expect(Qs.parse('foo2=bar2&baz2=')).to.deep.equal({ foo2: 'bar2', baz2: '' }); - expect(Qs.parse('foo=bar&baz', {strictNullHandling: true})).to.deep.equal({ foo: 'bar', baz: null }); - expect(Qs.parse('foo=bar&baz')).to.deep.equal({ foo: 'bar', baz: '' }); + expect(Qs.parse('0=foo')).to.deep.equal({ '0': 'foo' }, { prototype: false }); + expect(Qs.parse('foo=c++')).to.deep.equal({ foo: 'c ' }, { prototype: false }); + expect(Qs.parse('a[>=]=23')).to.deep.equal({ a: { '>=': '23' } }, { prototype: false }); + expect(Qs.parse('a[<=>]==23')).to.deep.equal({ a: { '<=>': '=23' } }, { prototype: false }); + expect(Qs.parse('a[==]=23')).to.deep.equal({ a: { '==': '23' } }, { prototype: false }); + expect(Qs.parse('foo', {strictNullHandling: true})).to.deep.equal({ foo: null }, { prototype: false }); + expect(Qs.parse('foo' )).to.deep.equal({ foo: '' }, { prototype: false }); + expect(Qs.parse('foo=')).to.deep.equal({ foo: '' }, { prototype: false }); + expect(Qs.parse('foo=bar')).to.deep.equal({ foo: 'bar' }, { prototype: false }); + expect(Qs.parse(' foo = bar = baz ')).to.deep.equal({ ' foo ': ' bar = baz ' }, { prototype: false }); + expect(Qs.parse('foo=bar=baz')).to.deep.equal({ foo: 'bar=baz' }, { prototype: false }); + expect(Qs.parse('foo=bar&bar=baz')).to.deep.equal({ foo: 'bar', bar: 'baz' }, { prototype: false }); + expect(Qs.parse('foo2=bar2&baz2=')).to.deep.equal({ foo2: 'bar2', baz2: '' }, { prototype: false }); + expect(Qs.parse('foo=bar&baz', {strictNullHandling: true})).to.deep.equal({ foo: 'bar', baz: null }, { prototype: false }); + expect(Qs.parse('foo=bar&baz')).to.deep.equal({ foo: 'bar', baz: '' }, { prototype: false }); expect(Qs.parse('cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World')).to.deep.equal({ cht: 'p3', chd: 't:60,40', chs: '250x100', chl: 'Hello|World' - }); + }, { prototype: false }); done(); }); it('parses a single nested string', function (done) { - expect(Qs.parse('a[b]=c')).to.deep.equal({ a: { b: 'c' } }); + expect(Qs.parse('a[b]=c')).to.deep.equal({ a: { b: 'c' } }, { prototype: false }); done(); }); it('parses a double nested string', function (done) { - expect(Qs.parse('a[b][c]=d')).to.deep.equal({ a: { b: { c: 'd' } } }); + expect(Qs.parse('a[b][c]=d')).to.deep.equal({ a: { b: { c: 'd' } } }, { prototype: false }); done(); }); it('defaults to a depth of 5', function (done) { - expect(Qs.parse('a[b][c][d][e][f][g][h]=i')).to.deep.equal({ a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } }); + expect(Qs.parse('a[b][c][d][e][f][g][h]=i')).to.deep.equal({ a: { b: { c: { d: { e: { f: { '[g][h]': 'i' } } } } } } }, { prototype: false }); done(); }); it('only parses one level when depth = 1', function (done) { - expect(Qs.parse('a[b][c]=d', { depth: 1 })).to.deep.equal({ a: { b: { '[c]': 'd' } } }); - expect(Qs.parse('a[b][c][d]=e', { depth: 1 })).to.deep.equal({ a: { b: { '[c][d]': 'e' } } }); + expect(Qs.parse('a[b][c]=d', { depth: 1 })).to.deep.equal({ a: { b: { '[c]': 'd' } } }, { prototype: false }); + expect(Qs.parse('a[b][c][d]=e', { depth: 1 })).to.deep.equal({ a: { b: { '[c][d]': 'e' } } }, { prototype: false }); done(); }); it('parses a simple array', function (done) { - expect(Qs.parse('a=b&a=c')).to.deep.equal({ a: ['b', 'c'] }); + expect(Qs.parse('a=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); done(); }); it('parses an explicit array', function (done) { - expect(Qs.parse('a[]=b')).to.deep.equal({ a: ['b'] }); - expect(Qs.parse('a[]=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }); - expect(Qs.parse('a[]=b&a[]=c&a[]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }); + expect(Qs.parse('a[]=b')).to.deep.equal({ a: ['b'] }, { prototype: false }); + expect(Qs.parse('a[]=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); + expect(Qs.parse('a[]=b&a[]=c&a[]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }, { prototype: false }); done(); }); it('parses a mix of simple and explicit arrays', function (done) { - expect(Qs.parse('a=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }); - expect(Qs.parse('a[]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }); - expect(Qs.parse('a[0]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }); - expect(Qs.parse('a=b&a[0]=c')).to.deep.equal({ a: ['b', 'c'] }); - expect(Qs.parse('a[1]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }); - expect(Qs.parse('a=b&a[1]=c')).to.deep.equal({ a: ['b', 'c'] }); + expect(Qs.parse('a=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); + expect(Qs.parse('a[]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); + expect(Qs.parse('a[0]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); + expect(Qs.parse('a=b&a[0]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); + expect(Qs.parse('a[1]=b&a=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); + expect(Qs.parse('a=b&a[1]=c')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); done(); }); it('parses a nested array', function (done) { - expect(Qs.parse('a[b][]=c&a[b][]=d')).to.deep.equal({ a: { b: ['c', 'd'] } }); - expect(Qs.parse('a[>=]=25')).to.deep.equal({ a: { '>=': '25' } }); + expect(Qs.parse('a[b][]=c&a[b][]=d')).to.deep.equal({ a: { b: ['c', 'd'] } }, { prototype: false }); + expect(Qs.parse('a[>=]=25')).to.deep.equal({ a: { '>=': '25' } }, { prototype: false }); done(); }); it('allows to specify array indices', function (done) { - expect(Qs.parse('a[1]=c&a[0]=b&a[2]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }); - expect(Qs.parse('a[1]=c&a[0]=b')).to.deep.equal({ a: ['b', 'c'] }); - expect(Qs.parse('a[1]=c')).to.deep.equal({ a: ['c'] }); + expect(Qs.parse('a[1]=c&a[0]=b&a[2]=d')).to.deep.equal({ a: ['b', 'c', 'd'] }, { prototype: false }); + expect(Qs.parse('a[1]=c&a[0]=b')).to.deep.equal({ a: ['b', 'c'] }, { prototype: false }); + expect(Qs.parse('a[1]=c')).to.deep.equal({ a: ['c'] }, { prototype: false }); done(); }); it('limits specific array indices to 20', function (done) { - expect(Qs.parse('a[20]=a')).to.deep.equal({ a: ['a'] }); - expect(Qs.parse('a[21]=a')).to.deep.equal({ a: { '21': 'a' } }); + expect(Qs.parse('a[20]=a')).to.deep.equal({ a: ['a'] }, { prototype: false }); + expect(Qs.parse('a[21]=a')).to.deep.equal({ a: { '21': 'a' } }, { prototype: false }); done(); }); it('supports keys that begin with a number', function (done) { - expect(Qs.parse('a[12b]=c')).to.deep.equal({ a: { '12b': 'c' } }); + expect(Qs.parse('a[12b]=c')).to.deep.equal({ a: { '12b': 'c' } }, { prototype: false }); done(); }); it('supports encoded = signs', function (done) { - expect(Qs.parse('he%3Dllo=th%3Dere')).to.deep.equal({ 'he=llo': 'th=ere' }); + expect(Qs.parse('he%3Dllo=th%3Dere')).to.deep.equal({ 'he=llo': 'th=ere' }, { prototype: false }); done(); }); it('is ok with url encoded strings', function (done) { - expect(Qs.parse('a[b%20c]=d')).to.deep.equal({ a: { 'b c': 'd' } }); - expect(Qs.parse('a[b]=c%20d')).to.deep.equal({ a: { b: 'c d' } }); + expect(Qs.parse('a[b%20c]=d')).to.deep.equal({ a: { 'b c': 'd' } }, { prototype: false }); + expect(Qs.parse('a[b]=c%20d')).to.deep.equal({ a: { b: 'c d' } }, { prototype: false }); done(); }); it('allows brackets in the value', function (done) { - expect(Qs.parse('pets=["tobi"]')).to.deep.equal({ pets: '["tobi"]' }); - expect(Qs.parse('operators=[">=", "<="]')).to.deep.equal({ operators: '[">=", "<="]' }); + expect(Qs.parse('pets=["tobi"]')).to.deep.equal({ pets: '["tobi"]' }, { prototype: false }); + expect(Qs.parse('operators=[">=", "<="]')).to.deep.equal({ operators: '[">=", "<="]' }, { prototype: false }); done(); }); it('allows empty values', function (done) { - expect(Qs.parse('')).to.deep.equal({}); - expect(Qs.parse(null)).to.deep.equal({}); - expect(Qs.parse(undefined)).to.deep.equal({}); + expect(Qs.parse('')).to.deep.equal({}, { prototype: false }); + expect(Qs.parse(null)).to.deep.equal({}, { prototype: false }); + expect(Qs.parse(undefined)).to.deep.equal({}, { prototype: false }); done(); }); it('transforms arrays to objects', function (done) { - expect(Qs.parse('foo[0]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }); - expect(Qs.parse('foo[bad]=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); - expect(Qs.parse('foo[bad]=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); - expect(Qs.parse('foo[]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }); - 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'} ]}); + expect(Qs.parse('foo[0]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }, { prototype: false }); + expect(Qs.parse('foo[bad]=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false }); + expect(Qs.parse('foo[bad]=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false }); + expect(Qs.parse('foo[]=bar&foo[bad]=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }, { prototype: false }); + expect(Qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } }, { prototype: false }); + 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'} ]}, { prototype: false }); + expect(Qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c')).to.deep.equal({ a: { '0': 'b', t: 'u', hasOwnProperty: 'c' } }, { prototype: false }); + expect(Qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y')).to.deep.equal({ a: { '0': 'b', hasOwnProperty: 'c', x: 'y' } }, { prototype: false }); done(); }); it('transforms arrays to objects (dot notation)', function (done) { - expect(Qs.parse('foo[0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: 'baz' } }); - expect(Qs.parse('foo[0].baz=bar&fool.bad.boo=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: { boo: 'baz' } } }); - expect(Qs.parse('foo[0][0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [[ { baz: 'bar'} ]], fool: { bad: 'baz' } }); - expect(Qs.parse('foo[0].baz[0]=15&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15'], bar: '2' }] }); - expect(Qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15', '16'], bar: '2' }] }); - expect(Qs.parse('foo.bad=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); - expect(Qs.parse('foo.bad=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }); - expect(Qs.parse('foo[]=bar&foo.bad=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }); - 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'} ]}); + expect(Qs.parse('foo[0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: 'baz' } }, { prototype: false }); + expect(Qs.parse('foo[0].baz=bar&fool.bad.boo=baz')).to.deep.equal({ foo: [ { baz: 'bar'} ], fool: { bad: { boo: 'baz' } } }, { prototype: false }); + expect(Qs.parse('foo[0][0].baz=bar&fool.bad=baz')).to.deep.equal({ foo: [[ { baz: 'bar'} ]], fool: { bad: 'baz' } }, { prototype: false }); + expect(Qs.parse('foo[0].baz[0]=15&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15'], bar: '2' }] }, { prototype: false }); + expect(Qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2')).to.deep.equal({ foo: [{ baz: ['15', '16'], bar: '2' }] }, { prototype: false }); + expect(Qs.parse('foo.bad=baz&foo[0]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false }); + expect(Qs.parse('foo.bad=baz&foo[]=bar')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar' } }, { prototype: false }); + expect(Qs.parse('foo[]=bar&foo.bad=baz')).to.deep.equal({ foo: { '0': 'bar', bad: 'baz' } }, { prototype: false }); + expect(Qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo')).to.deep.equal({ foo: { bad: 'baz', '0': 'bar', '1': 'foo' } }, { prototype: false }); + 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'} ]}, { prototype: false }); done(); }); it('can add keys to objects', function (done) { - expect(Qs.parse('a[b]=c&a=d')).to.deep.equal({ a: { b: 'c', d: true } }); + expect(Qs.parse('a[b]=c&a=d')).to.deep.equal({ a: { b: 'c', d: true } }, { prototype: false }); done(); }); it('correctly prunes undefined values when converting an array to an object', function (done) { - expect(Qs.parse('a[2]=b&a[99999999]=c')).to.deep.equal({ a: { '2': 'b', '99999999': 'c' } }); + expect(Qs.parse('a[2]=b&a[99999999]=c')).to.deep.equal({ a: { '2': 'b', '99999999': 'c' } }, { prototype: false }); done(); }); it('supports malformed uri characters', function (done) { - expect(Qs.parse('{%:%}', {strictNullHandling: true})).to.deep.equal({ '{%:%}': null }); - expect(Qs.parse('{%:%}=')).to.deep.equal({ '{%:%}': '' }); - expect(Qs.parse('foo=%:%}')).to.deep.equal({ foo: '%:%}' }); + expect(Qs.parse('{%:%}', {strictNullHandling: true})).to.deep.equal({ '{%:%}': null }, { prototype: false }); + expect(Qs.parse('{%:%}=')).to.deep.equal({ '{%:%}': '' }, { prototype: false }); + expect(Qs.parse('foo=%:%}')).to.deep.equal({ foo: '%:%}' }, { prototype: false }); done(); }); it('doesn\'t produce empty keys', function (done) { - expect(Qs.parse('_r=1&')).to.deep.equal({ '_r': '1' }); - done(); - }); - - it('cannot override prototypes', function (done) { - - var obj = Qs.parse('hasOwnProperty=bad&toString=bad&bad[toString]=bad&constructor=bad'); - expect(typeof obj.toString).to.equal('function'); - expect(typeof obj.bad.toString).to.equal('function'); - expect(typeof obj.constructor).to.equal('function'); - expect(typeof obj.hasOwnProperty).to.equal('function'); + expect(Qs.parse('_r=1&')).to.deep.equal({ '_r': '1' }, { prototype: false }); done(); }); @@ -225,45 +217,45 @@ describe('parse()', function () { it('parses arrays of objects', function (done) { - expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }); - expect(Qs.parse('a[0][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }); + expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }, { prototype: false }); + expect(Qs.parse('a[0][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }, { prototype: false }); done(); }); 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]=', {strictNullHandling: true})).to.deep.equal({ a: ['b', null, 'c', ''] }); - expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', {strictNullHandling: true})).to.deep.equal({ a: ['b', '', 'c', null] }); - expect(Qs.parse('a[]=&a[]=b&a[]=c')).to.deep.equal({ a: ['', 'b', 'c'] }); + expect(Qs.parse('a[]=b&a[]=&a[]=c')).to.deep.equal({ a: ['b', '', 'c'] }, { prototype: false }); + expect(Qs.parse('a[0]=b&a[1]&a[2]=c&a[19]=', {strictNullHandling: true})).to.deep.equal({ a: ['b', null, 'c', ''] }, { prototype: false }); + expect(Qs.parse('a[0]=b&a[1]=&a[2]=c&a[19]', {strictNullHandling: true})).to.deep.equal({ a: ['b', '', 'c', null] }, { prototype: false }); + expect(Qs.parse('a[]=&a[]=b&a[]=c')).to.deep.equal({ a: ['', 'b', 'c'] }, { prototype: false }); done(); }); it('compacts sparse arrays', function (done) { - expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] }); + expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] }, { prototype: false }); done(); }); it('parses semi-parsed strings', function (done) { - expect(Qs.parse({ 'a[b]': 'c' })).to.deep.equal({ a: { b: 'c' } }); - expect(Qs.parse({ 'a[b]': 'c', 'a[d]': 'e' })).to.deep.equal({ a: { b: 'c', d: 'e' } }); + expect(Qs.parse({ 'a[b]': 'c' })).to.deep.equal({ a: { b: 'c' } }, { prototype: false }); + expect(Qs.parse({ 'a[b]': 'c', 'a[d]': 'e' })).to.deep.equal({ a: { b: 'c', d: 'e' } }, { prototype: false }); done(); }); it('parses buffers correctly', function (done) { var b = new Buffer('test'); - expect(Qs.parse({ a: b })).to.deep.equal({ a: b }); + expect(Qs.parse({ a: b })).to.deep.equal({ a: b }, { prototype: false }); done(); }); it('continues parsing when no parent is found', function (done) { - expect(Qs.parse('[]=&a=b')).to.deep.equal({ '0': '', a: 'b' }); - expect(Qs.parse('[]&a=b', {strictNullHandling: true})).to.deep.equal({ '0': null, a: 'b' }); - expect(Qs.parse('[foo]=bar')).to.deep.equal({ foo: 'bar' }); + expect(Qs.parse('[]=&a=b')).to.deep.equal({ '0': '', a: 'b' }, { prototype: false }); + expect(Qs.parse('[]&a=b', {strictNullHandling: true})).to.deep.equal({ '0': null, a: 'b' }, { prototype: false }); + expect(Qs.parse('[foo]=bar')).to.deep.equal({ foo: 'bar' }, { prototype: false }); done(); }); @@ -287,9 +279,9 @@ describe('parse()', function () { Object.prototype.crash = ''; 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('a=b')).to.deep.equal({ a: 'b' }, { prototype: false }); expect(Qs.parse.bind(null, 'a[][b]=c')).to.not.throw(); - expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }); + expect(Qs.parse('a[][b]=c')).to.deep.equal({ a: [{ b: 'c' }] }, { prototype: false }); delete Object.prototype.crash; delete Array.prototype.crash; done(); @@ -297,39 +289,39 @@ describe('parse()', function () { it('parses a string with an alternative string delimiter', function (done) { - expect(Qs.parse('a=b;c=d', { delimiter: ';' })).to.deep.equal({ a: 'b', c: 'd' }); + expect(Qs.parse('a=b;c=d', { delimiter: ';' })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false }); done(); }); it('parses a string with an alternative RegExp delimiter', function (done) { - expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' }); + expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false }); done(); }); it('does not use non-splittable objects as delimiters', function (done) { - expect(Qs.parse('a=b&c=d', { delimiter: true })).to.deep.equal({ a: 'b', c: 'd' }); + expect(Qs.parse('a=b&c=d', { delimiter: true })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false }); done(); }); it('allows overriding parameter limit', function (done) { - expect(Qs.parse('a=b&c=d', { parameterLimit: 1 })).to.deep.equal({ a: 'b' }); + expect(Qs.parse('a=b&c=d', { parameterLimit: 1 })).to.deep.equal({ a: 'b' }, { prototype: false }); done(); }); it('allows setting the parameter limit to Infinity', function (done) { - expect(Qs.parse('a=b&c=d', { parameterLimit: Infinity })).to.deep.equal({ a: 'b', c: 'd' }); + expect(Qs.parse('a=b&c=d', { parameterLimit: Infinity })).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false }); done(); }); it('allows overriding array limit', function (done) { - expect(Qs.parse('a[0]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '0': 'b' } }); - expect(Qs.parse('a[-1]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '-1': 'b' } }); - expect(Qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 })).to.deep.equal({ a: { '0': 'b', '1': 'c' } }); + expect(Qs.parse('a[0]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '0': 'b' } }, { prototype: false }); + expect(Qs.parse('a[-1]=b', { arrayLimit: -1 })).to.deep.equal({ a: { '-1': 'b' } }, { prototype: false }); + expect(Qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 })).to.deep.equal({ a: { '0': 'b', '1': 'c' } }, { prototype: false }); done(); }); @@ -349,7 +341,7 @@ describe('parse()', function () { var result = Qs.parse(input); - expect(result).to.deep.equal(expected); + expect(result).to.deep.equal(expected, { prototype: false }); done(); }); @@ -369,7 +361,7 @@ describe('parse()', function () { var result = Qs.parse(input); - expect(result).to.deep.equal(expected); + expect(result).to.deep.equal(expected, { prototype: false }); done(); }); @@ -389,7 +381,7 @@ describe('parse()', function () { var result = Qs.parse(input); - expect(result).to.deep.equal(expected); + expect(result).to.deep.equal(expected, { prototype: false }); done(); }); @@ -399,7 +391,7 @@ describe('parse()', function () { delete global.Buffer; var result = Qs.parse('a=b&c=d'); global.Buffer = tempBuffer; - expect(result).to.deep.equal({ a: 'b', c: 'd' }); + expect(result).to.deep.equal({ a: 'b', c: 'd' }, { prototype: false }); done(); }); @@ -418,7 +410,7 @@ describe('parse()', function () { expect(parsed).to.contain('foo'); expect(parsed.foo).to.contain('bar', 'baz'); expect(parsed.foo.bar).to.equal('baz'); - expect(parsed.foo.baz).to.deep.equal(a); + expect(parsed.foo.baz).to.deep.equal(a, { prototype: false }); done(); }); @@ -427,24 +419,24 @@ describe('parse()', function () { var a = Object.create(null); a.b = 'c'; - expect(Qs.parse(a)).to.deep.equal({ b: 'c' }); + expect(Qs.parse(a)).to.deep.equal({ b: 'c' }, { prototype: false }); var result = Qs.parse({ a: a }); expect(result).to.contain('a'); - expect(result.a).to.deep.equal(a); + expect(result.a).to.deep.equal(a, { prototype: false }); done(); }); it('parses dates correctly', function (done) { var now = new Date(); - expect(Qs.parse({ a: now })).to.deep.equal({ a: now }); + expect(Qs.parse({ a: now })).to.deep.equal({ a: now }, { prototype: false }); done(); }); it('parses regular expressions correctly', function (done) { var re = /^test$/; - expect(Qs.parse({ a: re })).to.deep.equal({ a: re }); + expect(Qs.parse({ a: re })).to.deep.equal({ a: re }, { prototype: false }); done(); }); });