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