diff --git a/types/d3-dsv/d3-dsv-tests.ts b/types/d3-dsv/d3-dsv-tests.ts index f3adb34e00..259180632f 100644 --- a/types/d3-dsv/d3-dsv-tests.ts +++ b/types/d3-dsv/d3-dsv-tests.ts @@ -5,6 +5,7 @@ * in the sense of typing and call signature consistency. They * are not intended as functional tests. */ +/* tslint:disable:no-unnecessary-type-assertion */ import * as d3Dsv from 'd3-dsv'; @@ -16,9 +17,9 @@ const csvTestString = '1997,Ford,E350,2.34\n2000,Mercury,Cougar,2.38'; const tsvTestString = '1997\tFord\tE350\t2.34\n2000\tMercury\tCougar\t2.38'; const pipedTestString = '1997|Ford|E350|2.34\n2000|Mercury|Cougar|2.38'; -const csvTestStringWithHeader = 'Year,Make,Model,Length\n1997,Ford,E350,2.34\n2000,Mercury,Cougar,2.38'; -const tsvTestStringWithHeader = 'Year\tMake\tModel\tLength\n1997\tFord\tE350\t2.34\n2000\tMercury\tCougar\t2.38'; -const pipedTestStringWithHeader = 'Year|Make|Model|Length\n1997|Ford|E350|2.34\n2000|Mercury|Cougar|2.38'; +const csvTestStringWithHeader = `Year,Make,Model,Length\n${csvTestString}`; +const tsvTestStringWithHeader = `Year\tMake\tModel\tLength\n${tsvTestString}`; +const pipedTestStringWithHeader = `Year|Make|Model|Length\n${pipedTestString}`; interface ParsedTestObject { year: Date | null; @@ -33,9 +34,8 @@ let parseMappedArray: d3Dsv.DSVParsedArray; let parseRowsArray: string[][]; let parseRowsMappedArray: ParsedTestObject[]; +let parsedTestObject: ParsedTestObject; let columns: string[]; -let num: number; -let dateNull: Date | null; let str: string; let strMaybe: string | undefined; @@ -48,12 +48,8 @@ let strMaybe: string | undefined; // without row mapper ----------------------------------------------------------------------- parseArray = d3Dsv.csvParse(csvTestStringWithHeader); - columns = parseArray.columns; - -strMaybe = parseArray[0]['Year']; -// $ExpectError -date = parseArray[0]['Year']; // fails, return value is string +strMaybe = parseArray[0].Year; // with row mapper --------------------------------------------------------------------------- @@ -61,28 +57,15 @@ parseMappedArray = d3Dsv.csvParse(csvTestStringWithHeader, (rawRow, index, colum const rr: d3Dsv.DSVRowString = rawRow; const i: number = index; const c: string[] = columns; - const pr: ParsedTestObject = { - year: rr['Year'] ? new Date(+rr['Year']!, 0, 1) : null, - make: rr['Make'] ? rr['Make']! : "Missing Value", - model: rr['Model'] ? rr['Model']! : "Missing Value", - length: rr['Length'] ? +rr['Length']! : NaN - }; - return pr; -}); - -parseMappedArray = d3Dsv.csvParse(csvTestStringWithHeader, (rawRow, index, columns) => { - const rr: d3Dsv.DSVRowString = rawRow; - const i: number = index; - const c: string[] = columns; - const d: number | null = rr['Year'] ? +rr['Year']! : null; + const d: number | null = rr.Year ? +rr.Year! : null; const pr: ParsedTestObject | null | undefined = d !== null ? ( d > 1997 ? { year: new Date(d, 0, 1), - make: rr['Make'] ? rr['Make']! : "Missing Value", - model: rr['Model'] ? rr['Model']! : "Missing Value", - length: rr['Length'] ? +rr['Length']! : NaN + make: rr.Make ? rr.Make! : "Missing Value", + model: rr.Model ? rr.Model! : "Missing Value", + length: rr.Length ? +rr.Length! : NaN } : undefined ) @@ -91,21 +74,14 @@ parseMappedArray = d3Dsv.csvParse(csvTestStringWithHeader, (rawRow, index, colum }); columns = parseMappedArray.columns; - -dateNull = parseMappedArray[0].year; -strMaybe = parseMappedArray[0].make; -strMaybe = parseMappedArray[0].model; -num = parseMappedArray[0].length; +parsedTestObject = parseMappedArray[0]; // csvParseRows(...) ============================================================================ // without row mapper ----------------------------------------------------------------------- parseRowsArray = d3Dsv.csvParseRows(csvTestString); - strMaybe = parseRowsArray[0][0]; // 'Year' of first row -// $ExpectError -date = parseRowsArray[0][0]; // fails, return value is string // with row mapper --------------------------------------------------------------------------- @@ -128,15 +104,12 @@ parseRowsMappedArray = d3Dsv.csvParseRows(csvTestString, (rawRow, index) => { return pr; }); -dateNull = parseRowsMappedArray[0].year; -strMaybe = parseRowsMappedArray[0].make; -strMaybe = parseRowsMappedArray[0].model; -num = parseRowsMappedArray[0].length; - // csvFormat(...) ============================================================================ str = d3Dsv.csvFormat(parseRowsMappedArray); -str = d3Dsv.csvFormat(parseRowsMappedArray, columns); +str = d3Dsv.csvFormat(parseRowsMappedArray, ["year", "length"]); +// $ExpectError +str = d3Dsv.csvFormat(parseRowsMappedArray, ["year", "unknown"]); // csvFormatRows(...) ======================================================================== @@ -156,12 +129,8 @@ str = d3Dsv.csvFormatRows(parseRowsMappedArray.map((d, i) => [ // without row mapper ----------------------------------------------------------------------- parseArray = d3Dsv.tsvParse(tsvTestStringWithHeader); - columns = parseArray.columns; - -strMaybe = parseArray[0]['Year']; -// $ExpectError -date = parseArray[0]['Year']; // fails, return value is string +strMaybe = parseArray[0].Year; // with row mapper --------------------------------------------------------------------------- @@ -169,15 +138,15 @@ parseMappedArray = d3Dsv.tsvParse(tsvTestStringWithHeader, (rawRow, index, colum const rr: d3Dsv.DSVRowString = rawRow; const i: number = index; const c: string[] = columns; - const d: number | null = rr['Year'] ? +rr['Year']! : null; + const d: number | null = rr.Year ? +rr.Year! : null; const pr: ParsedTestObject | null | undefined = d !== null ? ( d > 1997 ? { year: new Date(d, 0, 1), - make: rr['Make'] ? rr['Make']! : "Missing Value", - model: rr['Model'] ? rr['Model']! : "Missing Value", - length: rr['Length'] ? +rr['Length']! : NaN + make: rr.Make ? rr.Make! : "Missing Value", + model: rr.Model ? rr.Model! : "Missing Value", + length: rr.Length ? +rr.Length! : NaN } : undefined ) @@ -186,21 +155,14 @@ parseMappedArray = d3Dsv.tsvParse(tsvTestStringWithHeader, (rawRow, index, colum }); columns = parseMappedArray.columns; - -dateNull = parseMappedArray[0].year; -strMaybe = parseMappedArray[0].make; -strMaybe = parseMappedArray[0].model; -num = parseMappedArray[0].length; +parsedTestObject = parseMappedArray[0]; // tsvParseRows(...) ============================================================================ // without row mapper ----------------------------------------------------------------------- parseRowsArray = d3Dsv.tsvParseRows(tsvTestString); - strMaybe = parseRowsArray[0][0]; // 'Year' of first row -// $ExpectError -date = parseRowsArray[0][0]; // fails, return value is string // with row mapper --------------------------------------------------------------------------- @@ -223,15 +185,12 @@ parseRowsMappedArray = d3Dsv.tsvParseRows(tsvTestString, (rawRow, index) => { return pr; }); -dateNull = parseRowsMappedArray[0].year; -strMaybe = parseRowsMappedArray[0].make; -strMaybe = parseRowsMappedArray[0].model; -num = parseRowsMappedArray[0].length; - // tsvFormat(...) ============================================================================ str = d3Dsv.tsvFormat(parseRowsMappedArray); -str = d3Dsv.tsvFormat(parseRowsMappedArray, columns); +str = d3Dsv.tsvFormat(parseRowsMappedArray, ["year", "length"]); +// $ExpectError +str = d3Dsv.tsvFormat(parseRowsMappedArray, ["year", "unknown"]); // tsvFormatRows(...) ======================================================================== @@ -256,12 +215,8 @@ dsv = d3Dsv.dsvFormat('|'); // without row mapper ----------------------------------------------------------------------- parseArray = dsv.parse(pipedTestStringWithHeader); - columns = parseArray.columns; - -strMaybe = parseArray[0]['Year']; -// $ExpectError -date = parseArray[0]['Year']; // fails, return value is string +strMaybe = parseArray[0].Year; // with row mapper --------------------------------------------------------------------------- @@ -269,15 +224,15 @@ parseMappedArray = dsv.parse(pipedTestStringWithHeader, (rawRow, index, columns) const rr: d3Dsv.DSVRowString = rawRow; const i: number = index; const c: string[] = columns; - const d: number | null = rr['Year'] ? +rr['Year']! : null; + const d: number | null = rr.Year ? +rr.Year! : null; const pr: ParsedTestObject | null | undefined = d !== null ? ( d > 1997 ? { year: new Date(d, 0, 1), - make: rr['Make'] ? rr['Make']! : "Missing Value", - model: rr['Model'] ? rr['Model']! : "Missing Value", - length: rr['Length'] ? +rr['Length']! : NaN + make: rr.Make ? rr.Make! : "Missing Value", + model: rr.Model ? rr.Model! : "Missing Value", + length: rr.Length ? +rr.Length! : NaN } : undefined ) @@ -286,21 +241,14 @@ parseMappedArray = dsv.parse(pipedTestStringWithHeader, (rawRow, index, columns) }); columns = parseMappedArray.columns; - -dateNull = parseMappedArray[0].year; -strMaybe = parseMappedArray[0].make; -strMaybe = parseMappedArray[0].model; -num = parseMappedArray[0].length; +parsedTestObject = parseMappedArray[0]; // parseRows(...) ============================================================================ // without row mapper ----------------------------------------------------------------------- parseRowsArray = dsv.parseRows(pipedTestString); - strMaybe = parseRowsArray[0][0]; // 'Year' of first row -// $ExpectError -date = parseRowsArray[0][0]; // fails, return value is string // with row mapper --------------------------------------------------------------------------- @@ -323,15 +271,12 @@ parseRowsMappedArray = dsv.parseRows(pipedTestString, (rawRow, index) => { return pr; }); -dateNull = parseRowsMappedArray[0].year; -strMaybe = parseRowsMappedArray[0].make; -strMaybe = parseRowsMappedArray[0].model; -num = parseRowsMappedArray[0].length; - // format(...) ============================================================================ str = dsv.format(parseRowsMappedArray); -str = dsv.format(parseRowsMappedArray, columns); +str = dsv.format(parseRowsMappedArray, ["year", "length"]); +// $ExpectError +str = dsv.format(parseRowsMappedArray, ["year", "unknown"]); // formatRows(...) ======================================================================== diff --git a/types/d3-dsv/index.d.ts b/types/d3-dsv/index.d.ts index 228384ed42..bdebc5ca01 100644 --- a/types/d3-dsv/index.d.ts +++ b/types/d3-dsv/index.d.ts @@ -1,9 +1,13 @@ // Type definitions for D3JS d3-dsv module 1.0 // Project: https://github.com/d3/d3-dsv/ -// Definitions by: Tom Wanzek , Alex Ford , Boris Yankov +// Definitions by: Tom Wanzek +// Alex Ford +// Boris Yankov +// denisname // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 -// Last module patch version validated against: 1.0.8 +// Last module patch version validated against: 1.0.10 // ------------------------------------------------------------------------------------------ // Shared Types and Interfaces @@ -19,6 +23,8 @@ export interface DSVRowString { /** * An object representing a DSV parsed row with values represented as an arbitrary datatype, depending * on the performed parsed row mapping. + * + * @deprecated */ export interface DSVRowAny { [key: string]: any; @@ -70,7 +76,7 @@ export function csvParse(csvString: string): DSVParsedArray; * the row is skipped and will be omitted 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 csvParse( +export function csvParse( csvString: string, row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null ): DSVParsedArray; @@ -105,7 +111,7 @@ export function csvParseRows(csvString: string): string[][]; * the row is skipped and will be omitted 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 csvParseRows( +export function csvParseRows( csvString: string, row: (rawRow: string[], index: number) => ParsedRow | undefined | null ): ParsedRow[]; @@ -126,7 +132,7 @@ export function csvParseRows( * @param rows Array of object rows. * @param columns An array of strings representing the column names. */ -export function csvFormat(rows: DSVRowAny[], columns?: string[]): string; +export function csvFormat(rows: T[], columns?: Array): string; // csvFormatRows(...) ======================================================================== @@ -180,7 +186,7 @@ export function tsvParse(tsvString: string): DSVParsedArray; * the row is skipped and will be omitted 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 tsvParse( +export function tsvParse( tsvString: string, row: (rawRow: DSVRowString, index: number, columns: string[]) => MappedRow | undefined | null ): DSVParsedArray; @@ -215,7 +221,7 @@ export function tsvParseRows(tsvString: string): string[][]; * the row is skipped and will be omitted 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 tsvParseRows( +export function tsvParseRows( tsvString: string, row: (rawRow: string[], index: number) => MappedRow | undefined | null ): MappedRow[]; @@ -236,7 +242,7 @@ export function tsvParseRows( * @param rows Array of object rows. * @param columns An array of strings representing the column names. */ -export function tsvFormat(rows: DSVRowAny[], columns?: string[]): string; +export function tsvFormat(rows: T[], columns?: Array): string; // tsvFormatRows(...) ======================================================================== @@ -288,7 +294,7 @@ export interface DSV { * the row is skipped and will be omitted 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. */ - parse( + parse( dsvString: string, row: (rawRow: DSVRowString, index: number, columns: string[]) => ParsedRow | undefined | null ): DSVParsedArray; @@ -317,7 +323,7 @@ export interface DSV { * the row is skipped and will be omitted 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. */ - parseRows( + parseRows( dsvString: string, row: (rawRow: string[], index: number) => ParsedRow | undefined | null ): ParsedRow[]; @@ -334,7 +340,7 @@ export interface DSV { * @param rows Array of object rows. * @param columns An array of strings representing the column names. */ - format(rows: DSVRowAny[], columns?: string[]): string; + format(rows: T[], columns?: Array): string; /** * Formats the specified array of array of string rows as delimiter-separated values, returning a string. diff --git a/types/d3-fetch/index.d.ts b/types/d3-fetch/index.d.ts index 25343d1bf5..3ea81690a0 100644 --- a/types/d3-fetch/index.d.ts +++ b/types/d3-fetch/index.d.ts @@ -2,7 +2,7 @@ // Project: https://d3js.org/d3-fetch/ // Definitions by: Hugues Stefanski // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.2 +// TypeScript Version: 2.3 // Last module patch version validated against: 1.1.0