-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample-email.ts
43 lines (38 loc) · 1.1 KB
/
example-email.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
import {
anyOf,
buildRegExp,
charClass,
charRange,
digit,
endOfString,
oneOrMore,
repeat,
startOfString,
} from '..';
test('example: email validation', () => {
const usernameChars = charClass(charRange('a', 'z'), digit, anyOf('._%+-'));
const hostnameChars = charClass(charRange('a', 'z'), digit, anyOf('.-'));
const domainChars = charRange('a', 'z');
const regex = buildRegExp(
[
startOfString,
oneOrMore(usernameChars),
'@',
oneOrMore(hostnameChars),
'.',
repeat(domainChars, { min: 2 }),
endOfString,
],
{ ignoreCase: true },
);
expect(regex).toMatchString('[email protected]');
expect(regex).toMatchString('[email protected]');
expect(regex).toMatchString('[email protected]');
expect(regex).toMatchString('[email protected]');
expect(regex).not.toMatchString('@');
expect(regex).not.toMatchString('aaa@');
expect(regex).not.toMatchString('[email protected]');
expect(regex).not.toMatchString('@gmail.com');
// eslint-disable-next-line no-useless-escape
expect(regex).toEqualRegex(/^[a-z\d._%+\-]+@[a-z\d.\-]+\.[a-z]{2,}$/i);
});