-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add parse function for extract repo name
- Loading branch information
1 parent
32df2ce
commit faffb3e
Showing
3 changed files
with
27 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function parseGitHubURI(path: string): string { | ||
const pattern = | ||
/^(?<domain>https:\/\/github.com\/)?(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\w.\/@-]+)?/; | ||
const g = pattern.exec(path)?.groups; | ||
if (g?.repo == null) { | ||
throw new Error('Invalid format'); | ||
} | ||
return g.repo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { parseGitHubURI } from '../../src/utils'; | ||
|
||
describe('parseGitHubURI', () => { | ||
it('returns as is when given `org/repo` format', () => { | ||
expect(parseGitHubURI('aster-mnch/octoget')).toBe('aster-mnch/octoget'); | ||
}); | ||
|
||
it('returns `org/repo` when given a full URI', () => { | ||
expect(parseGitHubURI('https://github.com/aster-mnch/octoget')).toBe('aster-mnch/octoget'); | ||
}); | ||
|
||
it('throws "Invalid format" error when given invalid syntax', () => { | ||
expect(() => parseGitHubURI('invalid')).toThrow('Invalid format'); | ||
}); | ||
}); |