-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathquery-helper.js
More file actions
38 lines (32 loc) · 765 Bytes
/
query-helper.js
File metadata and controls
38 lines (32 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
/**
* Creates a valid query string by appending a given param and value to query.
*
* @param {string} query
* @param {string} param
* @param {string} value
*/
function addToQuery(query, param, value) {
if (!value) {
return query;
}
const queryAddParam = query ? query + '&' + param : param;
return value !== true ? queryAddParam + '=' + value : queryAddParam;
}
/**
* Adds a valid query string to a given url.
*
* @param {string} url
* @param {string} param
* @param {string} value
*/
function addToUrl(url, param, value) {
const urlParts = url.split('?');
const base = urlParts[0];
const query = urlParts[1];
return base + '?' + addToQuery(query, param, value);
}
module.exports = {
addToQuery,
addToUrl,
};