15 lines
366 B
JavaScript
15 lines
366 B
JavaScript
export default function createSearch(query) {
|
|
const keys = Object.keys(query).sort();
|
|
const params = keys.reduce(
|
|
(memo, key) =>
|
|
memo.concat(
|
|
query[key] === ''
|
|
? key // Omit the trailing "=" from key=
|
|
: `${key}=${encodeURIComponent(query[key])}`
|
|
),
|
|
[]
|
|
);
|
|
|
|
return params.length ? `?${params.join('&')}` : '';
|
|
}
|