Add isRegExp helper function

This commit is contained in:
Arvin Sevilla 2020-02-18 13:08:48 +08:00
parent 14004429fc
commit 5c37b8cf0b
3 changed files with 7 additions and 3 deletions

View file

@ -98,6 +98,10 @@ class Helper {
return typeof obj === 'number' || obj instanceof Number;
}
static isRegExp(obj: any): obj is RegExp {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}
static async waitForEvent(
emitter: platform.EventEmitterType,
eventName: (string | symbol),

View file

@ -358,7 +358,7 @@ export class Page extends platform.EventEmitter {
async waitForRequest(urlOrPredicate: string | RegExp | ((r: network.Request) => boolean), options: types.TimeoutOptions = {}): Promise<network.Request> {
const { timeout = this._timeoutSettings.timeout() } = options;
return helper.waitForEvent(this, Events.Page.Request, (request: network.Request) => {
if (helper.isString(urlOrPredicate) || urlOrPredicate instanceof RegExp)
if (helper.isString(urlOrPredicate) || helper.isRegExp(urlOrPredicate))
return platform.urlMatches(request.url(), urlOrPredicate);
return urlOrPredicate(request);
}, timeout, this._disconnectedPromise);
@ -367,7 +367,7 @@ export class Page extends platform.EventEmitter {
async waitForResponse(urlOrPredicate: string | RegExp | ((r: network.Response) => boolean), options: types.TimeoutOptions = {}): Promise<network.Response> {
const { timeout = this._timeoutSettings.timeout() } = options;
return helper.waitForEvent(this, Events.Page.Response, (response: network.Response) => {
if (helper.isString(urlOrPredicate) || urlOrPredicate instanceof RegExp)
if (helper.isString(urlOrPredicate) || helper.isRegExp(urlOrPredicate))
return platform.urlMatches(response.url(), urlOrPredicate);
return urlOrPredicate(response);
}, timeout, this._disconnectedPromise);

View file

@ -222,7 +222,7 @@ export function urlMatches(urlString: string, match: types.URLMatch | undefined)
return true;
if (helper.isString(match))
match = helper.globToRegex(match);
if (match instanceof RegExp || match.constructor.name === 'RegExp')
if (helper.isRegExp(match))
return match.test(urlString);
if (typeof match === 'string' && match === urlString)
return true;