-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
82 lines (80 loc) · 4.4 KB
/
main.js
File metadata and controls
82 lines (80 loc) · 4.4 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
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
79
80
81
82
/* eslint-disable node/no-unsupported-features/es-syntax */
// REFERENCES
// https://html.spec.whatwg.org/multipage/links.html#api-for-a-and-area-elements
// https://tools.ietf.org/html/rfc1738
// https://tools.ietf.org/html/rfc1808
// https://tools.ietf.org/html/rfc2396
// https://tools.ietf.org/html/rfc3986
//
// SCHEMA
// ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
// │ href │
// ├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
// │ protocol │ │ auth │ host │ path │ hash │
// │ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │
// │ │ │ │ hostname │ port │ pathname │ search │ │
// │ │ │ │ │ │ ├─┬──────────────┤ │
// │ │ │ │ │ │ │ │ query │ │
// | https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash |
// │ │ │ │ │ hostname │ port │ │ │ │
// │ │ │ │ ├─────────────────┴──────┤ │ │ │
// │ protocol │ │ username │ password │ host │ │ │ │
// ├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │
// │ origin │ │ origin │ pathname │ search │ hash │
// ├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
// │ href │
// └────────────────────────────────────────────────────────────────────────────────────────────────┘
export const hyperlink = ({ auth, hash, host, hostname, origin, password, path, pathname, port, protocol, query, search, username }) => {
let href = ''
if (origin !== undefined) {
;[protocol, host] = origin.split('//')
}
if (host === undefined) {
if (hostname !== undefined) {
host = hostname
if (port !== undefined) {
host += ':'
host += port
}
}
}
if (host !== undefined) {
if (protocol !== undefined) {
href += protocol
}
href += '//'
if (auth === undefined) {
if (username !== undefined) {
auth = username
if (password !== undefined) {
auth += ':'
auth += password
}
}
}
if (auth !== undefined) {
href += auth
href += '@'
}
href += host
}
if (path === undefined) {
if (search === undefined && query !== undefined) {
search = '?'
search += query
}
if (pathname !== undefined) {
path = pathname
if (search !== undefined) {
path += search
}
}
}
if (path !== undefined) {
href += path
if (hash !== undefined) {
href += hash
}
}
return href
}