diff --git a/types/d3-fetch/d3-fetch-tests.ts b/types/d3-fetch/d3-fetch-tests.ts index 24c6783997..941955c996 100644 --- a/types/d3-fetch/d3-fetch-tests.ts +++ b/types/d3-fetch/d3-fetch-tests.ts @@ -11,28 +11,41 @@ const init: RequestInit = {}; let p1: Promise = d3Fetch.blob(url); p1 = d3Fetch.blob(url, init); + let p2: Promise = d3Fetch.buffer(url); p2 = d3Fetch.buffer(url, init); + let p3: Promise = d3Fetch.image(url); -p3 = d3Fetch.image(url, init); +const imageProperties = { + width: '300px', + height: '500px' +}; +p3 = d3Fetch.image(url, imageProperties); + let p4: Promise = d3Fetch.json(url); p4 = d3Fetch.json(url, init); +let p5: Promise = d3Fetch.json(url); +p5 = d3Fetch.json(url, init); let myString: Promise; myString = d3Fetch.text(url); myString = d3Fetch.text(url, init); -const parseRow = (d: {}) => { - const myType: MyType = { foo: 'foo' }; +const parseRow = (rawRow: DSVRowString, index: number, columns: string[]): (MyType | undefined | null) => { + const myType: MyType | null = rawRow['foo'] ? { foo: rawRow['foo'] + '+ bar' } : null; return myType; }; +let promise1: Promise>; let promise2: Promise>; -promise2 = d3Fetch.csv(url); -promise2 = d3Fetch.csv(url, init); +promise1 = d3Fetch.csv(url); +promise1 = d3Fetch.csv(url, init); +promise2 = d3Fetch.csv(url, parseRow); promise2 = d3Fetch.csv(url, init, parseRow); -promise2 = d3Fetch.dsv(';', url); -promise2 = d3Fetch.dsv(';', url, init); +promise1 = d3Fetch.dsv(';', url); +promise1 = d3Fetch.dsv(';', url, init); +promise2 = d3Fetch.dsv(';', url, parseRow); promise2 = d3Fetch.dsv(';', url, init, parseRow); -promise2 = d3Fetch.tsv(url); -promise2 = d3Fetch.tsv(url, init); +promise1 = d3Fetch.tsv(url); +promise1 = d3Fetch.tsv(url, init); +promise2 = d3Fetch.tsv(url, parseRow); promise2 = d3Fetch.tsv(url, init, parseRow); diff --git a/types/d3-fetch/index.d.ts b/types/d3-fetch/index.d.ts index 9fd19fe8ed..3eb11ce48a 100644 --- a/types/d3-fetch/index.d.ts +++ b/types/d3-fetch/index.d.ts @@ -3,40 +3,234 @@ // Definitions by: Hugues Stefanski // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 + +// Last module patch version validated against: 1.0.1 + import { DSVParsedArray, DSVRowString, DSVRowAny } from 'd3-dsv'; -/** Fetches the binary file at the specified input URL as a Blob. If init is specified, it is passed along to the underlying call to fetch. */ +/** + * Fetches the binary file at the specified input URL and returns it as a Promise of a Blob. + * If init is specified, it is passed along to the underlying call to fetch. + * + * @param url A valid URL string. + * @param init An optional request initialization object. + */ export function blob(url: string, init?: RequestInit): Promise; -/** Fetches the binary file at the specified input URL as an ArrayBuffer. If init is specified, it is passed along to the underlying call to fetch. */ + +/** + * Fetches the binary file at the specified input URL and returns it as a Promise of an ArrayBuffer. + * If init is specified, it is passed along to the underlying call to fetch. + * + * @param url A valid URL string. + * @param init An optional request initialization object. + */ export function buffer(url: string, init?: RequestInit): Promise; -/** Equivalent to d3.dsv with the comma character as the delimiter. */ -export function csv( +/** + * Fetches the CSV file at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row + * objects are represented as strings. + * + * If init is specified, it is passed along to the underlying call to fetch. + * + * @param url A valid URL string. + * @param init An optional request initialization object. + */ +export function csv( url: string, init?: RequestInit, - row?: (d: DSVRowAny) => ParsedRow +): Promise>; +/** + * Fetches the CSV file at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. + * + * The specified row conversion function is used to map and filter row objects to a more-specific representation; + * see dsv.csvParse for details. + * + * The generic parameter describes the type of the object representation of a parsed row. + * + * @param url A valid URL string. + * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d), + * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, + * the row is skipped and will be ommitted from the array returned by dsv.csvParse; otherwise, the returned value defines the corresponding row object. + * In effect, row is similar to applying a map and filter operator to the returned rows. + */ +export function csv( + url: string, + row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null +): Promise>; +/** + * Fetches the CSV file at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. + * + * The init object is passed along to the underlying call to fetch. + * + * The specified row conversion function is used to map and filter row objects to a more-specific representation; + * see dsv.csvParse for details. + * + * The generic parameter describes the type of the object representation of a parsed row. + * + * @param url A valid URL string. + * @param init An request initialization object. + * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d), + * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, + * the row is skipped and will be ommitted from the array returned by dsv.csvParse; otherwise, the returned value defines the corresponding row object. + * In effect, row is similar to applying a map and filter operator to the returned rows. + */ +export function csv( + url: string, + init: RequestInit, + row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null ): Promise>; -/** Fetches the DSV file at the specified input URL. */ -export function dsv( +/** + * Fetches the DSV file with the specified delimiter character at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row + * objects are represented as strings. + * + * If init is specified, it is passed along to the underlying call to fetch. + * + * @param delimiter The delimiter character used in the DSV file to be fetched. + * @param url A valid URL string. + * @param init An optional request initialization object. + */ +export function dsv( delimiter: string, url: string, init?: RequestInit, - row?: (d: DSVRowAny) => ParsedRow +): Promise>; +/** + * Fetches the DSV file with the specified delimiter character at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. + * + * The specified row conversion function is used to map and filter row objects to a more-specific representation; + * see dsv.parse for details. + * + * The generic parameter describes the type of the object representation of a parsed row. + * + * @param delimiter The delimiter character used in the DSV file to be fetched. + * @param url A valid URL string. + * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d), + * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, + * the row is skipped and will be ommitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object. + * In effect, row is similar to applying a map and filter operator to the returned rows. + */ +export function dsv( + delimiter: string, + url: string, + row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null +): Promise>; +/** + * Fetches the DSV file with the specified delimiter character at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. + * + * The init object is passed along to the underlying call to fetch. + * + * The specified row conversion function is used to map and filter row objects to a more-specific representation; + * see dsv.parse for details. + * + * The generic parameter describes the type of the object representation of a parsed row. + * + * @param delimiter The delimiter character used in the DSV file to be fetched. + * @param url A valid URL string. + * @param init An request initialization object. + * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d), + * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, + * the row is skipped and will be ommitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object. + * In effect, row is similar to applying a map and filter operator to the returned rows. + */ +export function dsv( + delimiter: string, + url: string, + init: RequestInit, + row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null ): Promise>; -/** Fetches the image at the specified input URL. If init is specified, it is passed along to the underlying call to fetch. */ -export function image(url: string, init?: RequestInit): Promise; +/** + * Fetches the image at the specified input URL and returns a promise of an HTML image element. + * + * If init is specified, sets any additional properties on the image before loading. + * + * @param url A valid URL string. + * @param init An optional object of image properties to set. + */ +export function image(url: string, init?: {[key: string]: any}): Promise; -/** Fetches the json file at the specified input URL. If init is specified, it is passed along to the underlying call to fetch. */ -export function json(url: string, init?: RequestInit): Promise<{}>; +/** + * Fetches the json file at the specified input URL and returns it as a Promise of a parsed JSON object. + * + * If init is specified, it is passed along to the underlying call to fetch. + * + * The generic parameter describes the type of the object parsed from the returned JSON. + * + * @param url A valid URL string. + * @param init An optional request initialization object. + */ +export function json(url: string, init?: RequestInit): Promise; -/** Fetches the text file at the specified input URL. If init is specified, it is passed along to the underlying call to fetch. */ +/** + * Fetches the text file at the specified input URL and returns it as a Promise of a string. + * + * If init is specified, it is passed along to the underlying call to fetch. + * + * @param url A valid URL string. + * @param init An optional request initialization object. + */ export function text(url: string, init?: RequestInit): Promise; -/** Equivalent to d3.dsv with the tab character as the delimiter. */ -export function tsv( +/** + * Fetches the TSV file at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. + * + * If init is specified, it is passed along to the underlying call to fetch. + * + * @param url A valid URL string. + * @param init An optional request initialization object. + */ +export function tsv( url: string, init?: RequestInit, - row?: (d: DSVRowAny) => ParsedRow +): Promise>; +/** + * Fetches the TSV file at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. The values of the properties of the parsed row + * objects are represented as strings. + * + * The specified row conversion function is used to map and filter row objects to a more-specific representation; + * see dsv.tsvParse for details. + * + * The generic parameter describes the type of the object representation of a parsed row. + * + * @param url A valid URL string. + * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d), + * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, + * the row is skipped and will be ommitted from the array returned by dsv.tsvParse; otherwise, the returned value defines the corresponding row object. + * In effect, row is similar to applying a map and filter operator to the returned rows. + */ +export function tsv( + url: string, + row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null +): Promise>; +/** + * Fetches the TSV file at the specified input URL and returns + * a promise of an array of objects representing the parsed rows. + * + * The init object is passed along to the underlying call to fetch. + * + * The specified row conversion function is used to map and filter row objects to a more-specific representation; + * see dsv.tsvParse for details. + * + * The generic parameter describes the type of the object representation of a parsed row. + * + * @param url A valid URL string. + * @param init An request initialization object. + * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d), + * the index (i) starting at zero for the first non-header row, and the array of column names. If the returned value is null or undefined, + * the row is skipped and will be ommitted from the array returned by dsv.tsvParse; otherwise, the returned value defines the corresponding row object. + * In effect, row is similar to applying a map and filter operator to the returned rows. + */ +export function tsv( + url: string, + init: RequestInit, + row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null ): Promise>; diff --git a/types/d3-fetch/tslint.json b/types/d3-fetch/tslint.json index 3db14f85ea..54efb0b84e 100644 --- a/types/d3-fetch/tslint.json +++ b/types/d3-fetch/tslint.json @@ -1 +1,7 @@ -{ "extends": "dtslint/dt.json" } +{ + "extends": "dtslint/dt.json", + "rules": { + "unified-signatures": false, + "no-unnecessary-generics": false + } +}