diff --git a/.travis.yml b/.travis.yml index 335fded1..77127208 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: node_js node_js: - - 0.10 - 4.0 - 4 + - 5 sudo: false diff --git a/lib/index.js b/lib/index.js old mode 100644 new mode 100755 index 0e094933..8ca9c30e --- a/lib/index.js +++ b/lib/index.js @@ -1,15 +1,15 @@ +'use strict'; + // Load modules -var Stringify = require('./stringify'); -var Parse = require('./parse'); +const Stringify = require('./stringify'); +const Parse = require('./parse'); // Declare internals -var internals = {}; +const internals = {}; -module.exports = { - stringify: Stringify, - parse: Parse -}; +exports.stringify = Stringify; +exports.parse = Parse; diff --git a/lib/parse.js b/lib/parse.js old mode 100644 new mode 100755 index 4a2137ef..802b4593 --- a/lib/parse.js +++ b/lib/parse.js @@ -1,11 +1,13 @@ +'use strict'; + // Load modules -var Utils = require('./utils'); +const Utils = require('./utils'); // Declare internals -var internals = { +const internals = { delimiter: '&', depth: 5, arrayLimit: 20, @@ -19,12 +21,12 @@ var internals = { internals.parseValues = function (str, options) { - var obj = {}; - var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit); + const obj = {}; + const parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit); - for (var i = 0, il = parts.length; i < il; ++i) { - var part = parts[i]; - var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1; + for (let i = 0; i < parts.length; ++i) { + const part = parts[i]; + const pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1; if (pos === -1) { obj[Utils.decode(part)] = ''; @@ -34,8 +36,8 @@ internals.parseValues = function (str, options) { } } else { - var key = Utils.decode(part.slice(0, pos)); - var val = Utils.decode(part.slice(pos + 1)); + const key = Utils.decode(part.slice(0, pos)); + const val = Utils.decode(part.slice(pos + 1)); if (!Object.prototype.hasOwnProperty.call(obj, key)) { obj[key] = val; @@ -56,18 +58,18 @@ internals.parseObject = function (chain, val, options) { return val; } - var root = chain.shift(); + const root = chain.shift(); - var obj; + let obj; if (root === '[]') { obj = []; obj = obj.concat(internals.parseObject(chain, val, options)); } else { obj = options.plainObjects ? 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; + const cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root; + const index = parseInt(cleanRoot, 10); + const indexString = '' + index; if (!isNaN(index) && root !== cleanRoot && indexString === cleanRoot && @@ -101,16 +103,16 @@ internals.parseKeys = function (key, val, options) { // The regex chunks - var parent = /^([^\[\]]*)/; - var child = /(\[[^\[\]]*\])/g; + const parent = /^([^\[\]]*)/; + const child = /(\[[^\[\]]*\])/g; // Get the parent - var segment = parent.exec(key); + let segment = parent.exec(key); // Stash the parent if it exists - var keys = []; + const keys = []; if (segment[1]) { // If we aren't using plain objects, optionally prefix keys // that would overwrite object prototype properties @@ -127,7 +129,7 @@ internals.parseKeys = function (key, val, options) { // Loop through children appending to the array until we hit depth - var i = 0; + let i = 0; while ((segment = child.exec(key)) !== null && i < options.depth) { ++i; @@ -171,15 +173,15 @@ module.exports = function (str, options) { return options.plainObjects ? Object.create(null) : {}; } - var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str; - var obj = options.plainObjects ? Object.create(null) : {}; + const tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str; + let obj = options.plainObjects ? Object.create(null) : {}; // Iterate over the keys and setup the new object - var keys = Object.keys(tempObj); - for (var i = 0, il = keys.length; i < il; ++i) { - var key = keys[i]; - var newObj = internals.parseKeys(key, tempObj[key], options); + const keys = Object.keys(tempObj); + for (let i = 0; i < keys.length; ++i) { + const key = keys[i]; + const newObj = internals.parseKeys(key, tempObj[key], options); obj = Utils.merge(obj, newObj, options); } diff --git a/lib/stringify.js b/lib/stringify.js old mode 100644 new mode 100755 index d05aa875..e71fc9a8 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -1,11 +1,13 @@ +'use strict'; + // Load modules -var Utils = require('./utils'); +const Utils = require('./utils'); // Declare internals -var internals = { +const internals = { delimiter: '&', arrayPrefixGenerators: { brackets: function (prefix, key) { @@ -56,22 +58,23 @@ internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHand return [prefix + '=' + obj]; } - var values = []; + let values = []; if (typeof obj === 'undefined') { return values; } - var objKeys; + let objKeys; if (Array.isArray(filter)) { objKeys = filter; - } else { - var keys = Object.keys(obj); + } + else { + const keys = Object.keys(obj); objKeys = sort ? keys.sort(sort) : keys; } - for (var i = 0, il = objKeys.length; i < il; ++i) { - var key = objKeys[i]; + for (let i = 0; i < objKeys.length; ++i) { + const key = objKeys[i]; if (skipNulls && obj[key] === null) { @@ -94,13 +97,13 @@ internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHand module.exports = function (obj, options) { options = options || {}; - var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter; - var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling; - var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : internals.skipNulls; - var encode = typeof options.encode === 'boolean' ? options.encode : internals.encode; - var sort = typeof options.sort === 'function' ? options.sort : null; - var objKeys; - var filter; + const delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter; + const strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling; + const skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : internals.skipNulls; + const encode = typeof options.encode === 'boolean' ? options.encode : internals.encode; + const sort = typeof options.sort === 'function' ? options.sort : null; + let objKeys; + let filter; if (typeof options.filter === 'function') { filter = options.filter; obj = filter('', obj); @@ -109,7 +112,7 @@ module.exports = function (obj, options) { objKeys = filter = options.filter; } - var keys = []; + let keys = []; if (typeof obj !== 'object' || obj === null) { @@ -117,7 +120,7 @@ module.exports = function (obj, options) { return ''; } - var arrayFormat; + let arrayFormat; if (options.arrayFormat in internals.arrayPrefixGenerators) { arrayFormat = options.arrayFormat; } @@ -128,7 +131,7 @@ module.exports = function (obj, options) { arrayFormat = 'indices'; } - var generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat]; + const generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat]; if (!objKeys) { objKeys = Object.keys(obj); @@ -138,8 +141,8 @@ module.exports = function (obj, options) { objKeys.sort(sort); } - for (var i = 0, il = objKeys.length; i < il; ++i) { - var key = objKeys[i]; + for (let i = 0; i < objKeys.length; ++i) { + const key = objKeys[i]; if (skipNulls && obj[key] === null) { diff --git a/lib/utils.js b/lib/utils.js old mode 100644 new mode 100755 index 88f31473..ff84c5f3 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,21 +1,29 @@ +'use strict'; + // Load modules // Declare internals -var internals = {}; -internals.hexTable = new Array(256); -for (var h = 0; h < 256; ++h) { - internals.hexTable[h] = '%' + ((h < 16 ? '0' : '') + h.toString(16)).toUpperCase(); -} +const internals = {}; + + +internals.hexTable = function () { + + const array = new Array(256); + for (let i = 0; i < 256; ++i) { + array[i] = '%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase(); + } + + return array; +}(); exports.arrayToObject = function (source, options) { - var obj = options.plainObjects ? Object.create(null) : {}; - for (var i = 0, il = source.length; i < il; ++i) { + const obj = options.plainObjects ? Object.create(null) : {}; + for (let i = 0; i < source.length; ++i) { if (typeof source[i] !== 'undefined') { - obj[i] = source[i]; } } @@ -55,10 +63,10 @@ exports.merge = function (target, source, options) { target = exports.arrayToObject(target, options); } - var keys = Object.keys(source); - for (var k = 0, kl = keys.length; k < kl; ++k) { - var key = keys[k]; - var value = source[key]; + const keys = Object.keys(source); + for (let i = 0; i < keys.length; ++i) { + const key = keys[i]; + const value = source[key]; if (!Object.prototype.hasOwnProperty.call(target, key)) { target[key] = value; @@ -76,7 +84,8 @@ exports.decode = function (str) { try { return decodeURIComponent(str.replace(/\+/g, ' ')); - } catch (e) { + } + catch (e) { return str; } }; @@ -93,9 +102,9 @@ exports.encode = function (str) { str = '' + str; } - var out = ''; - for (var i = 0, il = str.length; i < il; ++i) { - var c = str.charCodeAt(i); + let out = ''; + for (let i = 0; i < str.length; ++i) { + let c = str.charCodeAt(i); if (c === 0x2D || // - c === 0x2E || // . @@ -105,28 +114,28 @@ exports.encode = function (str) { (c >= 0x41 && c <= 0x5A) || // a-z (c >= 0x61 && c <= 0x7A)) { // A-Z - out += str[i]; + out = out + str[i]; continue; } if (c < 0x80) { - out += internals.hexTable[c]; + out = out + internals.hexTable[c]; continue; } if (c < 0x800) { - out += internals.hexTable[0xC0 | (c >> 6)] + internals.hexTable[0x80 | (c & 0x3F)]; + out = out + (internals.hexTable[0xC0 | (c >> 6)] + internals.hexTable[0x80 | (c & 0x3F)]); continue; } if (c < 0xD800 || c >= 0xE000) { - out += internals.hexTable[0xE0 | (c >> 12)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)]; + out = out + (internals.hexTable[0xE0 | (c >> 12)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)]); continue; } ++i; c = 0x10000 + (((c & 0x3FF) << 10) | (str.charCodeAt(i) & 0x3FF)); - out += internals.hexTable[0xF0 | (c >> 18)] + internals.hexTable[0x80 | ((c >> 12) & 0x3F)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)]; + out = out + (internals.hexTable[0xF0 | (c >> 18)] + internals.hexTable[0x80 | ((c >> 12) & 0x3F)] + internals.hexTable[0x80 | ((c >> 6) & 0x3F)] + internals.hexTable[0x80 | (c & 0x3F)]); } return out; @@ -141,7 +150,7 @@ exports.compact = function (obj, refs) { } refs = refs || []; - var lookup = refs.indexOf(obj); + const lookup = refs.indexOf(obj); if (lookup !== -1) { return refs[lookup]; } @@ -149,9 +158,9 @@ exports.compact = function (obj, refs) { refs.push(obj); if (Array.isArray(obj)) { - var compacted = []; + const compacted = []; - for (var i = 0, il = obj.length; i < il; ++i) { + for (let i = 0; i < obj.length; ++i) { if (typeof obj[i] !== 'undefined') { compacted.push(obj[i]); } @@ -160,9 +169,9 @@ exports.compact = function (obj, refs) { return compacted; } - var keys = Object.keys(obj); - for (i = 0, il = keys.length; i < il; ++i) { - var key = keys[i]; + const keys = Object.keys(obj); + for (let i = 0; i < keys.length; ++i) { + const key = keys[i]; obj[key] = exports.compact(obj[key], refs); } diff --git a/package.json b/package.json index 0f525135..fc428c1f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "qs", "description": "A querystring parser that supports nesting and arrays, with a depth limit", "homepage": "https://github.com/hapijs/qs", - "version": "5.2.0", + "version": "6.0.0", "repository": { "type": "git", "url": "https://github.com/hapijs/qs.git" @@ -12,12 +12,12 @@ "querystring", "qs" ], - "engines": ">=0.10.40", + "engines": ">=4.0.0", "dependencies": {}, "devDependencies": { "browserify": "^10.2.1", - "code": "1.x.x", - "lab": "5.x.x" + "code": "2.x.x", + "lab": "7.x.x" }, "scripts": { "test": "lab -a code -t 100 -L", diff --git a/test/parse.js b/test/parse.js old mode 100644 new mode 100755 index 679f1974..8df8b1e0 --- a/test/parse.js +++ b/test/parse.js @@ -1,27 +1,29 @@ +'use strict'; + /* eslint no-extend-native:0 */ // Load modules -var Code = require('code'); -var Lab = require('lab'); -var Qs = require('../'); +const Code = require('code'); +const Lab = require('lab'); +const Qs = require('../'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var expect = Code.expect; -var describe = lab.experiment; -var it = lab.test; +const lab = exports.lab = Lab.script(); +const expect = Code.expect; +const describe = lab.experiment; +const it = lab.test; -describe('parse()', function () { +describe('parse()', () => { - it('parses a simple string', function (done) { + it('parses a simple string', (done) => { expect(Qs.parse('0=foo')).to.deep.equal({ '0': 'foo' }); expect(Qs.parse('foo=c++')).to.deep.equal({ foo: 'c ' }); @@ -29,7 +31,7 @@ describe('parse()', function () { 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=')).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 ' }); @@ -47,45 +49,45 @@ describe('parse()', function () { done(); }); - it('allows enabling dot notation', function (done) { + it('allows enabling dot notation', (done) => { expect(Qs.parse('a.b=c')).to.deep.equal({ 'a.b': 'c' }); expect(Qs.parse('a.b=c', { allowDots: true })).to.deep.equal({ a: { b: 'c' } }); done(); }); - it('parses a single nested string', function (done) { + it('parses a single nested string', (done) => { expect(Qs.parse('a[b]=c')).to.deep.equal({ a: { b: 'c' } }); done(); }); - it('parses a double nested string', function (done) { + it('parses a double nested string', (done) => { expect(Qs.parse('a[b][c]=d')).to.deep.equal({ a: { b: { c: 'd' } } }); done(); }); - it('defaults to a depth of 5', function (done) { + it('defaults to a depth of 5', (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' } } } } } } }); done(); }); - it('only parses one level when depth = 1', function (done) { + it('only parses one level when depth = 1', (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' } } }); done(); }); - it('parses a simple array', function (done) { + it('parses a simple array', (done) => { expect(Qs.parse('a=b&a=c')).to.deep.equal({ a: ['b', 'c'] }); done(); }); - it('parses an explicit array', function (done) { + it('parses an explicit array', (done) => { expect(Qs.parse('a[]=b')).to.deep.equal({ a: ['b'] }); expect(Qs.parse('a[]=b&a[]=c')).to.deep.equal({ a: ['b', 'c'] }); @@ -93,7 +95,7 @@ describe('parse()', function () { done(); }); - it('parses a mix of simple and explicit arrays', function (done) { + it('parses a mix of simple and explicit arrays', (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'] }); @@ -104,14 +106,14 @@ describe('parse()', function () { done(); }); - it('parses a nested array', function (done) { + it('parses a nested array', (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' } }); done(); }); - it('allows to specify array indices', function (done) { + it('allows to specify array indices', (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'] }); @@ -119,40 +121,40 @@ describe('parse()', function () { done(); }); - it('limits specific array indices to 20', function (done) { + it('limits specific array indices to 20', (done) => { expect(Qs.parse('a[20]=a')).to.deep.equal({ a: ['a'] }); expect(Qs.parse('a[21]=a')).to.deep.equal({ a: { '21': 'a' } }); done(); }); - it('supports keys that begin with a number', function (done) { + it('supports keys that begin with a number', (done) => { expect(Qs.parse('a[12b]=c')).to.deep.equal({ a: { '12b': 'c' } }); done(); }); - it('supports encoded = signs', function (done) { + it('supports encoded = signs', (done) => { expect(Qs.parse('he%3Dllo=th%3Dere')).to.deep.equal({ 'he=llo': 'th=ere' }); done(); }); - it('is ok with url encoded strings', function (done) { + it('is ok with url encoded strings', (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' } }); done(); }); - it('allows brackets in the value', function (done) { + it('allows brackets in the value', (done) => { expect(Qs.parse('pets=["tobi"]')).to.deep.equal({ pets: '["tobi"]' }); expect(Qs.parse('operators=[">=", "<="]')).to.deep.equal({ operators: '[">=", "<="]' }); done(); }); - it('allows empty values', function (done) { + it('allows empty values', (done) => { expect(Qs.parse('')).to.deep.equal({}); expect(Qs.parse(null)).to.deep.equal({}); @@ -160,7 +162,7 @@ describe('parse()', function () { done(); }); - it('transforms arrays to objects', function (done) { + it('transforms arrays to objects', (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' } }); @@ -173,7 +175,7 @@ describe('parse()', function () { done(); }); - it('transforms arrays to objects (dot notation)', function (done) { + it('transforms arrays to objects (dot notation)', (done) => { expect(Qs.parse('foo[0].baz=bar&fool.bad=baz', { allowDots: true })).to.deep.equal({ foo: [{ baz: 'bar' }], fool: { bad: 'baz' } }); expect(Qs.parse('foo[0].baz=bar&fool.bad.boo=baz', { allowDots: true })).to.deep.equal({ foo: [{ baz: 'bar' }], fool: { bad: { boo: 'baz' } } }); @@ -188,19 +190,19 @@ describe('parse()', function () { done(); }); - it('can add keys to objects', function (done) { + it('can add keys to objects', (done) => { expect(Qs.parse('a[b]=c&a=d')).to.deep.equal({ a: { b: 'c', d: true } }); done(); }); - it('correctly prunes undefined values when converting an array to an object', function (done) { + it('correctly prunes undefined values when converting an array to an object', (done) => { expect(Qs.parse('a[2]=b&a[99999999]=c')).to.deep.equal({ a: { '2': 'b', '99999999': 'c' } }); done(); }); - it('supports malformed uri characters', function (done) { + it('supports malformed uri characters', (done) => { expect(Qs.parse('{%:%}', { strictNullHandling: true })).to.deep.equal({ '{%:%}': null }); expect(Qs.parse('{%:%}=')).to.deep.equal({ '{%:%}': '' }); @@ -208,13 +210,13 @@ describe('parse()', function () { done(); }); - it('doesn\'t produce empty keys', function (done) { + it('doesn\'t produce empty keys', (done) => { expect(Qs.parse('_r=1&')).to.deep.equal({ '_r': '1' }); done(); }); - it('cannot access Object prototype', function (done) { + it('cannot access Object prototype', (done) => { Qs.parse('constructor[prototype][bad]=bad'); Qs.parse('bad[constructor][prototype][bad]=bad'); @@ -222,14 +224,14 @@ describe('parse()', function () { done(); }); - it('parses arrays of objects', function (done) { + it('parses arrays of objects', (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' }] }); done(); }); - it('allows for empty strings in arrays', function (done) { + it('allows for empty strings in arrays', (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', ''] }); @@ -238,27 +240,27 @@ describe('parse()', function () { done(); }); - it('compacts sparse arrays', function (done) { + it('compacts sparse arrays', (done) => { expect(Qs.parse('a[10]=1&a[2]=2')).to.deep.equal({ a: ['2', '1'] }); done(); }); - it('parses semi-parsed strings', function (done) { + it('parses semi-parsed strings', (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' } }); done(); }); - it('parses buffers correctly', function (done) { + it('parses buffers correctly', (done) => { - var b = new Buffer('test'); + const b = new Buffer('test'); expect(Qs.parse({ a: b })).to.deep.equal({ a: b }); done(); }); - it('continues parsing when no parent is found', function (done) { + it('continues parsing when no parent is found', (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' }); @@ -266,14 +268,14 @@ describe('parse()', function () { done(); }); - it('does not error when parsing a very long array', function (done) { + it('does not error when parsing a very long array', (done) => { - var str = 'a[]=a'; + let str = 'a[]=a'; while (Buffer.byteLength(str) < 128 * 1024) { - str += '&' + str; + str = str + '&' + str; } - expect(function () { + expect(() => { Qs.parse(str); }).to.not.throw(); @@ -281,7 +283,7 @@ describe('parse()', function () { done(); }); - it('should not throw when a native prototype has an enumerable property', { parallel: false }, function (done) { + it('should not throw when a native prototype has an enumerable property', { parallel: false }, (done) => { Object.prototype.crash = ''; Array.prototype.crash = ''; @@ -294,37 +296,37 @@ describe('parse()', function () { done(); }); - it('parses a string with an alternative string delimiter', function (done) { + it('parses a string with an alternative string delimiter', (done) => { expect(Qs.parse('a=b;c=d', { delimiter: ';' })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); - it('parses a string with an alternative RegExp delimiter', function (done) { + it('parses a string with an alternative RegExp delimiter', (done) => { expect(Qs.parse('a=b; c=d', { delimiter: /[;,] */ })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); - it('does not use non-splittable objects as delimiters', function (done) { + it('does not use non-splittable objects as delimiters', (done) => { expect(Qs.parse('a=b&c=d', { delimiter: true })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); - it('allows overriding parameter limit', function (done) { + it('allows overriding parameter limit', (done) => { expect(Qs.parse('a=b&c=d', { parameterLimit: 1 })).to.deep.equal({ a: 'b' }); done(); }); - it('allows setting the parameter limit to Infinity', function (done) { + it('allows setting the parameter limit to Infinity', (done) => { expect(Qs.parse('a=b&c=d', { parameterLimit: Infinity })).to.deep.equal({ a: 'b', c: 'd' }); done(); }); - it('allows overriding array limit', function (done) { + it('allows overriding array limit', (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' } }); @@ -332,90 +334,90 @@ describe('parse()', function () { done(); }); - it('allows disabling array parsing', function (done) { + it('allows disabling array parsing', (done) => { expect(Qs.parse('a[0]=b&a[1]=c', { parseArrays: false })).to.deep.equal({ a: { '0': 'b', '1': 'c' } }); done(); }); - it('parses an object', function (done) { + it('parses an object', (done) => { - var input = { + const input = { 'user[name]': { 'pop[bob]': 3 }, 'user[email]': null }; - var expected = { + const expected = { 'user': { 'name': { 'pop[bob]': 3 }, 'email': null } }; - var result = Qs.parse(input); + const result = Qs.parse(input); expect(result).to.deep.equal(expected); done(); }); - it('parses an object in dot notation', function (done) { + it('parses an object in dot notation', (done) => { - var input = { + const input = { 'user.name': { 'pop[bob]': 3 }, 'user.email.': null }; - var expected = { + const expected = { 'user': { 'name': { 'pop[bob]': 3 }, 'email': null } }; - var result = Qs.parse(input, { allowDots: true }); + const result = Qs.parse(input, { allowDots: true }); expect(result).to.deep.equal(expected); done(); }); - it('parses an object and not child values', function (done) { + it('parses an object and not child values', (done) => { - var input = { + const input = { 'user[name]': { 'pop[bob]': { 'test': 3 } }, 'user[email]': null }; - var expected = { + const expected = { 'user': { 'name': { 'pop[bob]': { 'test': 3 } }, 'email': null } }; - var result = Qs.parse(input); + const result = Qs.parse(input); expect(result).to.deep.equal(expected); done(); }); - it('does not blow up when Buffer global is missing', function (done) { + it('does not blow up when Buffer global is missing', (done) => { - var tempBuffer = global.Buffer; + const tempBuffer = global.Buffer; delete global.Buffer; - var result = Qs.parse('a=b&c=d'); + const result = Qs.parse('a=b&c=d'); global.Buffer = tempBuffer; expect(result).to.deep.equal({ a: 'b', c: 'd' }); done(); }); - it('does not crash when parsing circular references', function (done) { + it('does not crash when parsing circular references', (done) => { - var a = {}; + const a = {}; a.b = a; - var parsed; + let parsed; - expect(function () { + expect(() => { parsed = Qs.parse({ 'foo[bar]': 'baz', 'foo[baz]': a }); }).to.not.throw(); @@ -427,48 +429,48 @@ describe('parse()', function () { done(); }); - it('parses plain objects correctly', function (done) { + it('parses plain objects correctly', (done) => { - var a = Object.create(null); + const a = Object.create(null); a.b = 'c'; expect(Qs.parse(a)).to.deep.equal({ b: 'c' }); - var result = Qs.parse({ a: a }); + const result = Qs.parse({ a: a }); expect(result).to.contain('a'); expect(result.a).to.deep.equal(a); done(); }); - it('parses dates correctly', function (done) { + it('parses dates correctly', (done) => { - var now = new Date(); + const now = new Date(); expect(Qs.parse({ a: now })).to.deep.equal({ a: now }); done(); }); - it('parses regular expressions correctly', function (done) { + it('parses regular expressions correctly', (done) => { - var re = /^test$/; + const re = /^test$/; expect(Qs.parse({ a: re })).to.deep.equal({ a: re }); done(); }); - it('can allow overwriting prototype properties', function (done) { + it('can allow overwriting prototype properties', (done) => { expect(Qs.parse('a[hasOwnProperty]=b', { allowPrototypes: true })).to.deep.equal({ a: { hasOwnProperty: 'b' } }, { prototype: false }); expect(Qs.parse('hasOwnProperty=b', { allowPrototypes: true })).to.deep.equal({ hasOwnProperty: 'b' }, { prototype: false }); done(); }); - it('can return plain objects', function (done) { + it('can return plain objects', (done) => { - var expected = Object.create(null); + const expected = Object.create(null); expected.a = Object.create(null); expected.a.b = 'c'; expected.a.hasOwnProperty = 'd'; expect(Qs.parse('a[b]=c&a[hasOwnProperty]=d', { plainObjects: true })).to.deep.equal(expected); expect(Qs.parse(null, { plainObjects: true })).to.deep.equal(Object.create(null)); - var expectedArray = Object.create(null); + const expectedArray = Object.create(null); expectedArray.a = Object.create(null); expectedArray.a['0'] = 'b'; expectedArray.a.c = 'd'; diff --git a/test/stringify.js b/test/stringify.js old mode 100644 new mode 100755 index 53139ff9..99ef4b63 --- a/test/stringify.js +++ b/test/stringify.js @@ -1,27 +1,29 @@ +'use strict'; + /* eslint no-extend-native:0 */ // Load modules -var Code = require('code'); -var Lab = require('lab'); -var Qs = require('../'); +const Code = require('code'); +const Lab = require('lab'); +const Qs = require('../'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var expect = Code.expect; -var describe = lab.experiment; -var it = lab.test; +const lab = exports.lab = Lab.script(); +const expect = Code.expect; +const describe = lab.experiment; +const it = lab.test; -describe('stringify()', function () { +describe('stringify()', () => { - it('stringifies a querystring object', function (done) { + it('stringifies a querystring object', (done) => { expect(Qs.stringify({ a: 'b' })).to.equal('a=b'); expect(Qs.stringify({ a: 1 })).to.equal('a=1'); @@ -34,94 +36,94 @@ describe('stringify()', function () { done(); }); - it('stringifies a nested object', function (done) { + it('stringifies a nested object', (done) => { 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(); }); - it('stringifies an array value', function (done) { + it('stringifies an array value', (done) => { expect(Qs.stringify({ a: ['b', 'c', 'd'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d'); done(); }); - it('omits nulls when asked', function (done) { + it('omits nulls when asked', (done) => { expect(Qs.stringify({ a: 'b', c: null }, { skipNulls: true })).to.equal('a=b'); done(); }); - it('omits nested nulls when asked', function (done) { + it('omits nested nulls when asked', (done) => { expect(Qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true })).to.equal('a%5Bb%5D=c'); done(); }); - it('omits array indices when asked', function (done) { + it('omits array indices when asked', (done) => { expect(Qs.stringify({ a: ['b', 'c', 'd'] }, { indices: false })).to.equal('a=b&a=c&a=d'); done(); }); - it('stringifies a nested array value', function (done) { + it('stringifies a nested array value', (done) => { expect(Qs.stringify({ a: { b: ['c', 'd'] } })).to.equal('a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d'); done(); }); - it('stringifies an object inside an array', function (done) { + it('stringifies an object inside an array', (done) => { 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(); }); - it('does not omit object keys when indices = false', function (done) { + it('does not omit object keys when indices = false', (done) => { expect(Qs.stringify({ a: [{ b: 'c' }] }, { indices: false })).to.equal('a%5Bb%5D=c'); done(); }); - it('uses indices notation for arrays when indices=true', function (done) { + it('uses indices notation for arrays when indices=true', (done) => { expect(Qs.stringify({ a: ['b', 'c'] }, { indices: true })).to.equal('a%5B0%5D=b&a%5B1%5D=c'); done(); }); - it('uses indices notation for arrays when no arrayFormat is specified', function (done) { + it('uses indices notation for arrays when no arrayFormat is specified', (done) => { expect(Qs.stringify({ a: ['b', 'c'] })).to.equal('a%5B0%5D=b&a%5B1%5D=c'); done(); }); - it('uses indices notation for arrays when no arrayFormat=indices', function (done) { + it('uses indices notation for arrays when no arrayFormat=indices', (done) => { expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })).to.equal('a%5B0%5D=b&a%5B1%5D=c'); done(); }); - it('uses repeat notation for arrays when no arrayFormat=repeat', function (done) { + it('uses repeat notation for arrays when no arrayFormat=repeat', (done) => { expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })).to.equal('a=b&a=c'); done(); }); - it('uses brackets notation for arrays when no arrayFormat=brackets', function (done) { + it('uses brackets notation for arrays when no arrayFormat=brackets', (done) => { expect(Qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })).to.equal('a%5B%5D=b&a%5B%5D=c'); done(); }); - it('stringifies a complicated object', function (done) { + it('stringifies a complicated object', (done) => { expect(Qs.stringify({ a: { b: 'c', d: 'e' } })).to.equal('a%5Bb%5D=c&a%5Bd%5D=e'); done(); }); - it('stringifies an empty value', function (done) { + it('stringifies an empty value', (done) => { expect(Qs.stringify({ a: '' })).to.equal('a='); expect(Qs.stringify({ a: null }, { strictNullHandling: true })).to.equal('a'); @@ -136,15 +138,15 @@ describe('stringify()', function () { done(); }); - it('stringifies an empty object', function (done) { + it('stringifies an empty object', (done) => { - var obj = Object.create(null); + const obj = Object.create(null); obj.a = 'b'; expect(Qs.stringify(obj)).to.equal('a=b'); done(); }); - it('returns an empty string for invalid input', function (done) { + it('returns an empty string for invalid input', (done) => { expect(Qs.stringify(undefined)).to.equal(''); expect(Qs.stringify(false)).to.equal(''); @@ -153,9 +155,9 @@ describe('stringify()', function () { done(); }); - it('stringifies an object with an empty object as a child', function (done) { + it('stringifies an object with an empty object as a child', (done) => { - var obj = { + const obj = { a: Object.create(null) }; @@ -164,7 +166,7 @@ describe('stringify()', function () { done(); }); - it('drops keys with a value of undefined', function (done) { + it('drops keys with a value of undefined', (done) => { expect(Qs.stringify({ a: undefined })).to.equal(''); @@ -174,27 +176,27 @@ describe('stringify()', function () { done(); }); - it('url encodes values', function (done) { + it('url encodes values', (done) => { expect(Qs.stringify({ a: 'b c' })).to.equal('a=b%20c'); done(); }); - it('stringifies a date', function (done) { + it('stringifies a date', (done) => { - var now = new Date(); - var str = 'a=' + encodeURIComponent(now.toISOString()); + const now = new Date(); + const str = 'a=' + encodeURIComponent(now.toISOString()); expect(Qs.stringify({ a: now })).to.equal(str); done(); }); - it('stringifies the weird object from qs', function (done) { + it('stringifies the weird object from qs', (done) => { expect(Qs.stringify({ 'my weird field': '~q1!2"\'w$5&7/z8)?' })).to.equal('my%20weird%20field=~q1%212%22%27w%245%267%2Fz8%29%3F'); done(); }); - it('skips properties that are part of the object prototype', function (done) { + it('skips properties that are part of the object prototype', (done) => { Object.prototype.crash = 'test'; expect(Qs.stringify({ a: 'b' })).to.equal('a=b'); @@ -203,7 +205,7 @@ describe('stringify()', function () { done(); }); - it('stringifies boolean values', function (done) { + it('stringifies boolean values', (done) => { expect(Qs.stringify({ a: true })).to.equal('a=true'); expect(Qs.stringify({ a: { b: true } })).to.equal('a%5Bb%5D=true'); @@ -212,30 +214,30 @@ describe('stringify()', function () { done(); }); - it('stringifies buffer values', function (done) { + it('stringifies buffer values', (done) => { expect(Qs.stringify({ a: new Buffer('test') })).to.equal('a=test'); expect(Qs.stringify({ a: { b: new Buffer('test') } })).to.equal('a%5Bb%5D=test'); done(); }); - it('stringifies an object using an alternative delimiter', function (done) { + it('stringifies an object using an alternative delimiter', (done) => { expect(Qs.stringify({ a: 'b', c: 'd' }, { delimiter: ';' })).to.equal('a=b;c=d'); done(); }); - it('doesn\'t blow up when Buffer global is missing', function (done) { + it('doesn\'t blow up when Buffer global is missing', (done) => { - var tempBuffer = global.Buffer; + const tempBuffer = global.Buffer; delete global.Buffer; - var result = Qs.stringify({ a: 'b', c: 'd' }); + const result = Qs.stringify({ a: 'b', c: 'd' }); global.Buffer = tempBuffer; expect(result).to.equal('a=b&c=d'); done(); }); - it('selects properties when filter=array', function (done) { + it('selects properties when filter=array', (done) => { expect(Qs.stringify({ a: 'b' }, { filter: ['a'] })).to.equal('a=b'); expect(Qs.stringify({ a: 1 }, { filter: [] })).to.equal(''); @@ -244,11 +246,11 @@ describe('stringify()', function () { }); - it('supports custom representations when filter=function', function (done) { + it('supports custom representations when filter=function', (done) => { - var calls = 0; - var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } }; - var filterFunc = function (prefix, value) { + let calls = 0; + const obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } }; + const filterFunc = function (prefix, value) { calls++; if (calls === 1) { @@ -271,7 +273,7 @@ describe('stringify()', function () { }); - it('can disable uri encoding', function (done) { + it('can disable uri encoding', (done) => { expect(Qs.stringify({ a: 'b' }, { encode: false })).to.equal('a=b'); expect(Qs.stringify({ a: { b: 'c' } }, { encode: false })).to.equal('a[b]=c'); @@ -279,15 +281,11 @@ describe('stringify()', function () { done(); }); - it('can sort the keys', function (done) { - - var sort = function alphabeticalSort (a, b) { - - return a.localeCompare(b); - }; + it('can sort the keys', (done) => { - expect(Qs.stringify({ a: 'c', z: 'y', b : 'f' }, { sort : sort })).to.equal('a=c&b=f&z=y'); - expect(Qs.stringify({ a: 'c', z: { j: 'a', i:'b' }, b : 'f' }, { sort : sort })).to.equal('a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a'); + const sort = (a, b) => a.localeCompare(b); + expect(Qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort })).to.equal('a=c&b=f&z=y'); + expect(Qs.stringify({ a: 'c', z: { j: 'a', i: 'b' }, b: 'f' }, { sort: sort })).to.equal('a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a'); done(); }); }); diff --git a/test/utils.js b/test/utils.js old mode 100644 new mode 100755 index a9a6b520..35ac8f20 --- a/test/utils.js +++ b/test/utils.js @@ -1,26 +1,28 @@ +'use strict'; + // Load modules -var Code = require('code'); -var Lab = require('lab'); -var Utils = require('../lib/utils'); +const Code = require('code'); +const Lab = require('lab'); +const Utils = require('../lib/utils'); // Declare internals -var internals = {}; +const internals = {}; // Test shortcuts -var lab = exports.lab = Lab.script(); -var expect = Code.expect; -var describe = lab.experiment; -var it = lab.test; +const lab = exports.lab = Lab.script(); +const expect = Code.expect; +const describe = lab.experiment; +const it = lab.test; -describe('merge()', function () { +describe('merge()', () => { - it('can merge two objects with the same key', function (done) { + it('can merge two objects with the same key', (done) => { expect(Utils.merge({ a: 'b' }, { a: 'c' })).to.deep.equal({ a: ['b', 'c'] }); done();