From 5c37b8cf0bf07c44db409ac1a85995f4f233c6be Mon Sep 17 00:00:00 2001 From: Arvin Sevilla Date: Tue, 18 Feb 2020 13:08:48 +0800 Subject: [PATCH] Add isRegExp helper function --- src/helper.ts | 4 ++++ src/page.ts | 4 ++-- src/platform.ts | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/helper.ts b/src/helper.ts index a250ed680b..cef51462ba 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -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), diff --git a/src/page.ts b/src/page.ts index 162586d4fa..8581180d1a 100644 --- a/src/page.ts +++ b/src/page.ts @@ -358,7 +358,7 @@ export class Page extends platform.EventEmitter { async waitForRequest(urlOrPredicate: string | RegExp | ((r: network.Request) => boolean), options: types.TimeoutOptions = {}): Promise { 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 { 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); diff --git a/src/platform.ts b/src/platform.ts index 217587c960..ac313f5c8e 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -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;