-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathindex.test.ts
220 lines (207 loc) · 8.15 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* eslint-disable ts/no-unused-expressions */
import type { MagicRegExp, MagicRegExpMatchArray, StringCapturedBy } from '../src'
import { expectTypeOf } from 'expect-type'
import { describe, expect, it } from 'vitest'
import { anyOf, caseInsensitive, char, createRegExp, digit, exactly, global, maybe, multiline, oneOrMore } from '../src'
import { createInput } from '../src/core/internal'
describe('magic-regexp', () => {
it('works as a normal regexp', () => {
const regExp = createRegExp('in', [global])
expect('thing'.match(regExp)?.[0]).toMatchInlineSnapshot('"in"')
expect(regExp.test('thing')).toBeTruthy()
expect(regExp.lastIndex).toMatchInlineSnapshot('4')
expectTypeOf(regExp).not.toEqualTypeOf(RegExp)
})
it('collects flag type', () => {
const re_array_flag = createRegExp('.', [global, multiline])
expectTypeOf(re_array_flag).toEqualTypeOf<MagicRegExp<'/\\./gm', never, [], 'g' | 'm'>>()
const re_set_flag = createRegExp('.', new Set([global] as const))
expectTypeOf(re_set_flag).toEqualTypeOf<MagicRegExp<'/\\./g', never, [], 'g'>>()
})
it('sanitize string input', () => {
const escapeChars = '.*+?^${}()[]/'
const re = createRegExp(escapeChars)
expect(String(re)).toMatchInlineSnapshot(`"/\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\[\\]\\//"`)
expectTypeOf(re).toEqualTypeOf<
MagicRegExp<'/\\.\\*\\+\\?\\^\\$\\{\\}\\(\\)\\[\\]\\//', never, []>
>()
})
})
describe('inputs', () => {
it('createInput serializes to string', () => {
expect(`${createInput('\\s')}`).toEqual('\\s')
})
it('type infer group names when nesting createInput', () => {
expectTypeOf(createRegExp(createInput(exactly('\\s').groupedAs('groupName')))).toEqualTypeOf<
MagicRegExp<'/(?<groupName>\\s)/', 'groupName', ['(?<groupName>\\s)'], never>
>()
})
it('takes variadic args and flags', () => {
const regExp = createRegExp(
oneOrMore(digit).as('major'),
'.',
oneOrMore(digit).as('minor'),
maybe('.', oneOrMore(char).groupedAs('patch')),
[caseInsensitive],
)
const result = '3.4.1-beta'.match(regExp)
expect(Array.isArray(result)).toBeTruthy()
expect(result?.groups).toMatchInlineSnapshot(`
{
"major": "3",
"minor": "4",
"patch": "1-beta",
}
`)
expectTypeOf(regExp).toEqualTypeOf<
MagicRegExp<
'/(?<major>\\d+)\\.(?<minor>\\d+)(?:\\.(?<patch>.+))?/i',
'major' | 'minor' | 'patch',
['(?<major>\\d+)', '(?<minor>\\d+)', '(?<patch>.+)'],
'i'
>
>()
})
it('any', () => {
const regExp = createRegExp(anyOf('foo', 'bar'))
expect(regExp).toMatchInlineSnapshot('/\\(\\?:foo\\|bar\\)/')
expect(regExp.test('foo')).toBeTruthy()
expect(regExp.test('bar')).toBeTruthy()
expect(regExp.test('baz')).toBeFalsy()
})
it('before', () => {
const regExp = createRegExp(char.before('foo'))
expect(regExp).toMatchInlineSnapshot('/\\.\\(\\?=foo\\)/')
expect('bafoo'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"')
expect(regExp.test('foo')).toBeFalsy()
})
it('after', () => {
const regExp = createRegExp(char.after('foo'))
expect(regExp).toMatchInlineSnapshot('/\\(\\?<=foo\\)\\./')
expect('fooafoo'.match(regExp)?.[0]).toMatchInlineSnapshot('"a"')
expect(regExp.test('foo')).toBeFalsy()
})
it('notBefore', () => {
const regExp = createRegExp(exactly('bar').notBefore('foo'))
expect(regExp).toMatchInlineSnapshot('/bar\\(\\?!foo\\)/')
expect('barfoo'.match(regExp)).toBeFalsy()
})
it('notAfter', () => {
const regExp = createRegExp(exactly('bar').notAfter('foo'))
expect(regExp).toMatchInlineSnapshot('/\\(\\?<!foo\\)bar/')
expect('foobar'.match(regExp)).toBeFalsy()
expect('fooabar'.match(regExp)).toBeTruthy()
})
it('exactly', () => {
const pattern = exactly('test/thing')
expect(pattern.toString()).toMatchInlineSnapshot(`"test\\/thing"`)
expect(createRegExp(pattern).test('test/thing')).toBeTruthy()
})
it('times', () => {
expect(exactly('test').times.between(1, 3).toString()).toMatchInlineSnapshot('"(?:test){1,3}"')
expect(exactly('test').times.atLeast(3).toString()).toMatchInlineSnapshot('"(?:test){3,}"')
expect(exactly('test').times.atMost(3).toString()).toMatchInlineSnapshot('"(?:test){0,3}"')
expect(exactly('test').times(4).or('foo').toString()).toMatchInlineSnapshot(
'"(?:(?:test){4}|foo)"',
)
})
it('capture groups', () => {
const pattern = anyOf(anyOf('foo', 'bar').groupedAs('test'), exactly('baz').groupedAs('test2'))
const regexp = createRegExp(pattern)
expect('football'.match(regexp)?.groups).toMatchInlineSnapshot(`
{
"test": "foo",
"test2": undefined,
}
`)
expect('fobazzer'.match(regexp)?.groups).toMatchInlineSnapshot(`
{
"test": undefined,
"test2": "baz",
}
`)
expectTypeOf('fobazzer'.match(regexp)).toEqualTypeOf<MagicRegExpMatchArray<
typeof regexp
> | null>()
expectTypeOf('fobazzer'.match(regexp)?.groups).toEqualTypeOf<
Record<'test' | 'test2', string | undefined> | undefined
>()
// @ts-expect-error there should be no 'other' group
'fobazzer'.match(createRegExp(pattern))?.groups.other
for (const match of 'fobazzer'.matchAll(createRegExp(pattern, [global]))) {
expect(match.groups).toMatchInlineSnapshot(`
{
"test": undefined,
"test2": "baz",
}
`)
expectTypeOf(match.groups).toEqualTypeOf<Record<'test' | 'test2', string | undefined>>()
}
''.match(
createRegExp(
anyOf(anyOf('foo', 'bar').groupedAs('test'), exactly('baz').groupedAs('test2')).and(
digit.times(5).groupedAs('id').optionally(),
),
),
)?.groups?.id
})
it('named backreference to capture groups', () => {
const pattern = exactly('foo')
.groupedAs('fooGroup')
.and(exactly('bar').groupedAs('barGroup'))
.and('baz')
.and
.referenceTo('barGroup')
.and
.referenceTo('fooGroup')
.and
.referenceTo('barGroup')
expect('foobarbazbarfoobar'.match(createRegExp(pattern))).toMatchInlineSnapshot(`
[
"foobarbazbarfoobar",
"foo",
"bar",
]
`)
expectTypeOf(pattern.and.referenceTo).toBeCallableWith('barGroup')
// @ts-expect-error there is no 'bazgroup' capture group
pattern.and.referenceTo('bazgroup')
})
it('can type-safe access matched array with hint for corresponding capture group', () => {
const pattern = anyOf(
exactly('foo|?').grouped(),
exactly('bar').and(maybe('baz').grouped()).groupedAs('groupName'),
exactly('boo').times(2).grouped(),
anyOf(
exactly('a').times(3),
exactly('b').and(maybe('c|d?')).times.any(),
exactly('1')
.and(maybe(exactly('2').and(maybe('3')).and('2')))
.and('1'),
).grouped(),
).grouped()
const match = 'booboo'.match(createRegExp(pattern))
if (!match)
return expect(match).toBeTruthy()
expectTypeOf(match.length).toEqualTypeOf<7>()
expectTypeOf(match[0]).toEqualTypeOf<string | undefined>()
expectTypeOf(match[1]).toEqualTypeOf<
| StringCapturedBy<'((foo\\|\\?)|(?<groupName>bar(baz)?)|(boo){2}|(a{3}|(?:b(?:c\\|d\\?)?)*|1(?:23?2)?1))'>
| undefined
>()
// @ts-expect-error match result array marked as readonly and shouldn't be assigned to
match[1] = 'match result array marked as readonly'
let typedVar: string | undefined
// eslint-disable-next-line unused-imports/no-unused-vars, prefer-const
typedVar = match[1] // can be assign to typed variable
expectTypeOf(match[2]).toEqualTypeOf<StringCapturedBy<'(foo\\|\\?)'> | undefined>()
expectTypeOf(match[2]?.concat(match[3] || '')).toEqualTypeOf<string | undefined>()
expectTypeOf(match[3]).toEqualTypeOf<StringCapturedBy<'(?<groupName>bar(baz)?)'> | undefined>()
expectTypeOf(match[4]).toEqualTypeOf<StringCapturedBy<'(baz)'> | undefined>()
expectTypeOf(match[5]).toEqualTypeOf<StringCapturedBy<'(boo)'> | undefined>()
expectTypeOf(match[6]).toEqualTypeOf<
StringCapturedBy<'(a{3}|(?:b(?:c\\|d\\?)?)*|1(?:23?2)?1)'> | undefined
>()
expectTypeOf(match[7]).toEqualTypeOf<never>()
})
})