-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.js
246 lines (216 loc) · 7.89 KB
/
src.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const UrlTool = {
defaultFormattingTemplate: '{protocol}://{host}/{path}/{query}{hash}',
parseQuery (queryString) {
if (queryString === undefined || queryString === null || queryString === '') {
return {}
}
queryString = queryString.trim()
if (queryString.startsWith('?')) {
queryString = queryString.substring(1)
}
const query = {}
const queryParts = queryString.split('&')
for (const queryPart of queryParts) {
const [key, value] = queryPart.split('=')
query[decodeURIComponent(key) || ''] = decodeURIComponent(value || '')
}
try { delete query[''] } catch {}
return query
},
stringifyQuery (query, questionMark = '?') {
const queryParts = []
for (const [key, value] of Object.entries(query)) {
queryParts.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
}
return queryParts.length ? `${questionMark}${queryParts.join('&')}` : ''
},
Url: class Url {
constructor (urlstring) {
if (typeof urlstring !== 'string') throw new TypeError('Url must be a string')
this._protocol = ''
this._host = ''
this._port = ''
this._path = ''
this._query = ''
this._hash = ''
this._raw = urlstring
this._parse()
}
_parse () {
// Check if protocol is provided
let rawWithoutProtocol = this._raw
if (this._raw.match(/([a-zA-Z-_\d]*?):\/\//) && this._raw.match(/([a-zA-Z-_\d]*?):\/\//).index === 0) {
// Protocol provided
const _foundProtocol = this._raw.match(/([a-zA-Z-_\d]*?):\/\//)[1]
if (_foundProtocol.length > 0) {
this._protocol = _foundProtocol
} else {
this._protocol = null
}
if (this._protocol === null) {
rawWithoutProtocol = this._raw.substring(this._raw.indexOf('://') + 3)
} else {
rawWithoutProtocol = this._raw.substring(this._raw.match(/([a-zA-Z-_\d]*?):\/\//)[0].length)
}
} else if (this._raw.startsWith('//')) {
this._protocol = 'http'
} else {
this._protocol = null
}
while (rawWithoutProtocol.startsWith('/')) {
rawWithoutProtocol = rawWithoutProtocol.substring(1)
}
const splittedBySlashes = rawWithoutProtocol.split('/')
let host = splittedBySlashes[0] || ''
const pathQueryHash = splittedBySlashes.slice(1).join('/') || ''
const splittedByQuestionMark = pathQueryHash.split('?')
const path = splittedByQuestionMark[0] || ''
let query = splittedByQuestionMark.length > 1 ? splittedByQuestionMark.slice(1).join('?') : ''
const hash = pathQueryHash.split('#').length > 1 ? pathQueryHash.split('#').slice(1).join('#') : ''
// Make sure query doesn't include the hash at the end
if (query.endsWith('#' + hash)) {
query = query.substring(0, query.length - hash.length - 1)
}
// Get port if provided
let port = null
if (host.includes(':')) {
const hostParts = host.split(':')
host = hostParts[0]
port = hostParts[1]
} else {
if (this._protocol === 'https') {
port = '443'
} else if (this._protocol === 'http') {
port = '80'
} else if (this._protocol === 'ftp') {
port = '21'
} else if (this._protocol === 'ftps') {
port = '990'
} else if (this._protocol === 'sftp' || this._protocol === 'ssh') {
port = '22'
} else if (this._protocol === 'telnet') {
port = '23'
} else if (this._protocol === 'gopher') {
port = '70'
} else {
port = '80'
}
}
this._hash = hash
this._path = path
this._query = query
this._host = host
this._port = port
if (this._query.length > 0) this._query = '?' + this._query
if (this._hash.length > 0) this._hash = '#' + this._hash
if (!this._protocol && port) {
if (port === '443') {
this._protocol = 'https'
} else if (port === '80') {
this._protocol = 'http'
} else if (port === '21') {
this._protocol = 'ftp'
} else if (port === '990') {
this._protocol = 'ftps'
} else if (port === '22') {
this._protocol = 'ssh'
} else if (port === '23') {
this._protocol = 'telnet'
} else if (port === '70') {
this._protocol = 'gopher'
} else {
this._protocol = 'http'
}
}
this._raw = this.format()
}
format (template = UrlTool.defaultFormattingTemplate) {
let result = template
result = result.replace(/{protocol}/g, this.protocol)
result = result.replace(/{hostWithoutPort}/g, this.hostWithoutPort)
result = result.replace(/{host}/g, this.host)
result = result.replace(/{path}\//g, this.pathWithSlash)
result = result.replace(/{path}/g, this.path)
result = result.replace(/{query}/g, this.query)
result = result.replace(/{hash}/g, this.hash)
result = result.replace(/{port}/g, this.__portwcolumn)
return result
}
toString () {
return this.format()
}
get protocol() {
return this._protocol
}
set protocol(n) {
this._protocol = n
this._parse()
}
get hostWithoutPort () {
return this._host
}
set hostWithoutPort (_) {
throw new Error('Can\'t set hostWithoutPort. Use host instead.')
}
get host() {
return this._host + this.__portwcolumn
}
set host(n) {
this._host = n
this._parse()
}
get __portwcolumn() {
if (this._port.length <= 0) return ''
if ((this.protocol === 'https' && this._port === '443') || (this.protocol === 'http' && this._port === '80')) return ''
return ':' + this._port
}
get port() {
if (this._port.length <= 0) return ''
return this._port
}
set port(n) {
this._port = n
this._parse()
}
get path() {
return this._path
}
set path(n) {
this._path = n
this._parse()
}
get pathWithSlash () {
if (this._path.length <= 0) return ''
if (this.query.length > 0) return this.path
return this._path.endsWith('/') ? this._path : this._path + '/'
}
set pathWithSlash (_) {
throw new Error('Can\'t set pathWithSlash. Use path instead.')
}
get query() {
return this._query
}
set query(n) {
this._query = n
this._parse()
}
get hash() {
return this._hash
}
set hash(n) {
this._hash = n
this._parse()
}
}
}
// Export:
try {
if (module && module.exports) {
module.exports = UrlTool
}
} catch {}
try {
if (window) {
window.UrlTool = UrlTool
}
} catch {}