Skip to content

Commit 07dbaa9

Browse files
committed
refactor: move core magic-regexp type into separate file
1 parent 97cb27a commit 07dbaa9

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

src/core/types/magic-regexp.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const MagicRegExpSymbol = Symbol('MagicRegExp')
2+
3+
export type MagicRegExp<Value extends string, T = never> = RegExp & {
4+
[MagicRegExpSymbol]: T & Value
5+
}
6+
7+
type ExtractGroups<T extends MagicRegExp<string, string>> = T extends MagicRegExp<string, infer V>
8+
? V
9+
: never
10+
11+
export type MagicRegExpMatchArray<T extends MagicRegExp<string, string>> = Omit<
12+
RegExpMatchArray,
13+
'groups'
14+
> & {
15+
groups: Record<ExtractGroups<T>, string | undefined>
16+
}

src/index.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import type { Flag } from './core/flags'
22
import { Input, exactly } from './core/inputs'
3-
4-
const MagicRegExpSymbol = Symbol('MagicRegExp')
5-
6-
export type MagicRegExp<Value extends string, T = never> = RegExp & {
7-
[MagicRegExpSymbol]: T & Value
8-
}
3+
import type { MagicRegExp, MagicRegExpMatchArray } from './core/types/magic-regexp'
94

105
export const createRegExp = <Value extends string, NamedGroups extends string = never>(
116
raw: Input<Value, NamedGroups> | Value,
@@ -14,30 +9,17 @@ export const createRegExp = <Value extends string, NamedGroups extends string =
149

1510
export * from './core/flags'
1611
export * from './core/inputs'
17-
18-
type ExtractNamedGroups<T extends MagicRegExp<string, string>> = T extends MagicRegExp<
19-
string,
20-
infer V
21-
>
22-
? V
23-
: never
24-
25-
export type MagicRegExpMatchArray<T extends MagicRegExp<string, string>> = Omit<
26-
RegExpMatchArray,
27-
'groups'
28-
> & {
29-
groups: Record<ExtractNamedGroups<T>, string | undefined>
30-
}
12+
export * from './core/types/magic-regexp'
3113

3214
// Add additional overload to global String object types to allow for typed capturing groups
3315
declare global {
3416
interface String {
35-
match<T extends string, RegExp extends MagicRegExp<any, T>>(
36-
regexp: RegExp
37-
): MagicRegExpMatchArray<RegExp> | null
17+
match<T extends string, R extends MagicRegExp<any, T>>(
18+
regexp: R
19+
): MagicRegExpMatchArray<R> | null
3820

39-
matchAll<T extends string, RegExp extends MagicRegExp<any, T>>(
40-
regexp: RegExp
41-
): IterableIterator<MagicRegExpMatchArray<RegExp>>
21+
matchAll<T extends string, R extends MagicRegExp<any, T>>(
22+
regexp: R
23+
): IterableIterator<MagicRegExpMatchArray<R>>
4224
}
4325
}

0 commit comments

Comments
 (0)