Skip to content

Commit

Permalink
[Refactor] use cached Array.isArray
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 31, 2019
1 parent 39a11bc commit 563588d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
34 changes: 19 additions & 15 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"root": true,
"root": true,

"extends": "@ljharb",
"extends": "@ljharb",

"rules": {
"complexity": [2, 22],
"consistent-return": [1],
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
"indent": [2, 4],
"max-params": [2, 9],
"max-statements": [2, 36],
"no-extra-parens": [1],
"no-continue": [1],
"no-magic-numbers": 0,
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
"operator-linebreak": 1
}
"rules": {
"complexity": [2, 25],
"consistent-return": [1],
"func-name-matching": 0,
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
"indent": [2, 4],
"max-len": 0,
"max-lines-per-function": 0,
"max-params": [2, 9],
"max-statements": [0, 36],
"no-extra-parens": [1],
"no-continue": [1],
"no-magic-numbers": 0,
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
"operator-linebreak": 1,
"sort-keys": 0,
},
}
13 changes: 8 additions & 5 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var defaults = {
encoder: Utils.encode
};

var isArray = Array.isArray;

var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots) {
var obj = object;
if (typeof filter === 'function') {
Expand Down Expand Up @@ -50,7 +52,7 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
}

var objKeys;
if (Array.isArray(filter)) {
if (isArray(filter)) {
objKeys = filter;
} else {
var keys = Object.keys(obj);
Expand All @@ -64,7 +66,7 @@ var stringify = function stringify(object, prefix, generateArrayPrefix, strictNu
continue;
}

if (Array.isArray(obj)) {
if (isArray(obj)) {
values = values.concat(stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
} else {
values = values.concat(stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots));
Expand All @@ -81,7 +83,7 @@ module.exports = function (object, opts) {
var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
var encoder = encode ? typeof options.encoder === 'function' ? options.encoder : defaults.encoder : null;
var sort = typeof options.sort === 'function' ? options.sort : null;
var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
var objKeys;
Expand All @@ -94,8 +96,9 @@ module.exports = function (object, opts) {
if (typeof options.filter === 'function') {
filter = options.filter;
obj = filter('', obj);
} else if (Array.isArray(options.filter)) {
objKeys = filter = options.filter;
} else if (isArray(options.filter)) {
objKeys = options.filter;
filter = options.filter;
}

var keys = [];
Expand Down
11 changes: 5 additions & 6 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ test('stringify()', function (t) {
st.end();
});


t.test('omits nested nulls when asked', function (st) {
st.equal(qs.stringify({ a: { b: 'c', d: null } }, { skipNulls: true }), 'a%5Bb%5D=c');
st.end();
Expand Down Expand Up @@ -258,20 +257,20 @@ test('stringify()', function (t) {

t.test('can sort the keys at depth 3 or more too', function (st) {
var sort = function (a, b) { return a.localeCompare(b); };
st.equal(qs.stringify({ a: 'a', z: { zj: {zjb: 'zjb', zja: 'zja'}, zi: {zib: 'zib', zia: 'zia'} }, b: 'b' }, { sort: sort, encode: false }), 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb');
st.equal(qs.stringify({ a: 'a', z: { zj: {zjb: 'zjb', zja: 'zja'}, zi: {zib: 'zib', zia: 'zia'} }, b: 'b' }, { sort: null, encode: false }), 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b');
st.equal(qs.stringify({ a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' }, { sort: sort, encode: false }), 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb');
st.equal(qs.stringify({ a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' }, { sort: null, encode: false }), 'a=a&z[zj][zjb]=zjb&z[zj][zja]=zja&z[zi][zib]=zib&z[zi][zia]=zia&b=b');
st.end();
});

t.test('can stringify with custom encoding', function (st) {
st.equal(qs.stringify({ : '大阪府', '': ''}, {
st.equal(qs.stringify({ : '大阪府', '': '' }, {
encoder: function (str) {
if (str.length === 0) {
return '';
}
var buf = iconv.encode(str, 'shiftjis');
var result = [];
for (var i=0; i < buf.length; ++i) {
for (var i = 0; i < buf.length; ++i) {
result.push(buf.readUInt8(i).toString(16));
}
return '%' + result.join('%');
Expand All @@ -281,7 +280,7 @@ test('stringify()', function (t) {
});

t.test('throws error with wrong encoder', function (st) {
st.throws(function () {
st['throws'](function () {
qs.stringify({}, {
encoder: 'string'
});
Expand Down

0 comments on commit 563588d

Please sign in to comment.