Skip to content

Commit

Permalink
es6. Closes #124
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Nov 3, 2015
1 parent 56f4fbb commit 6c70b31
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

7 comments on commit 6c70b31

@simov
Copy link
Contributor

@simov simov commented on 6c70b31 Nov 3, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure I missed the complaints thread, but still:

I get the consistency point, and I don't care about supporting node 0.10 either, but why is adding noise to the code base considered a good thing?

Does using const and let in the context of this module have any real benefit?
Does using const and let over var makes this module easier to read and maintain?

Arrow functions? Yes, they make the code easier to read.

@nlf
Copy link
Collaborator

@nlf nlf commented on 6c70b31 Nov 3, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consistency is the point. all hapi core projects must follow the same linting and testing rules, and this module is no exception. this update makes this module adhere to the new rules.

@simov
Copy link
Contributor

@simov simov commented on 6c70b31 Nov 3, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm the last person who's going to whine about your rules. Still I think completely banning the var keyword is just wrong. What possibly can go wrong in a 5 line function that you need two consts there? Anyway, I like the ES6 features, but I don't think they are needed by default everywhere.

@Marsup
Copy link
Contributor

@Marsup Marsup commented on 6c70b31 Nov 3, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var leaks outside of what people consider "normal" scope, let/const don't. It's all about intentions, with const it's obvious, with let you really place it where the scope of the variable should be, so no debate here for me, let will win in the end.

@Max-Kolodezniy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this module brakes all kind of npm usages. I'm using latest stable node (v 0.12.2) and I have "Use of const in strict mode." error

@nlf
Copy link
Collaborator

@nlf nlf commented on 6c70b31 Nov 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.12.12 is definitely not the latest stable node, the 5.x releases are stable and there's even a 4.x release that's in place for long term support

@Max-Kolodezniy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! "sudo n stable" gave me v5.0.0. Closed my issue #138

Please sign in to comment.