parent
699b849914
commit
7582c641fb
|
@ -5,6 +5,11 @@ describe('createSearch', () => {
|
||||||
expect(createSearch({ a: 'a', b: '' })).toEqual('?a=a&b');
|
expect(createSearch({ a: 'a', b: '' })).toEqual('?a=a&b');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('omits the trailing = for null/undefined values', () => {
|
||||||
|
expect(createSearch({ a: 'a', b: null })).toEqual('?a=a&b');
|
||||||
|
expect(createSearch({ a: 'a', b: undefined })).toEqual('?a=a&b');
|
||||||
|
});
|
||||||
|
|
||||||
it('sorts keys', () => {
|
it('sorts keys', () => {
|
||||||
expect(createSearch({ b: 'b', a: 'a', c: 'c' })).toEqual('?a=a&b=b&c=c');
|
expect(createSearch({ b: 'b', a: 'a', c: 'c' })).toEqual('?a=a&b=b&c=c');
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
export default function createSearch(query) {
|
export default function createSearch(query) {
|
||||||
const keys = Object.keys(query).sort();
|
const keys = Object.keys(query).sort();
|
||||||
const params = keys.reduce(
|
const pairs = keys.reduce(
|
||||||
(memo, key) =>
|
(memo, key) =>
|
||||||
memo.concat(
|
memo.concat(
|
||||||
query[key] ? `${key}=${encodeURIComponent(query[key])}` : key
|
query[key] == null || query[key] === ''
|
||||||
|
? key
|
||||||
|
: `${key}=${encodeURIComponent(query[key])}`
|
||||||
),
|
),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
return params.length ? `?${params.join('&')}` : '';
|
return pairs.length ? `?${pairs.join('&')}` : '';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue