refactor(RegExpWrapper): remove the facade (#10512)
This commit is contained in:

committed by
Alex Rickabaugh

parent
b4613ab2d2
commit
83e2d3d1cb
@ -198,7 +198,7 @@ function dateFormatter(format: string, date: Date, locale: string): string {
|
||||
if (datePartsFormatterCache.has(format)) {
|
||||
parts = datePartsFormatterCache.get(format);
|
||||
} else {
|
||||
var matchs = DATE_FORMATS_SPLIT.exec(format);
|
||||
const matches = DATE_FORMATS_SPLIT.exec(format);
|
||||
|
||||
while (format) {
|
||||
match = DATE_FORMATS_SPLIT.exec(format);
|
||||
|
@ -312,50 +312,6 @@ export class NumberWrapper {
|
||||
|
||||
export var RegExp = _global.RegExp;
|
||||
|
||||
export class RegExpWrapper {
|
||||
static create(regExpStr: string, flags: string = ''): RegExp {
|
||||
flags = flags.replace(/g/g, '');
|
||||
return new _global.RegExp(regExpStr, flags + 'g');
|
||||
}
|
||||
static firstMatch(regExp: RegExp, input: string): RegExpExecArray {
|
||||
// Reset multimatch regex state
|
||||
regExp.lastIndex = 0;
|
||||
return regExp.exec(input);
|
||||
}
|
||||
static test(regExp: RegExp, input: string): boolean {
|
||||
regExp.lastIndex = 0;
|
||||
return regExp.test(input);
|
||||
}
|
||||
static matcher(regExp: RegExp, input: string): {re: RegExp; input: string} {
|
||||
// Reset regex state for the case
|
||||
// someone did not loop over all matches
|
||||
// last time.
|
||||
regExp.lastIndex = 0;
|
||||
return {re: regExp, input: input};
|
||||
}
|
||||
static replaceAll(regExp: RegExp, input: string, replace: Function): string {
|
||||
let c = regExp.exec(input);
|
||||
let res = '';
|
||||
regExp.lastIndex = 0;
|
||||
let prev = 0;
|
||||
while (c) {
|
||||
res += input.substring(prev, c.index);
|
||||
res += replace(c);
|
||||
prev = c.index + c[0].length;
|
||||
regExp.lastIndex = prev;
|
||||
c = regExp.exec(input);
|
||||
}
|
||||
res += input.substring(prev);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export class RegExpMatcherWrapper {
|
||||
static next(matcher: {re: RegExp; input: string}): RegExpExecArray {
|
||||
return matcher.re.exec(matcher.input);
|
||||
}
|
||||
}
|
||||
|
||||
export class FunctionWrapper {
|
||||
static apply(fn: Function, posArgs: any): any { return fn.apply(null, posArgs); }
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {NumberWrapper, RegExpMatcherWrapper, RegExpWrapper, StringWrapper, escapeRegExp, hasConstructor, isPresent, isPromise, resolveEnumToken} from '../src/lang';
|
||||
import {NumberWrapper, StringWrapper, escapeRegExp, hasConstructor, isPresent, isPromise, resolveEnumToken} from '../src/lang';
|
||||
|
||||
enum UsefulEnum {
|
||||
MyToken,
|
||||
@ -18,42 +18,11 @@ class MySubclass extends MySuperclass {}
|
||||
|
||||
export function main() {
|
||||
describe('RegExp', () => {
|
||||
it('should expose the index for each match', () => {
|
||||
var re = /(!)/g;
|
||||
var matcher = RegExpWrapper.matcher(re, '0!23!567!!');
|
||||
var indexes: number[] = [];
|
||||
var m: any /** TODO #9100 */;
|
||||
|
||||
while (isPresent(m = RegExpMatcherWrapper.next(matcher))) {
|
||||
indexes.push(m.index);
|
||||
expect(m[0]).toEqual('!');
|
||||
expect(m[1]).toEqual('!');
|
||||
expect(m.length).toBe(2);
|
||||
}
|
||||
|
||||
expect(indexes).toEqual([1, 4, 8, 9]);
|
||||
});
|
||||
|
||||
it('should reset before it is reused', () => {
|
||||
var re = /^['"]/g;
|
||||
var str = '\'';
|
||||
expect(RegExpWrapper.test(re, str)).toEqual(true);
|
||||
// If not reset, the second attempt to test results in false
|
||||
expect(RegExpWrapper.test(re, str)).toEqual(true);
|
||||
});
|
||||
|
||||
it('should implement replace all', () => {
|
||||
let re = /(\d)+/g;
|
||||
let m =
|
||||
RegExpWrapper.replaceAll(re, 'a1b2c', (match: any /** TODO #9100 */) => `!${match[1]}!`);
|
||||
expect(m).toEqual('a!1!b!2!c');
|
||||
});
|
||||
|
||||
it('should escape regexp', () => {
|
||||
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('b')), 'abc')).toBeTruthy();
|
||||
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('b')), 'adc')).toBeFalsy();
|
||||
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('a.b')), 'a.b')).toBeTruthy();
|
||||
expect(RegExpWrapper.firstMatch(new RegExp(escapeRegExp('axb')), 'a.b')).toBeFalsy();
|
||||
expect(new RegExp(escapeRegExp('b')).exec('abc')).toBeTruthy();
|
||||
expect(new RegExp(escapeRegExp('b')).exec('adc')).toBeFalsy();
|
||||
expect(new RegExp(escapeRegExp('a.b')).exec('a.b')).toBeTruthy();
|
||||
expect(new RegExp(escapeRegExp('a.b')).exec('axb')).toBeFalsy();
|
||||
});
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user