forked from SimpleRegex/SRL-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder-test.js
More file actions
209 lines (174 loc) · 6.04 KB
/
builder-test.js
File metadata and controls
209 lines (174 loc) · 6.04 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict'
const assert = require('assert')
const SRL = require('../')
describe('Builder isMatching', () => {
it('Simple Phone Number Format', () => {
const regex = new SRL()
.startsWith()
.literally('+')
.digit().between(1, 3)
.literally(' ')
.digit().between(3, 4)
.literally('-')
.digit().onceOrMore()
.mustEnd()
assert.ok(regex.isMatching('+49 123-45'))
assert.ok(regex.isMatching('+492 1235-4'))
assert.ok(!regex.isMatching('+49 123 45'))
assert.ok(!regex.isMatching('49 123-45'))
assert.ok(!regex.isMatching('a+49 123-45'))
assert.ok(!regex.isMatching('+49 123-45b'))
})
it('Simple Email Format', () => {
const regex = new SRL()
.startsWith()
.anyOf((query) => {
query.digit().letter().oneOf('._%+-')
})
.onceOrMore()
.literally('@')
.anyOf((query) => {
query.digit().letter().oneOf('.-')
})
.onceOrMore()
.literally('.')
.letter().atLeast(2)
.mustEnd()
.caseInsensitive()
assert.equal(regex.getMatch('sample@example.com')[0], 'sample@example.com')
assert.equal(regex.getMatch('super-He4vy.add+ress@top-Le.ve1.domains')[0], 'super-He4vy.add+ress@top-Le.ve1.domains')
assert.ok(!regex.isMatching('sample.example.com'))
assert.ok(!regex.isMatching('missing@tld'))
assert.ok(!regex.isMatching('hav ing@spac.es'))
assert.ok(!regex.isMatching('no@pe.123'))
assert.ok(!regex.isMatching('invalid@email.com123'))
})
it('Capture Group', () => {
const regex = new SRL()
.literally('colo')
.optional('u')
.literally('r')
.anyOf((query) => {
query.literally(':').and((query) => {
query.literally(' is')
})
})
.whitespace()
.capture((query) => {
query.letter().onceOrMore()
})
.literally('.')
assert.ok(regex.isMatching('my favorite color: blue.'))
assert.ok(regex.isMatching('my favorite colour is green.'))
assert.ok(!regex.isMatching('my favorite colour is green!'))
const testcase = 'my favorite colour is green. And my favorite color: yellow.'
const matches = regex.getMatch(testcase)
assert.equal(matches[1], 'green')
})
it('More Methods', () => {
const regex = new SRL()
.noWhitespace()
.literally('a')
.ifFollowedBy((builder) => {
return builder.noCharacter()
})
.tab()
.mustEnd()
.multiLine()
const target = `
ba\t
aaabbb
`
assert.ok(regex.isMatching(target))
const regex2 = new SRL()
.startsWith()
.literally('a')
.newLine()
.whitespace()
.onceOrMore()
.literally('b')
.mustEnd()
const target2 = `a
b`
assert.ok(regex2.isMatching(target2))
})
it('Replace', () => {
const regex = new SRL()
.capture((query) => {
query.anyCharacter().onceOrMore()
})
.whitespace()
.capture((query) => {
query.digit().onceOrMore()
})
.literally(', ')
.capture((query) => {
query.digit().onceOrMore()
})
.caseInsensitive()
.get()
assert.equal('April 15, 2003'.replace(regex, '$1 1, $3'), 'April 1, 2003')
})
it('Lazyness', () => {
const regex = new SRL()
.capture((query) => {
query.literally(',').twice()
.whitespace().optional()
.lazy()
})
const matches = regex.getMatch(',, ')
assert.equal(matches[1], ',,')
assert.notEqual(matches[1], ',, ')
const regex2 = new SRL()
.literally(',')
.atLeast(1)
.lazy()
const matches2 = regex2.getMatch(',,,,,')
assert.equal(matches2[0], ',')
assert.notEqual(matches2[0], ',,,,,')
})
it('Global as Default', () => {
const regex = new SRL()
.literally('a')
.get()
let count = 0
'aaa'.replace(regex, () => count++)
assert.equal(count, 3)
})
it('Raw', () => {
const regex = new SRL()
.literally('foo')
.raw('b[a-z]r')
.raw(/\d+/)
assert.ok(regex.isMatching('foobzr123'))
assert.ok(regex.isMatching('foobar1'))
assert.ok(!regex.isMatching('fooa'))
assert.ok(!regex.isMatching('foobar'))
})
it('Remove modifier', () => {
const regex = new SRL()
.literally('foo')
.removeModifier('g')
.get()
assert.deepEqual(regex, /(?:foo)/)
})
it('Stores named captures', () => {
const regex_new = new SRL("capture (anything once or more) as first");
const testcase = 'hello world';
let matches_new = regex_new.getMatch(testcase)
assert.equal(matches_new["first"], 'hello world')
const regex_cached = new SRL("capture (anything once or more) as first");
let matches_cached = regex_cached.getMatch(testcase)
assert.equal(matches_cached["first"], 'hello world')
})
it('Stores named captures with implodeString', () => {
const query = "begin with literally 'hello '\ncapture (anything once or more) as first";
const regex_new = new SRL(query);
const testcase = 'hello world';
let matches_new = regex_new.getMatch(testcase)
assert.equal(matches_new["first"], 'world')
const regex_cached = new SRL(query);
let matches_cached = regex_cached.getMatch(testcase)
assert.equal(matches_cached["first"], 'world')
})
})