Skip to content

Commit fb00cbe

Browse files
committed
feat(regex): EXT_DTS_REGEX
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent d8fe2fb commit fb00cbe

File tree

11 files changed

+244
-1
lines changed

11 files changed

+244
-1
lines changed

__tests__/setup/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*/
55

66
import './chai'
7+
import './serializers'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @file Entry Point - Snapshot Serializers
3+
* @module tests/setup/serializers
4+
* @see https://vitest.dev/guide/snapshot.html#custom-serializer
5+
*/
6+
7+
import './regex-array'
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @file Snapshot Serializers - RegExpArray
3+
* @module tests/setup/serializers/regex-array
4+
*/
5+
6+
import type { RegExpArray } from '#tests/types'
7+
import { isNumber, isString, type Fn } from '@flex-development/tutils'
8+
import { get, omit } from 'radash'
9+
10+
expect.addSnapshotSerializer({
11+
/**
12+
* Prints the given `value`.
13+
*
14+
* @param {unknown} value - Value to print
15+
* @param {Fn<[unknown], string>} printer - Print function
16+
* @return {string} `value` as printable string
17+
*/
18+
print(value: unknown, printer: Fn<[unknown], string>): string {
19+
return printer(omit(value as RegExpArray, ['index', 'input']))
20+
},
21+
/**
22+
* Checks if the given `value` is a {@linkcode RegExpArray}.
23+
*
24+
* @param {unknown} value - Value to check
25+
* @return {value is RegExpArray} `true` if `value` is {@linkcode RegExpArray}
26+
*/
27+
test(value: unknown): value is RegExpArray {
28+
return (
29+
Array.isArray(value) &&
30+
isNumber(get(value, 'index')) &&
31+
isString(get(value, 'input'))
32+
)
33+
}
34+
})

__tests__/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @file Entry Point - Test Type Definitions
3+
* @module tests/types
4+
*/
5+
6+
export type { default as RegExpArray } from './reg-exp-array'

__tests__/types/reg-exp-array.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @file Test Type Definitions - RegExpArray
3+
* @module tests/types/RegExpArray
4+
*/
5+
6+
/**
7+
* A {@linkcode RegExpExecArray} or {@linkcode RegExpMatchArray}.
8+
*/
9+
type RegExpArray = RegExpExecArray | RegExpMatchArray
10+
11+
export type { RegExpArray as default }

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* @module ext-regex
44
*/
55

6-
export {}
6+
export * from './regex'

src/regex/__snapshots__/dts.snap

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`unit:EXT_DTS_REGEX > .d.cts > should match extension ".d.cts" 1`] = `
4+
{
5+
"0": ".d.cts",
6+
"1": "c",
7+
"groups": {
8+
"type": "c",
9+
},
10+
}
11+
`;
12+
13+
exports[`unit:EXT_DTS_REGEX > .d.cts > should match module id given last extension is ".d.cts" 1`] = `
14+
{
15+
"0": ".d.cts",
16+
"1": "c",
17+
"groups": {
18+
"type": "c",
19+
},
20+
}
21+
`;
22+
23+
exports[`unit:EXT_DTS_REGEX > .d.cts > should match module id given only extension is ".d.cts" 1`] = `
24+
{
25+
"0": ".d.cts",
26+
"1": "c",
27+
"groups": {
28+
"type": "c",
29+
},
30+
}
31+
`;
32+
33+
exports[`unit:EXT_DTS_REGEX > .d.mts > should match extension ".d.mts" 1`] = `
34+
{
35+
"0": ".d.mts",
36+
"1": "m",
37+
"groups": {
38+
"type": "m",
39+
},
40+
}
41+
`;
42+
43+
exports[`unit:EXT_DTS_REGEX > .d.mts > should match module id given last extension is ".d.mts" 1`] = `
44+
{
45+
"0": ".d.mts",
46+
"1": "m",
47+
"groups": {
48+
"type": "m",
49+
},
50+
}
51+
`;
52+
53+
exports[`unit:EXT_DTS_REGEX > .d.mts > should match module id given only extension is ".d.mts" 1`] = `
54+
{
55+
"0": ".d.mts",
56+
"1": "m",
57+
"groups": {
58+
"type": "m",
59+
},
60+
}
61+
`;
62+
63+
exports[`unit:EXT_DTS_REGEX > .d.ts > should match extension ".d.ts" 1`] = `
64+
{
65+
"0": ".d.ts",
66+
"1": undefined,
67+
"groups": {
68+
"type": undefined,
69+
},
70+
}
71+
`;
72+
73+
exports[`unit:EXT_DTS_REGEX > .d.ts > should match module id given last extension is ".d.ts" 1`] = `
74+
{
75+
"0": ".d.ts",
76+
"1": undefined,
77+
"groups": {
78+
"type": undefined,
79+
},
80+
}
81+
`;
82+
83+
exports[`unit:EXT_DTS_REGEX > .d.ts > should match module id given only extension is ".d.ts" 1`] = `
84+
{
85+
"0": ".d.ts",
86+
"1": undefined,
87+
"groups": {
88+
"type": undefined,
89+
},
90+
}
91+
`;

src/regex/__tests__/dts.spec-d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @file Type Tests - EXT_DTS_REGEX
3+
* @module ext-regex/regex/tests/unit-d/dts
4+
*/
5+
6+
import type TEST_SUBJECT from '../dts'
7+
8+
describe('unit-d:EXT_DTS_REGEX', () => {
9+
it('should be instance of RegExp', () => {
10+
expectTypeOf<typeof TEST_SUBJECT>().toEqualTypeOf<RegExp>()
11+
})
12+
})

src/regex/__tests__/dts.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @file Unit Tests - EXT_DTS_REGEX
3+
* @module ext-regex/regex/tests/unit/dts
4+
*/
5+
6+
import TEST_SUBJECT from '../dts'
7+
8+
describe('unit:EXT_DTS_REGEX', () => {
9+
describe('.d.cts', () => {
10+
it('should match extension ".d.cts"', () => {
11+
expect(TEST_SUBJECT.exec('.d.cts')).toMatchSnapshot()
12+
})
13+
14+
it('should match module id given last extension is ".d.cts"', () => {
15+
expect(TEST_SUBJECT.exec('dist/users.controller.d.cts')).toMatchSnapshot()
16+
})
17+
18+
it('should match module id given only extension is ".d.cts"', () => {
19+
expect(TEST_SUBJECT.exec('dist/index.d.cts')).toMatchSnapshot()
20+
})
21+
})
22+
23+
describe('.d.mts', () => {
24+
it('should match extension ".d.mts"', () => {
25+
expect(TEST_SUBJECT.exec('.d.mts')).toMatchSnapshot()
26+
})
27+
28+
it('should match module id given last extension is ".d.mts"', () => {
29+
expect(TEST_SUBJECT.exec('dist/users.controller.d.mts')).toMatchSnapshot()
30+
})
31+
32+
it('should match module id given only extension is ".d.mts"', () => {
33+
expect(TEST_SUBJECT.exec('dist/index.d.mts')).toMatchSnapshot()
34+
})
35+
})
36+
37+
describe('.d.ts', () => {
38+
it('should match extension ".d.ts"', () => {
39+
expect(TEST_SUBJECT.exec('.d.ts')).toMatchSnapshot()
40+
})
41+
42+
it('should match module id given last extension is ".d.ts"', () => {
43+
expect(TEST_SUBJECT.exec('dist/users.controller.d.ts')).toMatchSnapshot()
44+
})
45+
46+
it('should match module id given only extension is ".d.ts"', () => {
47+
expect(TEST_SUBJECT.exec('dist/index.d.ts')).toMatchSnapshot()
48+
})
49+
})
50+
})

src/regex/dts.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @file Regular Expressions - EXT_DTS_REGEX
3+
* @module ext-regex/regex/dts
4+
*/
5+
6+
/**
7+
* TypeScript declaration file extension regex.
8+
*
9+
* Supported extensions:
10+
*
11+
* - `.d.cts`
12+
* - `.d.mts`
13+
* - `.d.ts`
14+
*
15+
* Captured groups:
16+
*
17+
* - `type`: Letter between `'.'` and `'ts'` in file extension
18+
*
19+
* @see https://regex101.com/r/LD2eKA
20+
*
21+
* @const {RegExp} EXT_DTS_REGEX
22+
*/
23+
const EXT_DTS_REGEX: RegExp = /\.d\.(?<type>[cm])?ts(?=\s*$)/
24+
25+
export default EXT_DTS_REGEX

0 commit comments

Comments
 (0)