Skip to content

Commit d84e2c2

Browse files
committed
feat(regex): EXT_JSON_REGEX
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 56cfd73 commit d84e2c2

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

src/regex/__snapshots__/json.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_JSON_REGEX > .json > should match extension ".json" 1`] = `
4+
{
5+
"0": ".json",
6+
"1": undefined,
7+
"groups": {
8+
"type": undefined,
9+
},
10+
}
11+
`;
12+
13+
exports[`unit:EXT_JSON_REGEX > .json > should match module id given last extension is ".json" 1`] = `
14+
{
15+
"0": ".json",
16+
"1": undefined,
17+
"groups": {
18+
"type": undefined,
19+
},
20+
}
21+
`;
22+
23+
exports[`unit:EXT_JSON_REGEX > .json > should match module id given only extension is ".json" 1`] = `
24+
{
25+
"0": ".json",
26+
"1": undefined,
27+
"groups": {
28+
"type": undefined,
29+
},
30+
}
31+
`;
32+
33+
exports[`unit:EXT_JSON_REGEX > .json5 > should match extension ".json5" 1`] = `
34+
{
35+
"0": ".json5",
36+
"1": "5",
37+
"groups": {
38+
"type": "5",
39+
},
40+
}
41+
`;
42+
43+
exports[`unit:EXT_JSON_REGEX > .json5 > should match module id given last extension is ".json5" 1`] = `
44+
{
45+
"0": ".json5",
46+
"1": "5",
47+
"groups": {
48+
"type": "5",
49+
},
50+
}
51+
`;
52+
53+
exports[`unit:EXT_JSON_REGEX > .json5 > should match module id given only extension is ".json5" 1`] = `
54+
{
55+
"0": ".json5",
56+
"1": "5",
57+
"groups": {
58+
"type": "5",
59+
},
60+
}
61+
`;
62+
63+
exports[`unit:EXT_JSON_REGEX > .jsonc > should match extension ".jsonc" 1`] = `
64+
{
65+
"0": ".jsonc",
66+
"1": "c",
67+
"groups": {
68+
"type": "c",
69+
},
70+
}
71+
`;
72+
73+
exports[`unit:EXT_JSON_REGEX > .jsonc > should match module id given last extension is ".jsonc" 1`] = `
74+
{
75+
"0": ".jsonc",
76+
"1": "c",
77+
"groups": {
78+
"type": "c",
79+
},
80+
}
81+
`;
82+
83+
exports[`unit:EXT_JSON_REGEX > .jsonc > should match module id given only extension is ".jsonc" 1`] = `
84+
{
85+
"0": ".jsonc",
86+
"1": "c",
87+
"groups": {
88+
"type": "c",
89+
},
90+
}
91+
`;

src/regex/__tests__/json.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_JSON_REGEX
3+
* @module ext-regex/regex/tests/unit-d/json
4+
*/
5+
6+
import type TEST_SUBJECT from '../json'
7+
8+
describe('unit-d:EXT_JSON_REGEX', () => {
9+
it('should be instance of RegExp', () => {
10+
expectTypeOf<typeof TEST_SUBJECT>().toEqualTypeOf<RegExp>()
11+
})
12+
})

src/regex/__tests__/json.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_JSON_REGEX
3+
* @module ext-regex/regex/tests/unit/json
4+
*/
5+
6+
import TEST_SUBJECT from '../json'
7+
8+
describe('unit:EXT_JSON_REGEX', () => {
9+
describe('.json', () => {
10+
it('should match extension ".json"', () => {
11+
expect(TEST_SUBJECT.exec('.json')).toMatchSnapshot()
12+
})
13+
14+
it('should match module id given last extension is ".json"', () => {
15+
expect(TEST_SUBJECT.exec('build.config.json')).toMatchSnapshot()
16+
})
17+
18+
it('should match module id given only extension is ".json"', () => {
19+
expect(TEST_SUBJECT.exec('package.json')).toMatchSnapshot()
20+
})
21+
})
22+
23+
describe('.json5', () => {
24+
it('should match extension ".json5"', () => {
25+
expect(TEST_SUBJECT.exec('.json5')).toMatchSnapshot()
26+
})
27+
28+
it('should match module id given last extension is ".json5"', () => {
29+
expect(TEST_SUBJECT.exec('build.config.json5')).toMatchSnapshot()
30+
})
31+
32+
it('should match module id given only extension is ".json5"', () => {
33+
expect(TEST_SUBJECT.exec('.markdownlint.json5')).toMatchSnapshot()
34+
})
35+
})
36+
37+
describe('.jsonc', () => {
38+
it('should match extension ".jsonc"', () => {
39+
expect(TEST_SUBJECT.exec('.jsonc')).toMatchSnapshot()
40+
})
41+
42+
it('should match module id given last extension is ".jsonc"', () => {
43+
expect(TEST_SUBJECT.exec('build.config.jsonc')).toMatchSnapshot()
44+
})
45+
46+
it('should match module id given only extension is ".jsonc"', () => {
47+
expect(TEST_SUBJECT.exec('.markdownlint.jsonc')).toMatchSnapshot()
48+
})
49+
})
50+
})

src/regex/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55

66
export { default as EXT_DTS_REGEX } from './dts'
77
export { default as EXT_JS_REGEX } from './js'
8+
export { default as EXT_JSON_REGEX } from './json'
89
export { default as EXT_TS_REGEX } from './ts'

src/regex/json.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_JSON_REGEX
3+
* @module ext-regex/regex/json
4+
*/
5+
6+
/**
7+
* JSON file extension regex.
8+
*
9+
* Supported extensions:
10+
*
11+
* - `.json`
12+
* - `.json5`
13+
* - `.jsonc`
14+
*
15+
* Captured groups:
16+
*
17+
* - `type`: Character after `'json'` in file extension
18+
*
19+
* @see https://regex101.com/r/YVxapd
20+
*
21+
* @const {RegExp} EXT_JSON_REGEX
22+
*/
23+
const EXT_JSON_REGEX: RegExp = /\.json(?<type>[5c])?(?=\s*$)/
24+
25+
export default EXT_JSON_REGEX

0 commit comments

Comments
 (0)