[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
This commit is contained in:
Leonardo Dino
2019-05-03 20:32:07 -07:00
committed by Wesley Wigham
parent e6c8ba4b9c
commit 87735e29a5
2 changed files with 25 additions and 2 deletions
+4 -2
View File
@@ -2,7 +2,7 @@
// Project: https://github.com/lapwinglabs/x-ray#readme
// Definitions by: Matt Traynham <https://github.com/mtraynham>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// TypeScript Version: 3.3
/// <reference types="node" />
@@ -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;
+21
View File
@@ -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;
}));