Skip to content

Commit 06f7481

Browse files
committed
feat(RegExp): expose match indexes in Dart
1 parent 009e11a commit 06f7481

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

modules/angular2/src/facade/lang.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,24 @@ class RegExpWrapper {
137137
}
138138

139139
class RegExpMatcherWrapper {
140-
static Match next(Iterator<Match> matcher) {
140+
static _JSLikeMatch next(Iterator<Match> matcher) {
141141
if (matcher.moveNext()) {
142-
return matcher.current;
142+
return new _JSLikeMatch(matcher.current);
143143
}
144144
return null;
145145
}
146146
}
147147

148+
class _JSLikeMatch {
149+
Match _m;
150+
151+
_JSLikeMatch(this._m);
152+
153+
String operator[](index) => _m[index];
154+
int get index => _m.start;
155+
int get length => _m.groupCount + 1;
156+
}
157+
148158
class FunctionWrapper {
149159
static apply(Function fn, posArgs) {
150160
return Function.apply(fn, posArgs);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib';
2+
3+
import {ListWrapper} from 'angular2/src/facade/collection';
4+
import {isPresent, RegExpWrapper, RegExpMatcherWrapper} from 'angular2/src/facade/lang';
5+
6+
export function main() {
7+
describe('RegExp', () => {
8+
it('should expose the index for each match', () => {
9+
var re = RegExpWrapper.create('(!)');
10+
var matcher = RegExpWrapper.matcher(re, '0!23!567!!');
11+
var indexes = [];
12+
var m;
13+
14+
while (isPresent(m = RegExpMatcherWrapper.next(matcher))) {
15+
ListWrapper.push(indexes, m.index);
16+
expect(m[0]).toEqual('!');
17+
expect(m[1]).toEqual('!');
18+
expect(m.length).toBe(2);
19+
}
20+
21+
expect(indexes).toEqual([1, 4, 8, 9]);
22+
})
23+
});
24+
}

0 commit comments

Comments
 (0)