UNPKG

953 BJavaScriptView Raw
1"use strict";
2
3/* eslint-disable */
4
5/**
6 * @param {string[]} pathComponents
7 * @returns {string}
8 */
9function 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 }, /** @type {string[]} */[]).join("/");
22}
23
24/**
25 * @param {string} urlString
26 * @returns {string}
27 */
28module.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