-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoveUrlParameters.js
More file actions
29 lines (27 loc) · 1.19 KB
/
Copy pathremoveUrlParameters.js
File metadata and controls
29 lines (27 loc) · 1.19 KB
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
/*
Complete the method so that it does the following:
Removes any duplicate query string parameters from the url
Removes any query string parameters specified within the 2nd argument (optional array)
Examples:
stripUrlParams('www.codewars.com?a=1&b=2&a=2') // returns 'www.codewars.com?a=1&b=2'
stripUrlParams('www.codewars.com?a=1&b=2&a=2', ['b']) // returns 'www.codewars.com?a=1'
stripUrlParams('www.codewars.com', ['b']) // returns 'www.codewars.com'
*/
function stripUrlParams(url, paramsToStrip){
paramsToStrip = paramsToStrip || [];
var queryStartIndex = url.indexOf('?');
if (queryStartIndex === -1) return url;
var newUrl = url.slice(0, queryStartIndex);
var query = url.slice(queryStartIndex);
var found = query.match(/(?:\?|&)([^=&#]+)=([^=&#]+)/g);
var keys=[];
found.forEach(function(keyValuePair){
var key = keyValuePair.slice(1, keyValuePair.indexOf('='));
//if unused key and the key is not one of the parameters to remove, add key/value to newUrl and store key
if (keys.indexOf(key) === -1 && paramsToStrip.indexOf(key) === -1) {
keys.push(key);
newUrl += keyValuePair;
}
});
return newUrl;
}