Skip to content

Commit

Permalink
Merge pull request #125 from hapijs/es6
Browse files Browse the repository at this point in the history
es6. Closes #124
  • Loading branch information
hueniverse committed Nov 3, 2015
2 parents 56f4fbb + 6c70b31 commit ed16969
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: node_js

node_js:
- 0.10
- 4.0
- 4
- 5

sudo: false
14 changes: 7 additions & 7 deletions lib/index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 27 additions & 25 deletions lib/parse.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)] = '';
Expand All @@ -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;
Expand All @@ -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 &&
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
43 changes: 23 additions & 20 deletions lib/stringify.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -109,15 +112,15 @@ module.exports = function (obj, options) {
objKeys = filter = options.filter;
}

var keys = [];
let keys = [];

if (typeof obj !== 'object' ||
obj === null) {

return '';
}

var arrayFormat;
let arrayFormat;
if (options.arrayFormat in internals.arrayPrefixGenerators) {
arrayFormat = options.arrayFormat;
}
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit ed16969

Please sign in to comment.