From 563588dafbfda6fa1800b9959700f6b9b4bd3038 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 31 Jan 2019 14:24:14 -0800 Subject: [PATCH] [Refactor] use cached `Array.isArray` --- .eslintrc | 34 +++++++++++++++++++--------------- lib/stringify.js | 13 ++++++++----- test/stringify.js | 11 +++++------ 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/.eslintrc b/.eslintrc index 1faac273..16344a23 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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, + }, } diff --git a/lib/stringify.js b/lib/stringify.js index 6e1c9a26..f05c6dd0 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -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') { @@ -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); @@ -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)); @@ -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; @@ -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 = []; diff --git a/test/stringify.js b/test/stringify.js index 699397e3..3befb520 100644 --- a/test/stringify.js +++ b/test/stringify.js @@ -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(); @@ -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('%'); @@ -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' });