chore(ts): Don't mask the Regexp builtin.

Doing so makes it impossible to compile with TypeScript, since it conflicts with the shape of the Regexp global var defined in the standard lib.
This commit is contained in:
Alex Eagle 2015-03-31 17:03:48 -07:00
parent b65b145122
commit a11f683e7b
1 changed files with 14 additions and 29 deletions

View File

@ -67,24 +67,20 @@ export class StringWrapper {
return s.charCodeAt(index); return s.charCodeAt(index);
} }
static split(s:string, regExp:RegExp) { static split(s:string, regExp) {
return s.split(regExp.multiple); return s.split(regExp);
} }
static equals(s:string, s2:string):boolean { static equals(s:string, s2:string):boolean {
return s === s2; return s === s2;
} }
static replace(s:string, from , replace:string): string { static replace(s:string, from: string, replace:string): string {
if (typeof(from) === "string") {
return s.replace(from, replace); return s.replace(from, replace);
} else {
return s.replace(from.single, replace);
}
} }
static replaceAll(s:string, from:RegExp, replace:string):string { static replaceAll(s:string, from:RegExp, replace:string):string {
return s.replace(from.multiple, replace); return s.replace(from, replace);
} }
static startsWith(s:string, start:string) { static startsWith(s:string, start:string) {
@ -96,7 +92,7 @@ export class StringWrapper {
} }
static replaceAllMapped(s:string, from:RegExp, cb:Function): string { static replaceAllMapped(s:string, from:RegExp, cb:Function): string {
return s.replace(from.multiple, function(...matches) { return s.replace(from, function(...matches) {
// Remove offset & string from the result array // Remove offset & string from the result array
matches.splice(-2, 2); matches.splice(-2, 2);
// The callback receives match, p1, ..., pn // The callback receives match, p1, ..., pn
@ -188,36 +184,25 @@ export class NumberWrapper {
} }
} }
export var RegExp; export var RegExp = _global.RegExp;
if (assertionsEnabled_) {
RegExp = assert.define('RegExp', function(obj) {
assert(obj).is(assert.structure({
single: _global.RegExp,
multiple: _global.RegExp
}));
});
} else {
RegExp = {};
}
export class RegExpWrapper { export class RegExpWrapper {
static create(regExpStr, flags:string = ''):RegExp { static create(regExpStr, flags:string = ''):RegExp {
flags = flags.replace(/g/g, ''); flags = flags.replace(/g/g, '');
return { return new _global.RegExp(regExpStr, flags + 'g');
multiple: new _global.RegExp(regExpStr, flags + 'g'),
single: new _global.RegExp(regExpStr, flags)
};
} }
static firstMatch(regExp, input) { static firstMatch(regExp, input) {
return input.match(regExp.single); // Reset multimatch regex state
regExp.lastIndex = 0;
return regExp.exec(input);
} }
static matcher(regExp, input) { static matcher(regExp, input) {
// Reset regex state for the case // Reset regex state for the case
// someone did not loop over all matches // someone did not loop over all matches
// last time. // last time.
regExp.multiple.lastIndex = 0; regExp.lastIndex = 0;
return { return {
re: regExp.multiple, re: regExp,
input: input input: input
}; };
} }