Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple of breaking changes: #25

Merged
merged 6 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Have an idea? Found a bug? See [how to contribute][contributing].

## :sparkling_heart: Support my projects

I open-source almost everything I can, and I try to reply everyone needing help using these projects. Obviously,
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously,
this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it).

However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
Expand Down
43 changes: 31 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ 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.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 +74,8 @@ function parsePath(url) {
parts = url.split("/");
if (output.protocol !== "file") {
output.resource = parts.shift();
} else {
output.resource = "";
}

// user@domain
Expand All @@ -81,18 +90,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 +126,8 @@ function parsePath(url) {
}

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parse-path",
"version": "3.0.4",
"version": "4.0.0",
"description": "Parse paths (local paths, urls: ssh/git/etc)",
"main": "lib/index.js",
"directories": {
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