From 881ecb46e39286b0c2b3c32fe61dca9377176884 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Wed, 3 Aug 2022 13:00:16 -0400 Subject: [PATCH] feat: improve regex pattern --- dist/index.js | 11 ++++++----- dist/index.mjs | 11 ++++++----- src/index.js | 11 ++++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/dist/index.js b/dist/index.js index d9100ec..8b54fee 100755 --- a/dist/index.js +++ b/dist/index.js @@ -43,7 +43,7 @@ var normalizeUrl__default = /*#__PURE__*/_interopDefaultLegacy(normalizeUrl); 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); @@ -72,14 +72,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."); diff --git a/dist/index.mjs b/dist/index.mjs index 0e255fd..40f37d6 100755 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -36,7 +36,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); @@ -65,14 +65,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."); diff --git a/src/index.js b/src/index.js index 3981b89..d7d9636 100644 --- a/src/index.js +++ b/src/index.js @@ -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) @@ -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.")