unpkg/modules/utils/createSearch.js

17 lines
383 B
JavaScript
Raw Normal View History

2017-09-02 05:43:56 +00:00
function createSearch(query) {
2018-05-26 00:22:59 +00:00
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])}`
),
[]
);
2017-09-02 05:43:56 +00:00
2018-05-26 00:22:59 +00:00
return params.length ? `?${params.join("&")}` : "";
2017-09-02 05:43:56 +00:00
}
2018-02-18 02:00:56 +00:00
module.exports = createSearch;