1
+ /* eslint-disable no-useless-escape */
1
2
import {
2
3
anyOf ,
4
+ buildRegExp ,
3
5
charClass ,
4
6
charRange ,
5
7
digit ,
@@ -9,11 +11,16 @@ import {
9
11
nonWord ,
10
12
oneOrMore ,
11
13
optional ,
14
+ type RegexSequence ,
12
15
whitespace ,
13
16
word ,
14
17
zeroOrMore ,
15
18
} from '../..' ;
16
19
20
+ function u ( sequence : RegexSequence ) {
21
+ return buildRegExp ( sequence , { unicode : true } ) ;
22
+ }
23
+
17
24
test ( '`charClass` base cases' , ( ) => {
18
25
expect ( charClass ( charRange ( 'a' , 'z' ) ) ) . toEqualRegex ( / [ a - z ] / ) ;
19
26
expect ( charClass ( charRange ( 'a' , 'z' ) , charRange ( 'A' , 'Z' ) ) ) . toEqualRegex ( / [ a - z A - Z ] / ) ;
@@ -66,51 +73,181 @@ test('`charRange` throws on incorrect arguments', () => {
66
73
) ;
67
74
} ) ;
68
75
69
- test ( '`anyOf` pattern' , ( ) => {
76
+ test ( '`anyOf` handles basic cases pattern' , ( ) => {
77
+ expect ( anyOf ( 'a' ) ) . toMatchString ( 'a' ) ;
70
78
expect ( anyOf ( 'a' ) ) . toEqualRegex ( / [ a ] / ) ;
79
+
80
+ expect ( [ 'x' , anyOf ( 'a' ) , 'x' ] ) . toMatchString ( 'xax' ) ;
71
81
expect ( [ 'x' , anyOf ( 'a' ) , 'x' ] ) . toEqualRegex ( / x [ a ] x / ) ;
82
+
83
+ expect ( anyOf ( 'ab' ) ) . toMatchString ( 'a' ) ;
84
+ expect ( anyOf ( 'ab' ) ) . toMatchString ( 'b' ) ;
85
+ expect ( anyOf ( 'ab' ) ) . not . toMatchString ( 'c' ) ;
72
86
expect ( anyOf ( 'ab' ) ) . toEqualRegex ( / [ a b ] / ) ;
87
+
88
+ expect ( [ 'x' , anyOf ( 'ab' ) ] ) . toMatchString ( 'xa' ) ;
89
+ expect ( [ 'x' , anyOf ( 'ab' ) ] ) . toMatchString ( 'xb' ) ;
90
+ expect ( [ 'x' , anyOf ( 'ab' ) ] ) . not . toMatchString ( 'x0' ) ;
73
91
expect ( [ 'x' , anyOf ( 'ab' ) ] ) . toEqualRegex ( / x [ a b ] / ) ;
92
+
93
+ expect ( [ 'x' , anyOf ( 'ab' ) , 'x' ] ) . toMatchString ( 'xax' ) ;
94
+ expect ( [ 'x' , anyOf ( 'ab' ) , 'x' ] ) . toMatchString ( 'xbx' ) ;
95
+ expect ( [ 'x' , anyOf ( 'ab' ) , 'x' ] ) . not . toMatchString ( 'x0x' ) ;
74
96
expect ( [ 'x' , anyOf ( 'ab' ) , 'x' ] ) . toEqualRegex ( / x [ a b ] x / ) ;
75
97
} ) ;
76
98
99
+ test ( '`anyOf` throws on empty text' , ( ) => {
100
+ expect ( ( ) => anyOf ( '' ) ) . toThrowErrorMatchingInlineSnapshot ( `"Expected at least one character"` ) ;
101
+ } ) ;
102
+
77
103
test ( '`anyOf` pattern with quantifiers' , ( ) => {
78
104
expect ( [ 'x' , oneOrMore ( anyOf ( 'abc' ) ) , 'x' ] ) . toEqualRegex ( / x [ a b c ] + x / ) ;
79
105
expect ( [ 'x' , optional ( anyOf ( 'abc' ) ) , 'x' ] ) . toEqualRegex ( / x [ a b c ] ? x / ) ;
80
106
expect ( [ 'x' , zeroOrMore ( anyOf ( 'abc' ) ) , 'x' ] ) . toEqualRegex ( / x [ a b c ] * x / ) ;
81
107
} ) ;
82
108
83
- test ( '`anyOf` pattern escapes special characters' , ( ) => {
84
- expect ( anyOf ( 'abc-+.]\\' ) ) . toEqualRegex ( / [ a b c + . \] \\ - ] / ) ;
85
- } ) ;
109
+ test ( '`anyOf` handles hyphens' , ( ) => {
110
+ expect ( anyOf ( '^-' ) ) . toMatchString ( '^' ) ;
111
+ expect ( anyOf ( '^-' ) ) . toMatchString ( '-' ) ;
112
+ expect ( anyOf ( '^-' ) ) . not . toMatchString ( 'a' ) ;
113
+ expect ( anyOf ( '^-' ) ) . toEqualRegex ( / [ \^ \- ] / ) ;
114
+
115
+ expect ( anyOf ( '-^' ) ) . toMatchString ( '^' ) ;
116
+ expect ( anyOf ( '-^' ) ) . toMatchString ( '-' ) ;
117
+ expect ( anyOf ( '-^' ) ) . not . toMatchString ( 'a' ) ;
118
+ expect ( anyOf ( '-^' ) ) . toEqualRegex ( / [ \- \^ ] / ) ;
86
119
87
- test ( '`anyOf` pattern moves hyphen to the last position' , ( ) => {
88
- expect ( anyOf ( 'a-bc' ) ) . toEqualRegex ( / [ a b c - ] / ) ;
120
+ expect ( anyOf ( '-^a' ) ) . toMatchString ( '^' ) ;
121
+ expect ( anyOf ( '-^a' ) ) . toMatchString ( '-' ) ;
122
+ expect ( anyOf ( '-^a' ) ) . toMatchString ( 'a' ) ;
123
+ expect ( anyOf ( '-^a' ) ) . not . toMatchString ( 'b' ) ;
124
+ expect ( anyOf ( '-^a' ) ) . toEqualRegex ( / [ \- \^ a ] / ) ;
89
125
} ) ;
90
126
91
- test ( '`anyOf` pattern edge cases' , ( ) => {
92
- expect ( anyOf ( '^-' ) ) . toEqualRegex ( / [ \^ - ] / ) ;
93
- expect ( anyOf ( '-^' ) ) . toEqualRegex ( / [ \^ - ] / ) ;
94
- expect ( anyOf ( '-^a' ) ) . toEqualRegex ( / [ a ^ - ] / ) ;
127
+ test ( '`anyOf` handles hyphens in unicode mode' , ( ) => {
128
+ expect ( u ( anyOf ( '^-' ) ) ) . toMatchString ( '^' ) ;
129
+ expect ( u ( anyOf ( '^-' ) ) ) . toMatchString ( '^' ) ;
130
+ expect ( u ( anyOf ( '^-' ) ) ) . toMatchString ( '-' ) ;
131
+ expect ( u ( anyOf ( '^-' ) ) ) . not . toMatchString ( 'a' ) ;
132
+ expect ( u ( anyOf ( '^-' ) ) ) . toEqualRegex ( / [ \^ \- ] / u) ;
133
+
134
+ expect ( u ( anyOf ( '-^' ) ) ) . toMatchString ( '^' ) ;
135
+ expect ( u ( anyOf ( '-^' ) ) ) . toMatchString ( '-' ) ;
136
+ expect ( u ( anyOf ( '-^' ) ) ) . not . toMatchString ( 'a' ) ;
137
+ expect ( u ( anyOf ( '-^' ) ) ) . toEqualRegex ( / [ \- \^ ] / u) ;
138
+
139
+ expect ( u ( anyOf ( '-^a' ) ) ) . toMatchString ( '^' ) ;
140
+ expect ( u ( anyOf ( '-^a' ) ) ) . toMatchString ( '-' ) ;
141
+ expect ( u ( anyOf ( '-^a' ) ) ) . toMatchString ( 'a' ) ;
142
+ expect ( u ( anyOf ( '-^a' ) ) ) . not . toMatchString ( 'b' ) ;
143
+ expect ( u ( anyOf ( '-^a' ) ) ) . toEqualRegex ( / [ \- \^ a ] / u) ;
144
+ } ) ;
95
145
146
+ test ( '`anyOf` handles special chars' , ( ) => {
147
+ expect ( anyOf ( '.' ) ) . toMatchString ( '.' ) ;
148
+ expect ( anyOf ( '.' ) ) . not . toMatchString ( 'a' ) ;
96
149
expect ( anyOf ( '.' ) ) . toEqualRegex ( / [ . ] / ) ;
150
+
151
+ expect ( anyOf ( '*' ) ) . toMatchString ( '*' ) ;
152
+ expect ( anyOf ( '*' ) ) . not . toMatchString ( 'a' ) ;
97
153
expect ( anyOf ( '*' ) ) . toEqualRegex ( / [ * ] / ) ;
154
+
155
+ expect ( anyOf ( '+' ) ) . toMatchString ( '+' ) ;
156
+ expect ( anyOf ( '+' ) ) . not . toMatchString ( 'a' ) ;
98
157
expect ( anyOf ( '+' ) ) . toEqualRegex ( / [ + ] / ) ;
158
+
159
+ expect ( anyOf ( '?' ) ) . toMatchString ( '?' ) ;
160
+ expect ( anyOf ( '?' ) ) . not . toMatchString ( 'a' ) ;
99
161
expect ( anyOf ( '?' ) ) . toEqualRegex ( / [ ? ] / ) ;
100
- expect ( anyOf ( '^' ) ) . toEqualRegex ( / [ ^ ] / ) ;
162
+
163
+ expect ( anyOf ( '^' ) ) . toMatchString ( '^' ) ;
164
+ expect ( anyOf ( '^' ) ) . not . toMatchString ( 'a' ) ;
165
+ expect ( anyOf ( '^' ) ) . toEqualRegex ( / [ \^ ] / ) ;
166
+
167
+ expect ( anyOf ( '^0' ) ) . toMatchString ( '^' ) ;
168
+ expect ( anyOf ( '^0' ) ) . not . toMatchString ( 'a' ) ;
169
+ expect ( anyOf ( '^0' ) ) . toEqualRegex ( / [ \^ 0 ] / ) ;
170
+
171
+ expect ( anyOf ( '0^' ) ) . toMatchString ( '^' ) ;
172
+ expect ( anyOf ( '0^' ) ) . not . toMatchString ( 'a' ) ;
173
+ expect ( anyOf ( '0^' ) ) . toEqualRegex ( / [ 0 \^ ] / ) ;
174
+
175
+ expect ( anyOf ( '$' ) ) . toMatchString ( '$' ) ;
176
+ expect ( anyOf ( '$' ) ) . not . toMatchString ( 'a' ) ;
101
177
expect ( anyOf ( '$' ) ) . toEqualRegex ( / [ $ ] / ) ;
178
+
179
+ expect ( anyOf ( '{' ) ) . toMatchString ( '{' ) ;
180
+ expect ( anyOf ( '{' ) ) . not . toMatchString ( 'a' ) ;
102
181
expect ( anyOf ( '{' ) ) . toEqualRegex ( / [ { ] / ) ;
182
+
183
+ expect ( anyOf ( '}' ) ) . toMatchString ( '}' ) ;
184
+ expect ( anyOf ( '}' ) ) . not . toMatchString ( 'a' ) ;
103
185
expect ( anyOf ( '}' ) ) . toEqualRegex ( / [ } ] / ) ;
186
+
187
+ expect ( anyOf ( '(' ) ) . toMatchString ( '(' ) ;
188
+ expect ( anyOf ( '(' ) ) . not . toMatchString ( 'a' ) ;
104
189
expect ( anyOf ( '(' ) ) . toEqualRegex ( / [ ( ] / ) ;
190
+
191
+ expect ( anyOf ( ')' ) ) . toMatchString ( ')' ) ;
192
+ expect ( anyOf ( ')' ) ) . not . toMatchString ( 'a' ) ;
105
193
expect ( anyOf ( ')' ) ) . toEqualRegex ( / [ ) ] / ) ;
194
+
195
+ expect ( anyOf ( '|' ) ) . toMatchString ( '|' ) ;
196
+ expect ( anyOf ( '|' ) ) . not . toMatchString ( 'a' ) ;
106
197
expect ( anyOf ( '|' ) ) . toEqualRegex ( / [ | ] / ) ;
198
+
199
+ expect ( anyOf ( '[' ) ) . toMatchString ( '[' ) ;
200
+ expect ( anyOf ( '[' ) ) . not . toMatchString ( 'a' ) ;
107
201
expect ( anyOf ( '[' ) ) . toEqualRegex ( / [ [ ] / ) ;
202
+
203
+ expect ( anyOf ( ']' ) ) . toMatchString ( ']' ) ;
204
+ expect ( anyOf ( ']' ) ) . not . toMatchString ( 'a' ) ;
108
205
expect ( anyOf ( ']' ) ) . toEqualRegex ( / [ \] ] / ) ;
206
+
207
+ expect ( anyOf ( '\\' ) ) . toMatchString ( '\\' ) ;
208
+ expect ( anyOf ( '\\' ) ) . not . toMatchString ( 'a' ) ;
109
209
expect ( anyOf ( '\\' ) ) . toEqualRegex ( / [ \\ ] / ) ;
110
210
} ) ;
111
211
112
- test ( '`anyOf` throws on empty text' , ( ) => {
113
- expect ( ( ) => anyOf ( '' ) ) . toThrowErrorMatchingInlineSnapshot ( `"Expected at least one character"` ) ;
212
+ test ( '`anyof` matches special characters' , ( ) => {
213
+ expect ( anyOf ( 'a' ) ) . toMatchString ( 'a' ) ;
214
+ } ) ;
215
+
216
+ test ( '`anyof` matches special characters in unicode mode' , ( ) => {
217
+ expect ( u ( anyOf ( 'a' ) ) ) . toMatchString ( 'a' ) ;
218
+
219
+ expect ( u ( anyOf ( '.' ) ) ) . toMatchString ( '.' ) ;
220
+ expect ( u ( anyOf ( '.' ) ) ) . not . toMatchString ( 'a' ) ;
221
+ expect ( u ( anyOf ( '*' ) ) ) . toMatchString ( '*' ) ;
222
+ expect ( u ( anyOf ( '*' ) ) ) . not . toMatchString ( 'a' ) ;
223
+ expect ( u ( anyOf ( '+' ) ) ) . toMatchString ( '+' ) ;
224
+ expect ( u ( anyOf ( '+' ) ) ) . not . toMatchString ( 'a' ) ;
225
+ expect ( u ( anyOf ( '?' ) ) ) . toMatchString ( '?' ) ;
226
+ expect ( u ( anyOf ( '?' ) ) ) . not . toMatchString ( 'a' ) ;
227
+ expect ( u ( anyOf ( '^' ) ) ) . toMatchString ( '^' ) ;
228
+ expect ( u ( anyOf ( '^' ) ) ) . not . toMatchString ( 'a' ) ;
229
+ expect ( u ( anyOf ( '^0' ) ) ) . toMatchString ( '^' ) ;
230
+ expect ( u ( anyOf ( '^0' ) ) ) . not . toMatchString ( 'a' ) ;
231
+ expect ( u ( anyOf ( '0^' ) ) ) . toMatchString ( '^' ) ;
232
+ expect ( u ( anyOf ( '0^' ) ) ) . not . toMatchString ( 'a' ) ;
233
+ expect ( u ( anyOf ( '$' ) ) ) . toMatchString ( '$' ) ;
234
+ expect ( u ( anyOf ( '$' ) ) ) . not . toMatchString ( 'a' ) ;
235
+ expect ( u ( anyOf ( '{' ) ) ) . toMatchString ( '{' ) ;
236
+ expect ( u ( anyOf ( '{' ) ) ) . not . toMatchString ( 'a' ) ;
237
+ expect ( u ( anyOf ( '}' ) ) ) . toMatchString ( '}' ) ;
238
+ expect ( u ( anyOf ( '}' ) ) ) . not . toMatchString ( 'a' ) ;
239
+ expect ( u ( anyOf ( '(' ) ) ) . toMatchString ( '(' ) ;
240
+ expect ( u ( anyOf ( '(' ) ) ) . not . toMatchString ( 'a' ) ;
241
+ expect ( u ( anyOf ( ')' ) ) ) . toMatchString ( ')' ) ;
242
+ expect ( u ( anyOf ( ')' ) ) ) . not . toMatchString ( 'a' ) ;
243
+ expect ( u ( anyOf ( '|' ) ) ) . toMatchString ( '|' ) ;
244
+ expect ( u ( anyOf ( '|' ) ) ) . not . toMatchString ( 'a' ) ;
245
+ expect ( u ( anyOf ( '[' ) ) ) . toMatchString ( '[' ) ;
246
+ expect ( u ( anyOf ( '[' ) ) ) . not . toMatchString ( 'a' ) ;
247
+ expect ( u ( anyOf ( ']' ) ) ) . toMatchString ( ']' ) ;
248
+ expect ( u ( anyOf ( ']' ) ) ) . not . toMatchString ( 'a' ) ;
249
+ expect ( u ( anyOf ( '\\' ) ) ) . toMatchString ( '\\' ) ;
250
+ expect ( u ( anyOf ( '\\' ) ) ) . not . toMatchString ( 'a' ) ;
114
251
} ) ;
115
252
116
253
test ( '`negated` character class pattern' , ( ) => {
0 commit comments