Skip to content

Commit be13927

Browse files
authored
Merge pull request #5 from SimpleRegex/dev
Release 0.2.0 to follow SRL standard
2 parents 9c19736 + ca38519 commit be13927

File tree

8 files changed

+189
-124
lines changed

8 files changed

+189
-124
lines changed

Diff for: README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ JavaScript implementation of [Simple Regex](https://simple-regex.com/) :tada::ta
99
> Because of the JavaScript regex engine, there is something different from [Simple Regex](https://simple-regex.com/)
1010
- NOT support `as` to assign capture name.
1111
- NOT support `if already had/if not already had`
12-
- NO `firstMatch`, since in JavaScript `lazy` means non-greedy (matching the fewest possible characters).
12+
- NO `first match` and NO `all lazy`, since in JavaScript `lazy` means non-greedy (matching the fewest possible characters).
1313

1414
## Installation
1515

@@ -26,21 +26,29 @@ The builder can agent `test/exec` method to the generated regex object. Or you c
2626
```js
2727
const SRL = require('srl')
2828
const query = new SRL('letter exactly 3 times')
29-
const regex = query.get() // /[a-z]{3}/
3029

31-
query.test('aaa') // true
32-
query.exec('aaa') // [ 'aaa', index: 0, input: 'aaa' ]
30+
query.isMatching('aaa') // true
31+
query.getMatch('aaa') // [ 'aaa', index: 0, input: 'aaa' ]
3332

3433
query
3534
.digit()
3635
.neverOrMore()
3736
.mustEnd()
38-
.get() // /[a-z]{3}[0-9]*$/
37+
.get() // /[a-z]{3}[0-9]*$/g
3938
```
4039

4140
Required Node 6.0+ for the ES6 support, Or you can use [Babel](http://babeljs.io/) to support Node below 6.0.
4241

43-
Using [Webpack](http://webpack.github.io) and [babel-loader](https://github.com/babel/babel-loader) to pack it if want to use in browsers.
42+
Using [Webpack](http://webpack.github.io) and [babel-loader](https://github.com/babel/babel-loader) to pack it if want to use in browsers.
43+
44+
## Additional
45+
46+
In SRL-JavaScript we apply `g` flag as default to follow the [Simple Regex](https://simple-regex.com/) "standard", so we provide more API to use regex conveniently.
47+
48+
- `isMatching` - Validate if the expression matches the given string.
49+
- `getMatch` - Get first match of the given string, like run `regex.exec` once.
50+
- `getMatches` - Get all matches of the given string, like a loop to run `regex.exec`.
51+
- `removeModifier` - Remove specific flag.
4452

4553
## Development
4654

Diff for: lib/Builder.js

+63-5
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,19 @@ 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 = this._modifiers.replace(flag, '')
643+
this._result = null
644+
645+
return this
646+
}
647+
636648
/**********************************************************/
637649
/* REGEX METHODS */
638650
/**********************************************************/
@@ -645,6 +657,52 @@ class Builder {
645657
const regexp = this.get()
646658
return regexp.test.apply(regexp, arguments)
647659
}
660+
661+
/**********************************************************/
662+
/* ADDITIONAL METHODS */
663+
/**********************************************************/
664+
665+
/**
666+
* Just like test in RegExp, but reset lastIndex.
667+
*
668+
* @param {string} target
669+
* @return {boolean}
670+
*/
671+
isMatching(target) {
672+
const result = this.test(target)
673+
this.get().lastIndex = 0
674+
return result
675+
}
676+
677+
/**
678+
* Just like match in String, but reset lastIndex.
679+
*
680+
* @param {string} target
681+
* @return {array|null}
682+
*/
683+
getMatch(target) {
684+
const regex = this.get()
685+
const result = regex.exec(target)
686+
regex.lastIndex = 0
687+
return result
688+
}
689+
690+
/**
691+
* Get all matches, just like loop for RegExp.exec.
692+
* @param {string} target
693+
*/
694+
getMatches(target) {
695+
const result = []
696+
const regex = this.get()
697+
let temp = null
698+
699+
while (temp = regex.exec(target)) {
700+
result.push(temp)
701+
}
702+
regex.lastIndex = 0
703+
704+
return result
705+
}
648706
}
649707

650708
module.exports = Builder

Diff for: lib/Language/Helpers/methodMatch.js

-1
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' },

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "srl",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Simple Regex Language",
55
"main": "lib/SRL.js",
66
"scripts": {

Diff for: test/builder-test.js

+40-36
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

33
const assert = require('assert')
4-
const SRL = require('../lib/Builder')
4+
const SRL = require('../')
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('[email protected]'.match(regex)[0], '[email protected]')
45-
assert.equal(regex.exec('[email protected]'), '[email protected]')
46-
assert.ok(!regex.test('sample.example.com'))
47-
assert.ok(!regex.test('missing@tld'))
48-
assert.ok(!regex.test('hav [email protected]'))
49-
assert.ok(!regex.test('[email protected]'))
50-
assert.ok(!regex.test('[email protected]'))
42+
43+
assert.equal(regex.getMatch('[email protected]')[0], '[email protected]')
44+
assert.equal(regex.getMatch('[email protected]')[0], '[email protected]')
45+
assert.ok(!regex.isMatching('sample.example.com'))
46+
assert.ok(!regex.isMatching('missing@tld'))
47+
assert.ok(!regex.isMatching('hav [email protected]'))
48+
assert.ok(!regex.isMatching('[email protected]'))
49+
assert.ok(!regex.isMatching('[email protected]'))
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,18 @@ 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'))
171+
})
172+
173+
it('Remove modifier', () => {
174+
const regex = new SRL()
175+
.literally('foo')
176+
.removeModifier('g')
177+
.get()
178+
179+
assert.deepEqual(regex, /(?:foo)/)
176180
})
177181
})

Diff for: test/cache-test.js

+1-1
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)