1+ /* eslint-disable no-useless-escape */
12import {
23 anyOf ,
4+ buildRegExp ,
35 charClass ,
46 charRange ,
57 digit ,
@@ -9,11 +11,16 @@ import {
911 nonWord ,
1012 oneOrMore ,
1113 optional ,
14+ type RegexSequence ,
1215 whitespace ,
1316 word ,
1417 zeroOrMore ,
1518} from '../..' ;
1619
20+ function u ( sequence : RegexSequence ) {
21+ return buildRegExp ( sequence , { unicode : true } ) ;
22+ }
23+
1724test ( '`charClass` base cases' , ( ) => {
1825 expect ( charClass ( charRange ( 'a' , 'z' ) ) ) . toEqualRegex ( / [ a - z ] / ) ;
1926 expect ( charClass ( charRange ( 'a' , 'z' ) , charRange ( 'A' , 'Z' ) ) ) . toEqualRegex ( / [ a - z A - Z ] / ) ;
@@ -66,51 +73,181 @@ test('`charRange` throws on incorrect arguments', () => {
6673 ) ;
6774} ) ;
6875
69- test ( '`anyOf` pattern' , ( ) => {
76+ test ( '`anyOf` handles basic cases pattern' , ( ) => {
77+ expect ( anyOf ( 'a' ) ) . toMatchString ( 'a' ) ;
7078 expect ( anyOf ( 'a' ) ) . toEqualRegex ( / [ a ] / ) ;
79+
80+ expect ( [ 'x' , anyOf ( 'a' ) , 'x' ] ) . toMatchString ( 'xax' ) ;
7181 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' ) ;
7286 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' ) ;
7391 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' ) ;
7496 expect ( [ 'x' , anyOf ( 'ab' ) , 'x' ] ) . toEqualRegex ( / x [ a b ] x / ) ;
7597} ) ;
7698
99+ test ( '`anyOf` throws on empty text' , ( ) => {
100+ expect ( ( ) => anyOf ( '' ) ) . toThrowErrorMatchingInlineSnapshot ( `"Expected at least one character"` ) ;
101+ } ) ;
102+
77103test ( '`anyOf` pattern with quantifiers' , ( ) => {
78104 expect ( [ 'x' , oneOrMore ( anyOf ( 'abc' ) ) , 'x' ] ) . toEqualRegex ( / x [ a b c ] + x / ) ;
79105 expect ( [ 'x' , optional ( anyOf ( 'abc' ) ) , 'x' ] ) . toEqualRegex ( / x [ a b c ] ? x / ) ;
80106 expect ( [ 'x' , zeroOrMore ( anyOf ( 'abc' ) ) , 'x' ] ) . toEqualRegex ( / x [ a b c ] * x / ) ;
81107} ) ;
82108
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 ( / [ \- \^ ] / ) ;
86119
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 ] / ) ;
89125} ) ;
90126
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+ } ) ;
95145
146+ test ( '`anyOf` handles special chars' , ( ) => {
147+ expect ( anyOf ( '.' ) ) . toMatchString ( '.' ) ;
148+ expect ( anyOf ( '.' ) ) . not . toMatchString ( 'a' ) ;
96149 expect ( anyOf ( '.' ) ) . toEqualRegex ( / [ . ] / ) ;
150+
151+ expect ( anyOf ( '*' ) ) . toMatchString ( '*' ) ;
152+ expect ( anyOf ( '*' ) ) . not . toMatchString ( 'a' ) ;
97153 expect ( anyOf ( '*' ) ) . toEqualRegex ( / [ * ] / ) ;
154+
155+ expect ( anyOf ( '+' ) ) . toMatchString ( '+' ) ;
156+ expect ( anyOf ( '+' ) ) . not . toMatchString ( 'a' ) ;
98157 expect ( anyOf ( '+' ) ) . toEqualRegex ( / [ + ] / ) ;
158+
159+ expect ( anyOf ( '?' ) ) . toMatchString ( '?' ) ;
160+ expect ( anyOf ( '?' ) ) . not . toMatchString ( 'a' ) ;
99161 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' ) ;
101177 expect ( anyOf ( '$' ) ) . toEqualRegex ( / [ $ ] / ) ;
178+
179+ expect ( anyOf ( '{' ) ) . toMatchString ( '{' ) ;
180+ expect ( anyOf ( '{' ) ) . not . toMatchString ( 'a' ) ;
102181 expect ( anyOf ( '{' ) ) . toEqualRegex ( / [ { ] / ) ;
182+
183+ expect ( anyOf ( '}' ) ) . toMatchString ( '}' ) ;
184+ expect ( anyOf ( '}' ) ) . not . toMatchString ( 'a' ) ;
103185 expect ( anyOf ( '}' ) ) . toEqualRegex ( / [ } ] / ) ;
186+
187+ expect ( anyOf ( '(' ) ) . toMatchString ( '(' ) ;
188+ expect ( anyOf ( '(' ) ) . not . toMatchString ( 'a' ) ;
104189 expect ( anyOf ( '(' ) ) . toEqualRegex ( / [ ( ] / ) ;
190+
191+ expect ( anyOf ( ')' ) ) . toMatchString ( ')' ) ;
192+ expect ( anyOf ( ')' ) ) . not . toMatchString ( 'a' ) ;
105193 expect ( anyOf ( ')' ) ) . toEqualRegex ( / [ ) ] / ) ;
194+
195+ expect ( anyOf ( '|' ) ) . toMatchString ( '|' ) ;
196+ expect ( anyOf ( '|' ) ) . not . toMatchString ( 'a' ) ;
106197 expect ( anyOf ( '|' ) ) . toEqualRegex ( / [ | ] / ) ;
198+
199+ expect ( anyOf ( '[' ) ) . toMatchString ( '[' ) ;
200+ expect ( anyOf ( '[' ) ) . not . toMatchString ( 'a' ) ;
107201 expect ( anyOf ( '[' ) ) . toEqualRegex ( / [ [ ] / ) ;
202+
203+ expect ( anyOf ( ']' ) ) . toMatchString ( ']' ) ;
204+ expect ( anyOf ( ']' ) ) . not . toMatchString ( 'a' ) ;
108205 expect ( anyOf ( ']' ) ) . toEqualRegex ( / [ \] ] / ) ;
206+
207+ expect ( anyOf ( '\\' ) ) . toMatchString ( '\\' ) ;
208+ expect ( anyOf ( '\\' ) ) . not . toMatchString ( 'a' ) ;
109209 expect ( anyOf ( '\\' ) ) . toEqualRegex ( / [ \\ ] / ) ;
110210} ) ;
111211
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' ) ;
114251} ) ;
115252
116253test ( '`negated` character class pattern' , ( ) => {
0 commit comments