From 06f7481141712d51bb12a069cd44652ae263cb18 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 26 Feb 2015 16:31:56 +0100 Subject: [PATCH] feat(RegExp): expose match indexes in Dart --- modules/angular2/src/facade/lang.dart | 14 +++++++++++-- modules/angular2/test/facade/lang_spec.js | 24 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 modules/angular2/test/facade/lang_spec.js diff --git a/modules/angular2/src/facade/lang.dart b/modules/angular2/src/facade/lang.dart index e6a121f01c..e18b481057 100644 --- a/modules/angular2/src/facade/lang.dart +++ b/modules/angular2/src/facade/lang.dart @@ -137,14 +137,24 @@ class RegExpWrapper { } class RegExpMatcherWrapper { - static Match next(Iterator matcher) { + static _JSLikeMatch next(Iterator matcher) { if (matcher.moveNext()) { - return matcher.current; + return new _JSLikeMatch(matcher.current); } return null; } } +class _JSLikeMatch { + Match _m; + + _JSLikeMatch(this._m); + + String operator[](index) => _m[index]; + int get index => _m.start; + int get length => _m.groupCount + 1; +} + class FunctionWrapper { static apply(Function fn, posArgs) { return Function.apply(fn, posArgs); diff --git a/modules/angular2/test/facade/lang_spec.js b/modules/angular2/test/facade/lang_spec.js new file mode 100644 index 0000000000..3e6929d05c --- /dev/null +++ b/modules/angular2/test/facade/lang_spec.js @@ -0,0 +1,24 @@ +import {describe, it, expect, beforeEach, ddescribe, iit, xit, el} from 'angular2/test_lib'; + +import {ListWrapper} from 'angular2/src/facade/collection'; +import {isPresent, RegExpWrapper, RegExpMatcherWrapper} from 'angular2/src/facade/lang'; + +export function main() { + describe('RegExp', () => { + it('should expose the index for each match', () => { + var re = RegExpWrapper.create('(!)'); + var matcher = RegExpWrapper.matcher(re, '0!23!567!!'); + var indexes = []; + var m; + + while (isPresent(m = RegExpMatcherWrapper.next(matcher))) { + ListWrapper.push(indexes, m.index); + expect(m[0]).toEqual('!'); + expect(m[1]).toEqual('!'); + expect(m.length).toBe(2); + } + + expect(indexes).toEqual([1, 4, 8, 9]); + }) + }); +}