-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-url-variations.html
78 lines (71 loc) · 2.78 KB
/
parse-url-variations.html
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
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>teset</title>
</head>
<body>
<script>
/**
* @external document
* @type {Document}
*/
/**
* @param {string} url
* @returns {HTMLAnchorElement}
*/
function parseUrl (url) {
if (typeof url === 'string' && url.indexOf('http', 0) !== -1) {
/**
* @type {HTMLElement | HTMLAnchorElement}
*/
const urlElement = document.createElement('a')
urlElement.href = url
// Internet Explorer omits the leading slash, so we should normalize it
urlElement.pathname = urlElement.pathname.replace(/(^\/?)/, '/')
return urlElement
}
}
function formatObj (obj) {
return '\n{' + '\n host: ' + obj.hostname + ',' + '\n path: ' + obj.pathname + '\n}\n'
}
[0, 1, -1, NaN, undefined, null, {}, 'com/29', 'example', 'example.com', 'https://example.com', 'https://example.com/', 'https://example.com/some/path?name', 'https://www.example.com/some/path?name=value&foo=bar#anchor', 'https://www.example.com/some/path?name=value#anchor', 'http://www.example.com/some/path?name=value#anchor', 'http://www.example.com/some/path'].forEach(function(item, i) {
const result = parseUrl(item)
console.log('Test #%s : %c%s %c%s', i, 'color:red', (result) ? formatObj(result) : 'undefined', 'color:blue', item)
});
function parseCacheUrl (url) {
const hostAndPath = url.split('//')[1]
const i = hostAndPath.indexOf('/')
return {
hostname: hostAndPath.slice(0, i),
pathname: hostAndPath.slice(i)
}
}
console.log('PARSE CACHE URL 1 %c%s (%s)', 'color:red', formatObj(parseCacheUrl('https://prebid.adnxs.com/pbc/v1/cache')), parseCacheUrl.toString().length);
function parseCacheUrl2 (url) {
const hostAndPath = url.split('//')[1].split('/')
return {
hostname: hostAndPath[0],
pathname: hostAndPath.slice(1).join('/')
}
}
console.log('PARSE CACHE URL 2 %c %s (%s)', 'color:red', formatObj(parseCacheUrl2('https://prebid.adnxs.com/pbc/v1/cache')), parseCacheUrl2.toString().length);
function parseCacheUrl3 (url) {
const hostAndPath = url.split('//')[1]
return {
hostname: hostAndPath.slice(0, hostAndPath.indexOf('/')),
pathname: hostAndPath.slice(hostAndPath.indexOf('/'))
};
}
console.log('PARSE CACHE URL 3 %c%s (%s)', 'color: red', formatObj(parseCacheUrl3('https://prebid.adnxs.com/pbc/v1/cache')), parseCacheUrl3.toString().length);
function parseCacheUrl4 (url) {
const hostAndPath = url.split('//')[1].split(/\/(.+)/)
return {
hostname: hostAndPath[0],
pathname: '/' + hostAndPath[1]
};
}
console.log('PARSE CACHE URL 4 %c%s (%s)', 'color: red', formatObj(parseCacheUrl4('https://prebid.adnxs.com/pbc/v1/cache')), parseCacheUrl4.toString().length);
</script>
</body>
</html>