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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
import { jlog } from '../src/utils'
import { filter, matchAll } from '../src'
import { takeLeftWhile } from 'fp-ts/Array'
describe('Basic tests', () => {
it('should do it', () => {
expect(filter(/\s/, [1, '2', 3, '4'])).toEqual(['2', '4'])
expect(filter(/\n/, [1, '2', 3, '4'])).toEqual([1, 3])
expect(filter(/\T/, [1, 0, '4', ''])).toEqual([1, '4'])
expect(filter(/\F/, [1, 0, '4', ''])).toEqual([0, ''])
expect(filter(/\F\T/, [1, 0, '4', ''])).toEqual([])
expect(filter(/\s\T/, [1, 0, '4', ''])).toEqual(['4'])
})
it('should do it', () => {
// jlog(matchAll(/[age \n]+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ]))
// jlog(matchAll(/[age \n]+/, [ {}, { age: 1 }, '' ]))
// jlog(matchAll(/([age \n])+/, [ {}, { age: 1 }, { age: 2 }, { age: 0 }, '' ]))
// jlog(matchAll(/\n+/, [ '', 1, 2, 0, '6', 5, '' ]))
// jlog(takeLeftWhile((x: number) => x < 5)([1, 2, 3, 4, 5, 6]))
})
describe('matchAll', () => {
it('should match simple expression', () => {
expect(matchAll(/\n/, ['b', 1, 2, 'a', 3]).groups).toEqual([
{ value: 1, index: 1 },
{ value: 2, index: 2 },
{ value: 3, index: 4 },
])
})
it('should match simple expression (AND)', () => {
expect(matchAll(/\n\T/, [1, 0, 2, '4', '']).groups).toEqual([
{ value: 1, index: 0 },
{ value: 2, index: 2 },
])
})
it('should match object property matchers', () => {
expect(
matchAll(/[age \n][age \T]/, [
{},
{ age: 1 },
{ age: 2 },
{ age: 0 },
'',
]).groups,
).toEqual([
{ value: { age: 1 }, index: 1 },
{ value: { age: 2 }, index: 2 },
])
})
it('should match object property matchers', () => {
// FIXME: This is invalid match result
jlog(
matchAll(/([age \n][age \T])+/, [
{},
{ age: 1 },
{ age: 2 },
{ age: 0 },
'',
]).groups,
)
expect(
matchAll(/([age \n][age \T])+/, [
{},
{ age: 1 },
{ age: 2 },
{ age: 0 },
'',
]).groups,
).toEqual([
{ value: [{ age: 1 }, { age: 2 }], index: 1 },
{ value: [{ age: 2 }], index: 2 },
])
})
})
})
|