From 87735e29a558c97ef0895a173269e712a199e2b7 Mon Sep 17 00:00:00 2001 From: Leonardo Dino Date: Sat, 4 May 2019 00:32:07 -0300 Subject: [PATCH] [x-ray] add instance.abort parameter (#35187) * [x-ray] add instance.abort parameter It's actually a required parameter created in this PR: https://github.com/matthewmueller/x-ray/pull/232 called here: https://github.com/matthewmueller/x-ray/blob/c95ef3e2a9a260155983b5ddb0eac65267f11d49/index.js#L127 see tests: https://github.com/matthewmueller/x-ray/blob/3c5b9e4a007937c26222c10646087b046b4aab1c/test/xray_spec.js#L338 https://github.com/matthewmueller/x-ray/blob/3c5b9e4a007937c26222c10646087b046b4aab1c/test/xray_spec.js#L304 * [x-ray] remove unnecessary generics Thanks @weswigham for the review * [x-ray] replace abort handler any with unknown thanks @weswigham --- types/x-ray/index.d.ts | 6 ++++-- types/x-ray/x-ray-tests.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/types/x-ray/index.d.ts b/types/x-ray/index.d.ts index 345059a8eb..9554cc8753 100644 --- a/types/x-ray/index.d.ts +++ b/types/x-ray/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/lapwinglabs/x-ray#readme // Definitions by: Matt Traynham // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.3 /// @@ -31,6 +31,8 @@ declare namespace XRay { } type Selector = ScalarSelector | SelectorArray; + type AbortHandler = (data: unknown[], url: string) => boolean; + interface Instance extends XRayCrawler.Instance { ( source: string, @@ -53,7 +55,7 @@ declare namespace XRay { interface InstanceInvocation { (callback: Callback): void; (source: string, callback: Callback): void; - abort(): this; + abort(arg: AbortHandler): this; paginate(selector: Selector): this; limit(n: number): this; stream(): NodeJS.ReadStream; diff --git a/types/x-ray/x-ray-tests.ts b/types/x-ray/x-ray-tests.ts index 8bc056b2f6..7339e32fc2 100644 --- a/types/x-ray/x-ray-tests.ts +++ b/types/x-ray/x-ray-tests.ts @@ -110,3 +110,24 @@ x({ first: x('a@href', 'title') }) })(fn); + +const pagedUrl = 'https://github.com/matthewmueller/x-ray/issues?q=is%3Aissue%20sort%3Acreated-asc%20'; + +x(pagedUrl, '.js-issue-row', [{ id: '@id', title: 'a.h4' }]) + .paginate('.next_page@href') + .abort((_, url) => url.includes('page=3')); + +const hasStringId = (obj: any): obj is {id: string} => { + if (!!obj) return false; + if (typeof obj !== 'object') return false; + if (!('id' in obj)) return false; + if (typeof obj.id !== 'string') return false; + return true; +}; + +x(pagedUrl, '.js-issue-row', [{ id: '@id', title: 'a.h4' }]) + .paginate('.next_page@href') + .abort((results) => results.some(result => { + if (hasStringId(result) && result.id === 'issue_40') return true; + return false; + }));