Skip to content

Commit

Permalink
feat: improve regex pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Aug 3, 2022
1 parent 110cdaa commit 881ecb4
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 @@ -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);
Expand Down Expand Up @@ -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.");
Expand Down
11 changes: 6 additions & 5 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.");
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 881ecb4

Please sign in to comment.