Skip to content

Commit 375f356

Browse files
committed
feat(internal): PACKAGE_PATH_REGEX
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 194b14e commit 375f356

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @file Unit Tests - PACKAGE_PATH_REGEX
3+
* @module mlly/internal/tests/unit/PACKAGE_PATH_REGEX
4+
*/
5+
6+
import TEST_SUBJECT from '../regex-package-path'
7+
8+
describe('unit:internal/PACKAGE_PATH_REGEX', () => {
9+
it('should match if specifier is valid package path', () => {
10+
// Arrange
11+
const cases: [string, Record<string, string | undefined>][] = [
12+
[
13+
'@flex-development/mlly',
14+
{
15+
pkg: '@flex-development/mlly',
16+
scope: '@flex-development',
17+
subpath: undefined,
18+
version: undefined,
19+
version_prefix: undefined
20+
}
21+
],
22+
[
23+
'@flex-development/mlly@1.0.0-alpha.7',
24+
{
25+
pkg: '@flex-development/mlly',
26+
scope: '@flex-development',
27+
subpath: undefined,
28+
version: '1.0.0-alpha.7',
29+
version_prefix: undefined
30+
}
31+
],
32+
[
33+
'@flex-development/mlly@v1.0.0-alpha.7',
34+
{
35+
pkg: '@flex-development/mlly',
36+
scope: '@flex-development',
37+
subpath: undefined,
38+
version: '1.0.0-alpha.7',
39+
version_prefix: 'v'
40+
}
41+
],
42+
[
43+
'@flex-development/mlly@v1.0.0-alpha.7/dist/index.mjs',
44+
{
45+
pkg: '@flex-development/mlly',
46+
scope: '@flex-development',
47+
subpath: '/dist/index.mjs',
48+
version: '1.0.0-alpha.7',
49+
version_prefix: 'v'
50+
}
51+
],
52+
[
53+
'mlly',
54+
{
55+
pkg: 'mlly',
56+
scope: undefined,
57+
subpath: undefined,
58+
version: undefined,
59+
version_prefix: undefined
60+
}
61+
]
62+
]
63+
64+
// Act + Expect
65+
cases.forEach(([specifier, groups]) => {
66+
const match = TEST_SUBJECT.exec(specifier)
67+
68+
expect(match).to.not.be.null
69+
expect(match?.groups).to.deep.equal(groups)
70+
})
71+
})
72+
73+
it('should not match if specifier is not valid package path', () => {
74+
// Arrange
75+
const cases: string[] = [
76+
'#src',
77+
'./package.json',
78+
'@FLEX-DEVELOPMENT/MLLY',
79+
'MLLY',
80+
'node:fs',
81+
'node_modules/@flex-development/mlly',
82+
import.meta.url
83+
]
84+
85+
// Act + Expect
86+
cases.forEach(specifier => expect(TEST_SUBJECT.test(specifier)).to.be.false)
87+
})
88+
})

src/internal/regex-package-path.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @file Internal - PACKAGE_PATH_REGEX
3+
* @module mlly/internal/PACKAGE_PATH_REGEX
4+
*/
5+
6+
/**
7+
* Regex pattern matching bare specifiers beginning with valid package names.
8+
*
9+
* **Note**: Does **not** match specifiers beginning with `'node_modules'`.
10+
*
11+
* @see https://regex101.com/r/z0MPgj
12+
*
13+
* @const {RegExp} PACKAGE_PATH_REGEX
14+
*/
15+
const PACKAGE_PATH_REGEX: RegExp =
16+
/^(?!\S+:|node_modules)(?<pkg>(?:(?:(?<scope>@[\d*a-z~-][\w*.~-]*)\/)?[\da-z~-][\w.~-]*))@?(?<version_prefix>v)?(?<version>(?:(?:0|[1-9]\d*)\.){2}(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-z-][\da-z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-z-][\da-z-]*))*)?(?:\+[\da-z-]+(?:\.[\da-z-]+)*)?)?(?<subpath>\/.*)?(?<!.*(?:%2[Ff]|%5[Cc]))/
17+
18+
export default PACKAGE_PATH_REGEX

0 commit comments

Comments
 (0)