-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.js
64 lines (64 loc) · 1.75 KB
/
index.spec.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
53
54
55
56
57
58
59
60
61
62
63
64
const {
whole,
repeat,
alpha,
numeric,
and,
or,
wildcard,
extra,
capture,
group,
matchers,
regex,
flags,
look
} = require('.')
describe('rexrex', () => {
test('regex', () => {
const rex = regex(
whole(or(numeric(7), capture(alpha(0, 3)), extra(matchers.ANY, matchers.LAZY))),
and(flags.GLOBAL, flags.INSENSITIVE)
)
expect(rex).toEqual(/^\d{7}|([A-z]{0,3})|.+?$/gi)
})
test('wildcard and lazy matchers', () => {
let rex = or(wildcard('2', matchers.LAZY), extra('4', false))
expect(rex).toEqual('2*?|4+')
rex = or(wildcard('2'), extra('4', true))
expect(rex).toEqual('2*|4+?')
})
test('flags', () => {
expect(flags).toEqual({
GLOBAL: 'g',
MULTI_LINE: 'm',
INSENSITIVE: 'i',
STICKY: 'y',
UNICODE: 'u'
})
})
test('capture/group', () => {
expect(capture('test')).toEqual('(test)')
expect(capture('')).toEqual('')
expect(capture()).toEqual('')
expect(capture('\\d+', 'number')).toEqual('(?<number>\\d+)')
expect(capture('\\d+', {})).toEqual('(\\d+)')
expect(group('test')).toEqual('(?:test)')
expect(group()).toEqual('')
})
test('ALL', () => expect(matchers.ALL).toEqual('(.|\\s)'))
test('Repeat', () => {
expect(repeat('8')).toEqual('8')
expect(repeat('8', 1)).toEqual('8{1}')
expect(repeat('8', 1, 2)).toEqual('8{1,2}')
expect(repeat('8', 1, Infinity)).toEqual('8{1,}')
})
test('Look ahead/behind', () => {
expect(look.ahead('8')).toEqual('(?=8)')
expect(look.ahead.positive('8')).toEqual('(?=8)')
expect(look.ahead.negative('8')).toEqual('(?!8)')
expect(look.behind('8')).toEqual('(?<=8)')
expect(look.behind.positive('8')).toEqual('(?<=8)')
expect(look.behind.negative('8')).toEqual('(?<!8)')
})
})