Revision 48f76d70
Added by koszko about 2 years ago
common/patterns.js | ||
---|---|---|
7 | 7 |
|
8 | 8 |
const proto_regex = /^(\w+):\/\/(.*)$/; |
9 | 9 |
|
10 |
const user_re = "[^/?#@]+@" |
|
10 | 11 |
const domain_re = "[^/?#]+"; |
11 | 12 |
const path_re = "[^?#]*"; |
12 | 13 |
const query_re = "\\??[^#]*"; |
... | ... | |
15 | 16 |
|
16 | 17 |
const file_regex = new RegExp(`^(${path_re}).*`); |
17 | 18 |
|
19 |
const ftp_regex = new RegExp(`^(${user_re})?(${domain_re})(${path_re}).*`); |
|
20 |
|
|
18 | 21 |
function deconstruct_url(url) |
19 | 22 |
{ |
20 | 23 |
const proto_match = proto_regex.exec(url); |
... | ... | |
25 | 28 |
|
26 | 29 |
if (deco.proto === "file") { |
27 | 30 |
deco.path = file_regex.exec(proto_match[2])[1]; |
31 |
} else if (deco.proto === "ftp") { |
|
32 |
[deco.domain, deco.path] = ftp_regex.exec(proto_match[2]).slice(2, 4); |
|
28 | 33 |
} else { |
29 | 34 |
const http_match = http_regex.exec(proto_match[2]); |
30 | 35 |
if (!http_match) |
31 | 36 |
return undefined; |
32 | 37 |
[deco.domain, deco.path, deco.query] = http_match.slice(1, 4); |
33 |
deco.domain = deco.domain.split("."); |
|
34 | 38 |
} |
35 | 39 |
|
40 |
if (deco.domain) |
|
41 |
deco.domain = deco.domain.split("."); |
|
42 |
|
|
36 | 43 |
const leading_dash = deco.path[0] === "/"; |
37 | 44 |
deco.trailing_dash = deco.path[deco.path.length - 1] === "/"; |
38 | 45 |
deco.path = deco.path.split("/").filter(s => s !== ""); |
Also available in: Unified diff
add support for `ftp://' protocol