github.com/outbrain/consul@v1.4.5/ui-v2/app/utils/createURL.js (about) 1 /** 2 * Creates a safe url encoded url 3 * 4 * @param {string[]} encoded - Pre-encoded parts for the url 5 * @param {string[]} raw - Possibly unsafe (not encoded) parts for the url 6 * @param {object} query - A 'query object' the values of which are possibly unsafe and will be passed through encode 7 * @param {function} [encode=encodeURIComponent] - Injectable encode function, defaulting to the browser default encodeURIComponent 8 * 9 * @example 10 * // returns 'a/nice-url/with%20some/non%20encoded?sortBy=the%20name&page=1' 11 * createURL(['a/nice-url'], ['with some', 'non encoded'], {sortBy: "the name", page: 1}) 12 */ 13 export default function(encoded, raw, query = {}, encode = encodeURIComponent) { 14 return [ 15 encoded.concat(raw.map(encode)).join('/'), 16 Object.keys(query) 17 .map(function(key, i, arr) { 18 if (query[key] != null) { 19 return `${encode(key)}=${encode(query[key])}`; 20 } 21 return key; 22 }) 23 .join('&'), 24 ] 25 .filter(item => item !== '') 26 .join('?'); 27 }