Skip to content

Commit

Permalink
A couple of breaking changes:
Browse files Browse the repository at this point in the history
 - When using a local url, set the pathname to be the same with the href
 - Do not normalize the local paths
 - Do not interpret //foo.com as a remote url.
 - Remove trailing slashes at the end
  • Loading branch information
IonicaBizau committed Oct 25, 2018
1 parent 388bea4 commit 3df3e93
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
44 changes: 32 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,21 @@ function parsePath(url) {
output.protocol = "file";
}

output.protocol = output.protocol || output.protocols[0] || (
isSsh(url)
? "ssh"
: url.charAt(1) === "/"
? ((url = url.substring(2)) && "")
: "file"
);
const firstChar = url.charAt(1)
if (output.href.startsWith("path")) debugger
if (!output.protocol) {
output.protocol = output.protocols[0]
if (!output.protocol) {
if (isSsh(url)) {
output.protocol = "ssh"
} else if (firstChar === "/" || firstChar === "~") {
url = url.substring(2)
output.protocol = "file"
} else {
output.protocol = "file"
}
}
}

if (protocolIndex !== -1) {
url = url.substring(protocolIndex + 3);
Expand All @@ -67,6 +75,8 @@ function parsePath(url) {
parts = url.split("/");
if (output.protocol !== "file") {
output.resource = parts.shift();
} else {
output.resource = "";
}

// user@domain
Expand All @@ -81,18 +91,26 @@ function parsePath(url) {
splits = output.resource.split(":");
if (splits.length === 2) {
output.resource = splits[0];
output.port = Number(splits[1]);
if (isNaN(output.port)) {
output.port = null;
parts.unshift(splits[1]);
if (splits[1]) {
output.port = Number(splits[1]);
if (isNaN(output.port)) {
output.port = null;
parts.unshift(splits[1]);
}
} else {
output.port = null
}
}

// Remove empty elements
parts = parts.filter(Boolean);

// Stringify the pathname
output.pathname = output.pathname || ((output.protocol !== "file" || output.href[0] === "/" ? "/" : "") + parts.join("/"));
if (output.protocol === "file") {
output.pathname = output.href
} else {
output.pathname = output.pathname || ((output.protocol !== "file" || output.href[0] === "/" ? "/" : "") + parts.join("/"));
}

// #some-hash
splits = output.pathname.split("#");
Expand All @@ -109,6 +127,8 @@ function parsePath(url) {
}

output.query = qs.parse(output.search);
output.href = output.href.replace(/\/$/, "")
output.pathname = output.pathname.replace(/\/$/, "")
return output;
}

Expand Down
17 changes: 2 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ const INPUTS = [
, search: ""
}
]
, [
"//ionicabizau.net/foo.js"
, {
protocols: []
, protocol: ""
, port: null
, resource: "ionicabizau.net"
, user: ""
, pathname: "/foo.js"
, hash: ""
, search: ""
}
]
, [
"http://domain.com/path/name?foo=bar&bar=42#some-hash"
, {
Expand Down Expand Up @@ -117,7 +104,7 @@ const INPUTS = [
, port: null
, resource: ""
, user: ""
, pathname: "path/to/file.png"
, pathname: "./path/to/file.png"
, hash: ""
, search: ""
}
Expand All @@ -130,7 +117,7 @@ const INPUTS = [
, port: null
, resource: ""
, user: ""
, pathname: ".path/to/file.png"
, pathname: "./.path/to/file.png"
, hash: ""
, search: ""
}
Expand Down

0 comments on commit 3df3e93

Please sign in to comment.