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 107c302 commit ef27de4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
}

var objKeys;
if (Array.isArray(filter)) {
if (isArray(filter)) {
objKeys = filter;
} else {
var keys = Object.keys(obj);
Expand All @@ -93,7 +93,7 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
continue;
}

if (Array.isArray(obj)) {
if (isArray(obj)) {
pushToArray(values, stringify(
obj[key],
generateArrayPrefix(prefix, key),
Expand Down Expand Up @@ -158,7 +158,7 @@ module.exports = function (object, opts) {
if (typeof options.filter === 'function') {
filter = options.filter;
obj = filter('', obj);
} else if (Array.isArray(options.filter)) {
} else if (isArray(options.filter)) {
filter = options.filter;
objKeys = filter;
}
Expand Down
16 changes: 16 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ test('stringify()', function (t) {
st.end();
});

t.test('stringifies falsy values', function (st) {
st.equal(qs.stringify(undefined), '');
st.equal(qs.stringify(null), '');
st.equal(qs.stringify(null, { strictNullHandling: true }), '');
st.equal(qs.stringify(false), '');
st.equal(qs.stringify(0), '');
st.end();
});

t.test('adds query prefix', function (st) {
st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b');
st.end();
Expand All @@ -29,6 +38,13 @@ test('stringify()', function (t) {
st.end();
});

t.test('stringifies nested falsy values', function (st) {
st.equal(qs.stringify({ a: { b: { c: null } } }), 'a%5Bb%5D%5Bc%5D=');
st.equal(qs.stringify({ a: { b: { c: null } } }, { strictNullHandling: true }), 'a%5Bb%5D%5Bc%5D');
st.equal(qs.stringify({ a: { b: { c: false } } }), 'a%5Bb%5D%5Bc%5D=false');
st.end();
});

t.test('stringifies a nested object', function (st) {
st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
Expand Down

0 comments on commit ef27de4

Please sign in to comment.