Skip to content

Commit

Permalink
[Refactor] clean up whitespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 31, 2015
1 parent d6412f7 commit bc0c1cb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 109 deletions.
46 changes: 13 additions & 33 deletions lib/parse.js
@@ -1,12 +1,7 @@
'use strict';

// Load modules

var Utils = require('./utils');


// Declare internals

var internals = {
delimiter: '&',
depth: 5,
Expand All @@ -18,9 +13,7 @@ var internals = {
allowDots: false
};


internals.parseValues = function (str, options) {

var obj = {};
var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);

Expand All @@ -34,8 +27,7 @@ internals.parseValues = function (str, options) {
if (options.strictNullHandling) {
obj[Utils.decode(part)] = null;
}
}
else {
} else {
var key = Utils.decode(part.slice(0, pos));
var val = Utils.decode(part.slice(pos + 1));

Expand All @@ -51,9 +43,7 @@ internals.parseValues = function (str, options) {
return obj;
};


internals.parseObject = function (chain, val, options) {

if (!chain.length) {
return val;
}
Expand All @@ -64,32 +54,28 @@ internals.parseObject = function (chain, val, options) {
if (root === '[]') {
obj = [];
obj = obj.concat(internals.parseObject(chain, val, options));
}
else {
} 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);
if (!isNaN(index) &&
if (
!isNaN(index) &&
root !== cleanRoot &&
String(index) === cleanRoot &&
index >= 0 &&
(options.parseArrays &&
index <= options.arrayLimit)) {

(options.parseArrays && index <= options.arrayLimit)
) {
obj = [];
obj[index] = internals.parseObject(chain, val, options);
}
else {
} else {
obj[cleanRoot] = internals.parseObject(chain, val, options);
}
}

return obj;
};


internals.parseKeys = function (key, val, options) {

if (!key) {
return;
}
Expand All @@ -115,9 +101,7 @@ internals.parseKeys = function (key, val, options) {
if (segment[1]) {
// If we aren't using plain objects, optionally prefix keys
// that would overwrite object prototype properties
if (!options.plainObjects &&
Object.prototype.hasOwnProperty(segment[1])) {

if (!options.plainObjects && Object.prototype.hasOwnProperty(segment[1])) {
if (!options.allowPrototypes) {
return;
}
Expand All @@ -130,11 +114,8 @@ internals.parseKeys = function (key, val, options) {

var i = 0;
while ((segment = child.exec(key)) !== null && i < options.depth) {

++i;
if (!options.plainObjects &&
Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {

if (!options.plainObjects && Object.prototype.hasOwnProperty(segment[1].replace(/\[|\]/g, ''))) {
if (!options.allowPrototypes) {
continue;
}
Expand All @@ -151,9 +132,7 @@ internals.parseKeys = function (key, val, options) {
return internals.parseObject(keys, val, options);
};


module.exports = function (str, options) {

options = options || {};
options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter;
options.depth = typeof options.depth === 'number' ? options.depth : internals.depth;
Expand All @@ -165,10 +144,11 @@ module.exports = function (str, options) {
options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit;
options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;

if (str === '' ||
if (
str === '' ||
str === null ||
typeof str === 'undefined') {

typeof str === 'undefined'
) {
return options.plainObjects ? Object.create(null) : {};
}

Expand Down
53 changes: 12 additions & 41 deletions lib/stringify.js
@@ -1,25 +1,17 @@
'use strict';

// Load modules

var Utils = require('./utils');


// Declare internals

var internals = {
delimiter: '&',
arrayPrefixGenerators: {
brackets: function (prefix, key) {

return prefix + '[]';
},
indices: function (prefix, key) {

return prefix + '[' + key + ']';
},
repeat: function (prefix, key) {

return prefix;
}
},
Expand All @@ -28,30 +20,22 @@ var internals = {
encode: true
};


internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort) {

if (typeof filter === 'function') {
obj = filter(prefix, obj);
}
else if (Utils.isBuffer(obj)) {
} else if (Utils.isBuffer(obj)) {
obj = obj.toString();
}
else if (obj instanceof Date) {
} else if (obj instanceof Date) {
obj = obj.toISOString();
}
else if (obj === null) {
} else if (obj === null) {
if (strictNullHandling) {
return encode ? Utils.encode(prefix) : prefix;
}

obj = '';
}

if (typeof obj === 'string' ||
typeof obj === 'number' ||
typeof obj === 'boolean') {

if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') {
if (encode) {
return [Utils.encode(prefix) + '=' + Utils.encode(obj)];
}
Expand All @@ -67,35 +51,29 @@ internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHand
var objKeys;
if (Array.isArray(filter)) {
objKeys = filter;
}
else {
} else {
var keys = Object.keys(obj);
objKeys = sort ? keys.sort(sort) : keys;
}

for (var i = 0; i < objKeys.length; ++i) {
var key = objKeys[i];

if (skipNulls &&
obj[key] === null) {

if (skipNulls && obj[key] === null) {
continue;
}

if (Array.isArray(obj)) {
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encode, filter));
}
else {
} else {
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix, strictNullHandling, skipNulls, encode, filter));
}
}

return values;
};


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;
Expand All @@ -107,27 +85,22 @@ module.exports = function (obj, options) {
if (typeof options.filter === 'function') {
filter = options.filter;
obj = filter('', obj);
}
else if (Array.isArray(options.filter)) {
} else if (Array.isArray(options.filter)) {
objKeys = filter = options.filter;
}

var keys = [];

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

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

var arrayFormat;
if (options.arrayFormat in internals.arrayPrefixGenerators) {
arrayFormat = options.arrayFormat;
}
else if ('indices' in options) {
} else if ('indices' in options) {
arrayFormat = options.indices ? 'indices' : 'repeat';
}
else {
} else {
arrayFormat = 'indices';
}

Expand All @@ -144,9 +117,7 @@ module.exports = function (obj, options) {
for (var i = 0; i < objKeys.length; ++i) {
var key = objKeys[i];

if (skipNulls &&
obj[key] === null) {

if (skipNulls && obj[key] === null) {
continue;
}

Expand Down

0 comments on commit bc0c1cb

Please sign in to comment.