-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds group extraction and named groups
- Loading branch information
ljdavies
committed
Apr 26, 2020
1 parent
8078f77
commit 88bbbc0
Showing
6 changed files
with
151 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import ExpressionBuilder from '../../src/exceptional-expressions'; | ||
import { group } from '../../src/utils'; | ||
import Sequences from '../../src/sequences'; | ||
|
||
describe('getMatches test', () => { | ||
let builder: ExpressionBuilder = new ExpressionBuilder('g'); | ||
let match: string | any[]; | ||
|
||
beforeEach(() => { | ||
builder.reset(); | ||
}); | ||
|
||
it('extracts single group match', () => { | ||
builder | ||
.contains('hello') | ||
.followedBy(Sequences.whitespace({ min: 1 })) | ||
.followedBy(group(Sequences.numbers(4), 'numbers')); | ||
|
||
match = builder.getMatchesWithGroups('hello 1234'); | ||
expect(match.length).toEqual(1); | ||
expect(match[0].match).toEqual('hello 1234'); | ||
expect(match[0].groups['numbers']).toEqual('1234'); | ||
}); | ||
|
||
it('extracts multi group match', () => { | ||
builder | ||
.contains('hello') | ||
.followedBy(Sequences.whitespace({ min: 1 })) | ||
.followedBy(group(Sequences.numbers(4), 'numbers')); | ||
|
||
match = builder.getMatchesWithGroups('hello 1234 hello 4567'); | ||
|
||
expect(match.length).toEqual(2); | ||
expect(match[0].match).toEqual('hello 1234'); | ||
expect(match[1].match).toEqual('hello 4567'); | ||
expect(match[0].groups['numbers']).toEqual('1234'); | ||
expect(match[1].groups['numbers']).toEqual('4567'); | ||
}); | ||
|
||
it('extracts nested groups match', () => { | ||
builder | ||
.contains('hello') | ||
.followedBy(Sequences.whitespace({ min: 1 })) | ||
.followedBy(group(['(', group(Sequences.numbers(4), 'numbers'), ')'], 'container')); | ||
|
||
match = builder.getMatchesWithGroups('hello (1234) hello (4567)'); | ||
|
||
expect(match.length).toEqual(2); | ||
expect(match[0].match).toEqual('hello (1234)'); | ||
expect(match[1].match).toEqual('hello (4567)'); | ||
expect(match[0].groups['numbers']).toEqual('1234'); | ||
expect(match[1].groups['numbers']).toEqual('4567'); | ||
expect(match[0].groups['container']).toEqual('(1234)'); | ||
expect(match[1].groups['container']).toEqual('(4567)'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import ExpBuilder from './exceptional-expressions'; | ||
import { or } from './utils'; | ||
import { or, anythingBut, group } from './utils'; | ||
|
||
export { ExpBuilder, or }; | ||
export { ExpBuilder, or, anythingBut, group }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters