-
Notifications
You must be signed in to change notification settings - Fork 4
/
example-regexp.ts
35 lines (30 loc) · 1.12 KB
/
example-regexp.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
import { buildRegExp, choiceOf, endOfString, repeat, startOfString } from '..';
test('example: mixing with RegExp literals (IPv4 address validator)', () => {
const octet = choiceOf(
/[0-9]/, // 0-9
/[1-9][0-9]/, // 10-99
/1[0-9][0-9]/, // 100-199
/2[0-4][0-9]/, // 200-249
/25[0-5]/, // 250-255
);
const regex = buildRegExp([
startOfString, // prettier break-line
repeat([octet, '.'], 3),
octet,
endOfString,
]);
expect(regex).toMatchString('0.0.0.0');
expect(regex).toMatchString('192.168.0.1');
expect(regex).toMatchString('1.99.100.249');
expect(regex).toMatchString('255.255.255.255');
expect(regex).toMatchString('123.45.67.89');
expect(regex).not.toMatchString('0.0.0.');
expect(regex).not.toMatchString('0.0.0.0.');
expect(regex).not.toMatchString('0.-1.0.0');
expect(regex).not.toMatchString('0.1000.0.0');
expect(regex).not.toMatchString('0.0.300.0');
expect(regex).not.toMatchString('255.255.255.256');
expect(regex).toEqualRegex(
/^(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/,
);
});