Skip to content

Commit 28ce8db

Browse files
committed
Apply g flag as default, remove keyword 'all', and tweak test cases.
1 parent 9c19736 commit 28ce8db

File tree

6 files changed

+163
-116
lines changed

6 files changed

+163
-116
lines changed

lib/Builder.js

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Builder {
8080
this._regEx = []
8181

8282
/** @var {string} _modifiers Raw modifier to apply on. */
83-
this._modifiers = ''
83+
this._modifiers = 'g'
8484

8585
/** @var {number} _lastMethodType Type of last method, to avoid invalid builds. */
8686
this._lastMethodType = METHOD_TYPE_BEGIN
@@ -379,10 +379,6 @@ class Builder {
379379
/* MODIFIER MAPPER */
380380
/**********************************************************/
381381

382-
all() {
383-
return this._addUniqueModifier('g')
384-
}
385-
386382
multiLine() {
387383
return this._addUniqueModifier('m')
388384
}
@@ -486,6 +482,7 @@ class Builder {
486482
this._regEx.push(condition)
487483
return this
488484
}
485+
489486
/**
490487
* Validate method call. This will throw an exception if the called method makes no sense at this point.
491488
* Will add the current type as the last method type.
@@ -619,6 +616,8 @@ class Builder {
619616
}
620617

621618
/**
619+
* Clone a new builder object.
620+
*
622621
* @return {Builder}
623622
*/
624623
clone() {
@@ -633,6 +632,18 @@ class Builder {
633632
return clone
634633
}
635634

635+
/**
636+
* Remote specific flag.
637+
*
638+
* @param {string} flag
639+
* @return {Builder}
640+
*/
641+
removeModifier(flag) {
642+
this._modifiers.replace(flag, '')
643+
644+
return this
645+
}
646+
636647
/**********************************************************/
637648
/* REGEX METHODS */
638649
/**********************************************************/
@@ -645,6 +656,52 @@ class Builder {
645656
const regexp = this.get()
646657
return regexp.test.apply(regexp, arguments)
647658
}
659+
660+
/**********************************************************/
661+
/* ADDITIONAL METHODS */
662+
/**********************************************************/
663+
664+
/**
665+
* Just like test in RegExp, but reset lastIndex.
666+
*
667+
* @param {string} target
668+
* @return {boolean}
669+
*/
670+
isMatching(target) {
671+
const result = this.test(target)
672+
this.get().lastIndex = 0
673+
return result
674+
}
675+
676+
/**
677+
* Just like match in String, but reset lastIndex.
678+
*
679+
* @param {string} target
680+
* @return {array|null}
681+
*/
682+
getMatch(target) {
683+
const regex = this.get()
684+
const result = regex.exec(target)
685+
regex.lastIndex = 0
686+
return result
687+
}
688+
689+
/**
690+
* Get all matches, just like loop for RegExp.exec.
691+
* @param {string} target
692+
*/
693+
getMatches(target) {
694+
const result = []
695+
const regex = this.get()
696+
let temp = null
697+
698+
while(temp = regex.exec(target)) {
699+
result.push(temp)
700+
}
701+
regex.lastIndex = 0
702+
703+
return result
704+
}
648705
}
649706

650707
module.exports = Builder

lib/Language/Helpers/methodMatch.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const mapper = {
2323
'new line': { 'class': SimpleMethod, 'method': 'newLine' },
2424
'whitespace': { 'class': SimpleMethod, 'method': 'whitespace' },
2525
'no whitespace': { 'class': SimpleMethod, 'method': 'noWhitespace' },
26-
'all': { 'class': SimpleMethod, 'method': 'all' },
2726
'anything': { 'class': SimpleMethod, 'method': 'any' },
2827
'tab': { 'class': SimpleMethod, 'method': 'atb' },
2928
'digit': { 'class': SimpleMethod, 'method': 'digit' },

test/builder-test.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const assert = require('assert')
44
const SRL = require('../lib/Builder')
55

6-
describe('Builder Test', () => {
6+
describe('Builder isMatching', () => {
77
it('Simple Phone Number Format', () => {
88
const regex = new SRL()
99
.startsWith()
@@ -15,12 +15,12 @@ describe('Builder Test', () => {
1515
.digit().onceOrMore()
1616
.mustEnd()
1717

18-
assert.ok(regex.test('+49 123-45'))
19-
assert.ok(regex.exec('+492 1235-4'))
20-
assert.ok(!regex.test('+49 123 45'))
21-
assert.ok(!regex.exec('49 123-45'))
22-
assert.ok(!regex.test('a+49 123-45'))
23-
assert.ok(!regex.test('+49 123-45b'))
18+
assert.ok(regex.isMatching('+49 123-45'))
19+
assert.ok(regex.isMatching('+492 1235-4'))
20+
assert.ok(!regex.isMatching('+49 123 45'))
21+
assert.ok(!regex.isMatching('49 123-45'))
22+
assert.ok(!regex.isMatching('a+49 123-45'))
23+
assert.ok(!regex.isMatching('+49 123-45b'))
2424
})
2525

2626
it('Simple Email Format', () => {
@@ -39,15 +39,14 @@ describe('Builder Test', () => {
3939
.letter().atLeast(2)
4040
.mustEnd()
4141
.caseInsensitive()
42-
.get() // Use get() to test resulting RegExp object.
43-
44-
assert.equal('sample@example.com'.match(regex)[0], 'sample@example.com')
45-
assert.equal(regex.exec('super-He4vy.add+ress@top-Le.ve1.domains'), 'super-He4vy.add+ress@top-Le.ve1.domains')
46-
assert.ok(!regex.test('sample.example.com'))
47-
assert.ok(!regex.test('missing@tld'))
48-
assert.ok(!regex.test('hav ing@spac.es'))
49-
assert.ok(!regex.test('no@pe.123'))
50-
assert.ok(!regex.test('invalid@email.com123'))
42+
43+
assert.equal(regex.getMatch('sample@example.com')[0], 'sample@example.com')
44+
assert.equal(regex.getMatch('super-He4vy.add+ress@top-Le.ve1.domains')[0], 'super-He4vy.add+ress@top-Le.ve1.domains')
45+
assert.ok(!regex.isMatching('sample.example.com'))
46+
assert.ok(!regex.isMatching('missing@tld'))
47+
assert.ok(!regex.isMatching('hav ing@spac.es'))
48+
assert.ok(!regex.isMatching('no@pe.123'))
49+
assert.ok(!regex.isMatching('invalid@email.com123'))
5150
})
5251

5352
it('Capture Group', () => {
@@ -65,14 +64,13 @@ describe('Builder Test', () => {
6564
query.letter().onceOrMore()
6665
})
6766
.literally('.')
68-
.get()
6967

70-
assert.ok(regex.test('my favorite color: blue.'))
71-
assert.ok(regex.test('my favorite colour is green.'))
72-
assert.ok(!regex.test('my favorite colour is green!'))
68+
assert.ok(regex.isMatching('my favorite color: blue.'))
69+
assert.ok(regex.isMatching('my favorite colour is green.'))
70+
assert.ok(!regex.isMatching('my favorite colour is green!'))
7371

7472
const testcase = 'my favorite colour is green. And my favorite color: yellow.'
75-
const matches = testcase.match(regex)
73+
const matches = regex.getMatch(testcase)
7674
assert.equal(matches[1], 'green')
7775
})
7876

@@ -86,12 +84,12 @@ describe('Builder Test', () => {
8684
.tab()
8785
.mustEnd()
8886
.multiLine()
89-
.get()
87+
9088
const target = `
9189
ba\t
9290
aaabbb
9391
`
94-
assert.ok(regex.test(target))
92+
assert.ok(regex.isMatching(target))
9593

9694
const regex2 = new SRL()
9795
.startsWith()
@@ -101,10 +99,10 @@ describe('Builder Test', () => {
10199
.onceOrMore()
102100
.literally('b')
103101
.mustEnd()
104-
.get()
102+
105103
const target2 = `a
106104
b`
107-
assert.ok(regex2.test(target2))
105+
assert.ok(regex2.isMatching(target2))
108106
})
109107

110108
it('Replace', () => {
@@ -133,28 +131,25 @@ describe('Builder Test', () => {
133131
.whitespace().optional()
134132
.lazy()
135133
})
136-
.get()
137134

138-
const matches = ',, '.match(regex)
135+
const matches = regex.getMatch(',, ')
139136
assert.equal(matches[1], ',,')
140137
assert.notEqual(matches[1], ',, ')
141138

142139
const regex2 = new SRL()
143140
.literally(',')
144141
.atLeast(1)
145142
.lazy()
146-
.get()
147143

148-
const matches2 = regex2.exec(',,,,,')
144+
const matches2 = regex2.getMatch(',,,,,')
149145
assert.equal(matches2[0], ',')
150146
assert.notEqual(matches2[0], ',,,,,')
151147

152148
})
153149

154-
it('Global', () => {
150+
it('Global as Default', () => {
155151
const regex = new SRL()
156152
.literally('a')
157-
.all()
158153
.get()
159154

160155
let count = 0
@@ -169,9 +164,9 @@ describe('Builder Test', () => {
169164
.raw('b[a-z]r')
170165
.raw(/\d+/)
171166

172-
assert.ok(regex.test('foobzr123'))
173-
assert.ok(regex.test('foobar1'))
174-
assert.ok(!regex.test('fooa'))
175-
assert.ok(!regex.test('foobar'))
167+
assert.ok(regex.isMatching('foobzr123'))
168+
assert.ok(regex.isMatching('foobar1'))
169+
assert.ok(!regex.isMatching('fooa'))
170+
assert.ok(!regex.isMatching('foobar'))
176171
})
177172
})

test/cache-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Cache', () => {
1212
})
1313

1414
it('In interpreter', () => {
15-
const RE = /(?:a)/
15+
const RE = /(?:a)/g
1616
const query = new Interpreter('Literally "a"')
1717
assert.deepEqual(query.get(), RE)
1818

0 commit comments

Comments
 (0)