-
Notifications
You must be signed in to change notification settings - Fork 7
/
index_test.js
52 lines (50 loc) · 1.61 KB
/
index_test.js
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
import test from 'ava';
import {re} from '../esm/index.js';
test('Composing regular expressions', t => {
const RE_YEAR = /([0-9]{4})/;
const RE_MONTH = /([0-9]{2})/;
const RE_DAY = /([0-9]{2})/;
const RE_DATE = re`/^${RE_YEAR}-${RE_MONTH}-${RE_DAY}$/u`;
t.is(RE_DATE.source, '^([0-9]{4})-([0-9]{2})-([0-9]{2})$');
});
test('Setting flags', t => {
const regexp1 = re`/abc/gu`;
t.true(regexp1 instanceof RegExp);
t.is(regexp1.source, 'abc');
t.is(regexp1.flags, 'gu');
const regexp2 = re`/xyz/`;
t.true(regexp2 instanceof RegExp);
t.is(regexp2.source, 'xyz');
t.is(regexp2.flags, '');
});
test('Computed flags', t => {
const regexp = re`/abc/${'g'+'u'}`;
t.true(regexp instanceof RegExp);
t.is(regexp.source, 'abc');
t.is(regexp.flags, 'gu');
});
test('Simple, flag-less mode', t => {
const regexp = re`abc`;
t.true(regexp instanceof RegExp);
t.is(regexp.source, 'abc');
t.is(regexp.flags, '');
});
test('Escaping special characters in strings', t => {
t.is(re`/-${'.'}-/u`.source, '-\\.-');
});
test('Use “raw” backslashes like in regular expressions', t => {
t.is(re`/\./u`.source, '\\.');
});
test('Slashes don’t need to be escaped', t => {
t.true(re`/^/$/u`.test('/'));
});
test('Escaping backticks', t => {
// Must escape in static text:
const RE_BACKTICK = re`/^\`$/u`;
t.is(RE_BACKTICK.source, '^`$');
t.true(RE_BACKTICK.test('`'));
// No escaping of backticks in dynamic text:
const str = '`\\`';
t.is(re`/${str}/`.source, '`\\\\`');
// Single backslash in `str` is escaped inside regular expression
});