-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
70 lines (61 loc) · 1.99 KB
/
util.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Add values to query string
function updateQueryStringParam(key, value) {
baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
urlQueryString = document.location.search;
var newParam = key + '=' + value,
params = '?' + newParam;
// If the "search" string exists, then build params from it
if (urlQueryString) {
keyRegex = new RegExp('([\?&])' + key + '[^&]*');
// If param exists already, update it
if (urlQueryString.match(keyRegex) !== null) {
params = urlQueryString.replace(keyRegex, "$1" + newParam);
} else { // Otherwise, add it to end of query string
params = urlQueryString + '&' + newParam;
}
}
window.history.replaceState({}, "", baseUrl + params);
}
// Remove parameters from query string
function removeParameterByName(parameter) {
var url = document.location.href;
var urlparts = url.split('?');
if (urlparts.length >= 2) {
var urlBase = urlparts.shift();
var queryString = urlparts.join("?");
var prefix = encodeURIComponent(parameter) + '=';
var pars = queryString.split(/[&;]/g);
for (var i = pars.length; i-- > 0;)
if (pars[i].lastIndexOf(prefix, 0) !== -1)
pars.splice(i, 1);
url = urlBase + '?' + pars.join('&');
window.history.replaceState('', document.title, url); // added this line to push the new url directly to url bar .
}
return url;
}
// Get values from query string
function getParameterByName(name, def) {
if (typeof def === 'undefined') {
def = null;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(window.location.href);
if (!results) return def;
if (!results[2]) return '';
return Number(decodeURIComponent(results[2].replace(/\+/g, " ")));
}
function round(value) {
//return Math.round(value * 100) / 100;
if (value > 10) {
return Math.round(value);
} else {
return Math.round(value * 10) / 10;
}
}
function nice_name(name) {
return _.startCase(name);
}
function padded(text) {
return text.padEnd(25, " ") + ": "
}