1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function normalizeUrl(pathComponents) {
|
10 | return pathComponents.reduce(function (accumulator, item) {
|
11 | switch (item) {
|
12 | case "..":
|
13 | accumulator.pop();
|
14 | break;
|
15 | case ".":
|
16 | break;
|
17 | default:
|
18 | accumulator.push(item);
|
19 | }
|
20 | return accumulator;
|
21 | }, []).join("/");
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | module.exports = function (urlString) {
|
29 | urlString = urlString.trim();
|
30 | if (/^data:/i.test(urlString)) {
|
31 | return urlString;
|
32 | }
|
33 | var protocol = urlString.indexOf("//") !== -1 ? urlString.split("//")[0] + "//" : "";
|
34 | var components = urlString.replace(new RegExp(protocol, "i"), "").split("/");
|
35 | var host = components[0].toLowerCase().replace(/\.$/, "");
|
36 | components[0] = "";
|
37 | var path = normalizeUrl(components);
|
38 | return protocol + host + path;
|
39 | }; |
\ | No newline at end of file |