Completed
Push — master ( 3a1758...2993be )
by Dimas
27:24 queued 18:24
created

url.ts ➔ getParameterByName   B

Complexity

Conditions 6

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
dl 0
loc 22
rs 8.6666
c 0
b 0
f 0
1
/**
2
 * get url parameter by name
3
 * @param name parameter name
4
 * @param url url target, null for current location.href
5
 */
6
function getParameterByName(name: string, url: string | null) {
7
  if (typeof URLSearchParams !== 'undefined') {
8
    if (!window.location.search) {
9
      url = window.location.href;
10
    }
11
    const urlParams = new URLSearchParams(url);
12
    return urlParams.get(name);
13
  }
14
  if (!url) {
15
    url = window.location.href;
16
  }
17
  name = name.replace(/[\[\]]/g, '\\$&');
18
  var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
19
    results = regex.exec(url);
20
  if (!results) return null;
21
  if (!results[2]) return '';
22
  return decodeURIComponent(results[2].replace(/\+/g, ' '));
23
}