Skip to content

Commit 0baab4f

Browse files
committed
Merge branch 'improve-regex' of github.com:privatenumber/parse-url into new-version
2 parents d1a4395 + 881ecb4 commit 0baab4f

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

dist/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ function normalizeUrl(urlString, options) {
288288
const parseUrl = (url, normalize = false) => {
289289

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

293293
const throwErr = msg => {
294294
const err = new Error(msg);
@@ -317,14 +317,15 @@ const parseUrl = (url, normalize = false) => {
317317

318318
// Potential git-ssh urls
319319
if (parsed.parse_failed) {
320-
const matched = parsed.href.match(GIT_RE);
320+
const matched = parsed.href.match(GIT_RE);
321+
321322
if (matched) {
322323
parsed.protocols = ["ssh"];
323324
parsed.protocol = "ssh";
324-
parsed.resource = matched[4];
325-
parsed.host = matched[4];
325+
parsed.resource = matched[1];
326+
parsed.host = matched[1];
326327
parsed.user = "git";
327-
parsed.pathname = `/${matched[6]}`;
328+
parsed.pathname = `/${matched[2]}`;
328329
parsed.parse_failed = false;
329330
} else {
330331
throwErr("URL parsing failed.");

dist/index.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ function normalizeUrl(urlString, options) {
282282
const parseUrl = (url, normalize = false) => {
283283

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

287287
const throwErr = msg => {
288288
const err = new Error(msg);
@@ -311,14 +311,15 @@ const parseUrl = (url, normalize = false) => {
311311

312312
// Potential git-ssh urls
313313
if (parsed.parse_failed) {
314-
const matched = parsed.href.match(GIT_RE);
314+
const matched = parsed.href.match(GIT_RE);
315+
315316
if (matched) {
316317
parsed.protocols = ["ssh"];
317318
parsed.protocol = "ssh";
318-
parsed.resource = matched[4];
319-
parsed.host = matched[4];
319+
parsed.resource = matched[1];
320+
parsed.host = matched[1];
320321
parsed.user = "git";
321-
parsed.pathname = `/${matched[6]}`;
322+
parsed.pathname = `/${matched[2]}`;
322323
parsed.parse_failed = false;
323324
} else {
324325
throwErr("URL parsing failed.");

src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import normalizeUrl from "normalize-url";
3535
const parseUrl = (url, normalize = false) => {
3636

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

4040
const throwErr = msg => {
4141
const err = new Error(msg)
@@ -64,14 +64,15 @@ const parseUrl = (url, normalize = false) => {
6464

6565
// Potential git-ssh urls
6666
if (parsed.parse_failed) {
67-
const matched = parsed.href.match(GIT_RE)
67+
const matched = parsed.href.match(GIT_RE)
68+
6869
if (matched) {
6970
parsed.protocols = ["ssh"]
7071
parsed.protocol = "ssh"
71-
parsed.resource = matched[4]
72-
parsed.host = matched[4]
72+
parsed.resource = matched[1]
73+
parsed.host = matched[1]
7374
parsed.user = "git"
74-
parsed.pathname = `/${matched[6]}`
75+
parsed.pathname = `/${matched[2]}`
7576
parsed.parse_failed = false
7677
} else {
7778
throwErr("URL parsing failed.")

0 commit comments

Comments
 (0)