Skip to content

Commit

Permalink
Merge branch 'improve-regex' of github.com:privatenumber/parse-url in…
Browse files Browse the repository at this point in the history
…to new-version
  • Loading branch information
IonicaBizau committed Aug 30, 2022
2 parents d1a4395 + 881ecb4 commit 0baab4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
11 changes: 6 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function normalizeUrl(urlString, options) {
const parseUrl = (url, normalize = false) => {

// Constants
const GIT_RE = /(^(git@|http(s)?:\/\/)([\w\.\-@]+)(\/|:))(([\~,\.\w,\-,\_,\/]+)(.git){0,1}((\/){0,1}))/;
const GIT_RE = /^(?:git@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;

const throwErr = msg => {
const err = new Error(msg);
Expand Down Expand Up @@ -317,14 +317,15 @@ const parseUrl = (url, normalize = false) => {

// Potential git-ssh urls
if (parsed.parse_failed) {
const matched = parsed.href.match(GIT_RE);
const matched = parsed.href.match(GIT_RE);

if (matched) {
parsed.protocols = ["ssh"];
parsed.protocol = "ssh";
parsed.resource = matched[4];
parsed.host = matched[4];
parsed.resource = matched[1];
parsed.host = matched[1];
parsed.user = "git";
parsed.pathname = `/${matched[6]}`;
parsed.pathname = `/${matched[2]}`;
parsed.parse_failed = false;
} else {
throwErr("URL parsing failed.");
Expand Down
11 changes: 6 additions & 5 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function normalizeUrl(urlString, options) {
const parseUrl = (url, normalize = false) => {

// Constants
const GIT_RE = /(^(git@|http(s)?:\/\/)([\w\.\-@]+)(\/|:))(([\~,\.\w,\-,\_,\/]+)(.git){0,1}((\/){0,1}))/;
const GIT_RE = /^(?:git@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;

const throwErr = msg => {
const err = new Error(msg);
Expand Down Expand Up @@ -311,14 +311,15 @@ const parseUrl = (url, normalize = false) => {

// Potential git-ssh urls
if (parsed.parse_failed) {
const matched = parsed.href.match(GIT_RE);
const matched = parsed.href.match(GIT_RE);

if (matched) {
parsed.protocols = ["ssh"];
parsed.protocol = "ssh";
parsed.resource = matched[4];
parsed.host = matched[4];
parsed.resource = matched[1];
parsed.host = matched[1];
parsed.user = "git";
parsed.pathname = `/${matched[6]}`;
parsed.pathname = `/${matched[2]}`;
parsed.parse_failed = false;
} else {
throwErr("URL parsing failed.");
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import normalizeUrl from "normalize-url";
const parseUrl = (url, normalize = false) => {

// Constants
const GIT_RE = /(^(git@|http(s)?:\/\/)([\w\.\-@]+)(\/|:))(([\~,\.\w,\-,\_,\/]+)(.git){0,1}((\/){0,1}))/
const GIT_RE = /^(?:git@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/

const throwErr = msg => {
const err = new Error(msg)
Expand Down Expand Up @@ -64,14 +64,15 @@ const parseUrl = (url, normalize = false) => {

// Potential git-ssh urls
if (parsed.parse_failed) {
const matched = parsed.href.match(GIT_RE)
const matched = parsed.href.match(GIT_RE)

if (matched) {
parsed.protocols = ["ssh"]
parsed.protocol = "ssh"
parsed.resource = matched[4]
parsed.host = matched[4]
parsed.resource = matched[1]
parsed.host = matched[1]
parsed.user = "git"
parsed.pathname = `/${matched[6]}`
parsed.pathname = `/${matched[2]}`
parsed.parse_failed = false
} else {
throwErr("URL parsing failed.")
Expand Down

0 comments on commit 0baab4f

Please sign in to comment.