UNPKG

26.1 kBTypeScriptView Raw
1// Type definitions for D3JS d3-dsv module 3.0
2// Project: https://github.com/d3/d3-dsv/, https://d3js.org/d3-dsv
3// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
4// Alex Ford <https://github.com/gustavderdrache>
5// Boris Yankov <https://github.com/borisyankov>
6// denisname <https://github.com/denisname>
7// Nathan Bierema <https://github.com/Methuselah96>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9
10// Last module patch version validated against: 3.0.1
11
12// ------------------------------------------------------------------------------------------
13// Shared Types and Interfaces
14// ------------------------------------------------------------------------------------------
15
16/**
17 * An object representing a DSV parsed row with values represented as strings.
18 * When the DSV content is not well-structured and some column-values are missing, `undefined` is used as value.
19 */
20export type DSVRowString<Columns extends string = string> = {
21 [key in Columns]: string | undefined;
22};
23
24/**
25 * An object in raw format before parsing, that is with only string values.
26 * When the DSV content is not well-structured and some column-values are missing, `undefined` is used as value.
27 */
28export type DSVRaw<T extends object> = {
29 [key in keyof T]: string | undefined;
30};
31
32/**
33 * An object representing a DSV parsed row with values represented as an arbitrary datatype, depending
34 * on the performed parsed row mapping.
35 *
36 * @deprecated Use `object` instead.
37 */
38export interface DSVRowAny {
39 [key: string]: any;
40}
41
42/**
43 * An array object representing all deserialized rows. The array is enhanced with a property listing
44 * the names of the parsed columns.
45 */
46export interface DSVRowArray<Columns extends string = string> extends Array<DSVRowString<Columns>> {
47 /**
48 * List of column names.
49 */
50 columns: Columns[];
51}
52
53/**
54 * An array object representing all parsed rows. The array is enhanced with a property listing
55 * the names of the parsed columns.
56 */
57export interface DSVParsedArray<T> extends Array<T> {
58 /**
59 * List of column names.
60 */
61 columns: Array<keyof T>;
62}
63
64// ------------------------------------------------------------------------------------------
65// CSV Parsers and Formatters
66// ------------------------------------------------------------------------------------------
67
68// csvParse(...) ============================================================================
69
70/**
71 * Parses the specified string, which must be in the comma-separated values format, returning an array of objects representing the parsed rows.
72 *
73 * Unlike csvParseRows, this method requires that the first line of the CSV content contains a comma-separated list of column names;
74 * these column names become the attributes on the returned objects.
75 *
76 * The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).
77 *
78 * Equivalent to `dsvFormat(",").parse`.
79 * Note: requires unsafe-eval content security policy.
80 *
81 * @param csvString A string, which must be in the comma-separated values format.
82 */
83// eslint-disable-next-line no-unnecessary-generics
84export function csvParse<Columns extends string>(csvString: string): DSVRowArray<Columns>;
85/**
86 * Parses the specified string, which must be in the comma-separated values format, returning an array of objects representing the parsed rows.
87 *
88 * Unlike csvParseRows, this method requires that the first line of the CSV content contains a comma-separated list of column names;
89 * these column names become the attributes on the returned objects.
90 *
91 * The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).
92 *
93 * Equivalent to `dsvFormat(",").parse`.
94 * Note: requires unsafe-eval content security policy.
95 *
96 * @param csvString A string, which must be in the comma-separated values format.
97 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
98 * 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,
99 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
100 * In effect, row is similar to applying a map and filter operator to the returned rows.
101 */
102export function csvParse<ParsedRow extends object, Columns extends string>(
103 csvString: string,
104 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
105): DSVParsedArray<ParsedRow>;
106
107// csvParseRows(...) ========================================================================
108
109/**
110 * Parses the specified string, which must be in the comma-separated values format, returning an array of arrays representing the parsed rows.
111 *
112 * Unlike csvParse, this method treats the header line as a standard row, and should be used whenever CSV content does not contain a header.
113 * Each row is represented as an array rather than an object. Rows may have variable length.
114 *
115 * If a row conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types.
116 * In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the + operator), but better is to specify a row conversion function.
117 *
118 * Equivalent to `dsvFormat(",").parseRows`.
119 *
120 * @param csvString A string, which must be in the comma-separated values format.
121 */
122export function csvParseRows(csvString: string): string[][];
123/**
124 * Parses the specified string, which must be in the comma-separated values format, returning an array of arrays representing the parsed rows.
125 *
126 * Unlike csvParse, this method treats the header line as a standard row, and should be used whenever CSV content does not contain a header.
127 * Each row is represented as an array rather than an object. Rows may have variable length.
128 *
129 * Equivalent to `dsvFormat(",").parseRows`.
130 *
131 * @param csvString A string, which must be in the comma-separated values format.
132 * @param row A row conversion function which is invoked for each row, being passed an array representing the current row (d), the index (i)
133 * starting at zero for the first row, and the array of column names. If the returned value is null or undefined,
134 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
135 * In effect, row is similar to applying a map and filter operator to the returned rows.
136 */
137export function csvParseRows<ParsedRow extends object>(
138 csvString: string,
139 row: (rawRow: string[], index: number) => ParsedRow | undefined | null
140): ParsedRow[];
141
142// csvFormat(...) ============================================================================
143
144/**
145 * Formats the specified array of object rows as comma-separated values, returning a string.
146 * This operation is the inverse of csvParse. Each row will be separated by a newline (\n),
147 * and each column within each row will be separated by the comma-delimiter.
148 * Values that contain either the comma-delimiter, a double-quote (") or a newline will be escaped using double-quotes.
149 *
150 * If columns is not specified, the list of column names that forms the header row is determined by the union of all properties on all objects in rows;
151 * the order of columns is nondeterministic.
152 *
153 * Equivalent to `dsvFormat(",").format`.
154 *
155 * @param rows Array of object rows.
156 * @param columns An array of strings representing the column names.
157 */
158export function csvFormat<T extends object>(rows: readonly T[], columns?: ReadonlyArray<keyof T>): string;
159
160// csvFormatBody(...) ============================================================================
161
162/**
163 * Equivalent to dsvFormat(",").formatBody.
164 *
165 * @param rows Array of object rows.
166 * @param columns An array of strings representing the column names.
167 */
168export function csvFormatBody<T extends object>(rows: readonly T[], columns?: ReadonlyArray<keyof T>): string;
169
170// csvFormatRows(...) ========================================================================
171
172/**
173 * Formats the specified array of array of string rows as comma-separated values, returning a string.
174 * This operation is the reverse of csvParseRows. Each row will be separated by a newline (\n),
175 * and each column within each row will be separated by the comma-delimiter.
176 * Values that contain either the comma-delimiter, a double-quote (") or a newline will be escaped using double-quotes.
177 *
178 * To convert an array of objects to an array of arrays while explicitly specifying the columns, use array.map.
179 * If you like, you can also array.concat this result with an array of column names to generate the first row.
180 *
181 * Equivalent to `dsvFormat(",").formatRows`.
182 *
183 * @param rows An array of array of string rows.
184 */
185export function csvFormatRows(rows: readonly string[][]): string;
186
187// csvFormatRow(...) ========================================================================
188
189/**
190 * Equivalent to dsvFormat(",").formatRow.
191 *
192 * @param row An array of strings representing a row.
193 */
194export function csvFormatRow(row: readonly string[]): string;
195
196// csvFormatValue(...) ========================================================================
197
198/**
199 * Equivalent to dsvFormat(",").formatValue.
200 *
201 * @param value A value.
202 */
203export function csvFormatValue(value: string): string;
204
205// ------------------------------------------------------------------------------------------
206// TSV Parsers and Formatters
207// ------------------------------------------------------------------------------------------
208
209// tsvParse(...) ============================================================================
210
211/**
212 * Parses the specified string, which must be in the tab-separated values format, returning an array of objects representing the parsed rows.
213 *
214 * Unlike tsvParseRows, this method requires that the first line of the TSV content contains a tab-separated list of column names;
215 * these column names become the attributes on the returned objects.
216 *
217 * The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).
218 *
219 * Equivalent to `dsvFormat("\t").parse`.
220 * Note: requires unsafe-eval content security policy.
221 *
222 * @param tsvString A string, which must be in the tab-separated values format.
223 */
224// eslint-disable-next-line no-unnecessary-generics
225export function tsvParse<Columns extends string>(tsvString: string): DSVRowArray<Columns>;
226/**
227 * Parses the specified string, which must be in the tab-separated values format, returning an array of objects representing the parsed rows.
228 *
229 * Unlike tsvParseRows, this method requires that the first line of the TSV content contains a tab-separated list of column names;
230 * these column names become the attributes on the returned objects.
231 *
232 * The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).
233 *
234 * Equivalent to `dsvFormat("\t").parse`.
235 * Note: requires unsafe-eval content security policy.
236 *
237 * @param tsvString A string, which must be in the tab-separated values format.
238 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
239 * 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,
240 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
241 * In effect, row is similar to applying a map and filter operator to the returned rows.
242 */
243export function tsvParse<ParsedRow extends object, Columns extends string>(
244 tsvString: string,
245 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
246): DSVParsedArray<ParsedRow>;
247
248// tsvParseRows(...) ========================================================================
249
250/**
251 * Parses the specified string, which must be in the tab-separated values format, returning an array of arrays representing the parsed rows.
252 *
253 * Unlike tsvParse, this method treats the header line as a standard row, and should be used whenever TSV content does not contain a header.
254 * Each row is represented as an array rather than an object. Rows may have variable length.
255 *
256 * If a row conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types.
257 * In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the + operator), but better is to specify a row conversion function.
258 *
259 * Equivalent to `dsvFormat("\t").parseRows`.
260 *
261 * @param tsvString A string, which must be in the tab-separated values format.
262 */
263export function tsvParseRows(tsvString: string): string[][];
264/**
265 * Parses the specified string, which must be in the tab-separated values format, returning an array of arrays representing the parsed rows.
266 *
267 * Unlike tsvParse, this method treats the header line as a standard row, and should be used whenever TSV content does not contain a header.
268 * Each row is represented as an array rather than an object. Rows may have variable length.
269 *
270 * Equivalent to `dsvFormat("\t").parseRows`.
271 *
272 * @param tsvString A string, which must be in the tab-separated values format.
273 * @param row A row conversion function which is invoked for each row, being passed an array representing the current row (d), the index (i)
274 * starting at zero for the first row, and the array of column names. If the returned value is null or undefined,
275 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
276 * In effect, row is similar to applying a map and filter operator to the returned rows.
277 */
278export function tsvParseRows<ParsedRow extends object>(
279 tsvString: string,
280 row: (rawRow: string[], index: number) => ParsedRow | undefined | null
281): ParsedRow[];
282
283// tsvFormat(...) ============================================================================
284
285/**
286 * Formats the specified array of object rows as tab-separated values, returning a string.
287 * This operation is the inverse of tsvParse. Each row will be separated by a newline (\n),
288 * and each column within each row will be separated by the tab-delimiter.
289 * Values that contain either the tab-delimiter, a double-quote (") or a newline will be escaped using double-quotes.
290 *
291 * If columns is not specified, the list of column names that forms the header row is determined by the union of all properties on all objects in rows;
292 * the order of columns is nondeterministic.
293 *
294 * Equivalent to `dsvFormat("\t").format`.
295 *
296 * @param rows Array of object rows.
297 * @param columns An array of strings representing the column names.
298 */
299export function tsvFormat<T extends object>(rows: readonly T[], columns?: ReadonlyArray<keyof T>): string;
300
301// tsvFormatBody(...) ============================================================================
302
303/**
304 * Equivalent to dsvFormat("\t").formatBody.
305 *
306 * @param rows Array of object rows.
307 * @param columns An array of strings representing the column names.
308 */
309export function tsvFormatBody<T extends object>(rows: readonly T[], columns?: ReadonlyArray<keyof T>): string;
310
311// tsvFormatRows(...) ========================================================================
312
313/**
314 * Formats the specified array of array of string rows as tab-separated values, returning a string.
315 * This operation is the reverse of tsvParseRows. Each row will be separated by a newline (\n),
316 * and each column within each row will be separated by the tab-delimiter.
317 * Values that contain either the tab-delimiter, a double-quote (") or a newline will be escaped using double-quotes.
318 *
319 * To convert an array of objects to an array of arrays while explicitly specifying the columns, use array.map.
320 * If you like, you can also array.concat this result with an array of column names to generate the first row.
321 *
322 * Equivalent to `dsvFormat("\t").formatRows`.
323 *
324 * @param rows An array of array of string rows.
325 */
326export function tsvFormatRows(rows: readonly string[][]): string;
327
328// tsvFormatRow(...) ========================================================================
329
330/**
331 * Equivalent to dsvFormat("\t").formatRow.
332 *
333 * @param row An array of strings representing a row.
334 */
335export function tsvFormatRow(row: readonly string[]): string;
336
337// tsvFormatValue(...) ========================================================================
338
339/**
340 * Equivalent to dsvFormat("\t").formatValue.
341 *
342 * @param value A value.
343 */
344export function tsvFormatValue(value: string): string;
345
346// ------------------------------------------------------------------------------------------
347// DSV Generalized Parsers and Formatters
348// ------------------------------------------------------------------------------------------
349
350/**
351 * A DSV parser and formatter
352 */
353export interface DSV {
354 /**
355 * Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.
356 *
357 * Unlike dsv.parseRows, this method requires that the first line of the DSV content contains a delimiter-separated list of column names;
358 * these column names become the attributes on the returned objects.
359 *
360 * The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).
361 *
362 * If the column names are not unique, only the last value is returned for each name; to access all values, use dsv.parseRows instead.
363 *
364 * Note: requires unsafe-eval content security policy.
365 *
366 * @param dsvString A string, which must be in the delimiter-separated values format with the appropriate delimiter.
367 */
368 // eslint-disable-next-line no-unnecessary-generics
369 parse<Columns extends string>(dsvString: string): DSVRowArray<Columns>;
370 /**
371 * Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.
372 *
373 * Unlike dsv.parseRows, this method requires that the first line of the DSV content contains a delimiter-separated list of column names;
374 * these column names become the attributes on the returned objects.
375 *
376 * The returned array also exposes a columns property containing the column names in input order (in contrast to Object.keys, whose iteration order is arbitrary).
377 *
378 * If the column names are not unique, only the last value is returned for each name; to access all values, use dsv.parseRows instead.
379 *
380 * Note: requires unsafe-eval content security policy.
381 *
382 * @param dsvString A string, which must be in the delimiter-separated values format with the appropriate delimiter.
383 * @param row A row conversion function which is invoked for each row, being passed an object representing the current row (d),
384 * 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,
385 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
386 * In effect, row is similar to applying a map and filter operator to the returned rows.
387 */
388 parse<ParsedRow extends object, Columns extends string>(
389 dsvString: string,
390 row: (rawRow: DSVRowString<Columns>, index: number, columns: Columns[]) => ParsedRow | undefined | null
391 ): DSVParsedArray<ParsedRow>;
392
393 /**
394 * Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of arrays representing the parsed rows.
395 *
396 * Unlike dsv.parse, this method treats the header line as a standard row, and should be used whenever DSV content does not contain a header.
397 * Each row is represented as an array rather than an object. Rows may have variable length.
398 *
399 * If a row conversion function is not specified, field values are strings. For safety, there is no automatic conversion to numbers, dates, or other types.
400 * In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the + operator), but better is to specify a row conversion function.
401 *
402 * @param dsvString A string, which must be in the delimiter-separated values format with the appropriate delimiter.
403 */
404 parseRows(dsvString: string): string[][];
405 /**
406 * Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of arrays representing the parsed rows.
407 *
408 * Unlike dsv.parse, this method treats the header line as a standard row, and should be used whenever DSV content does not contain a header.
409 * Each row is represented as an array rather than an object. Rows may have variable length.
410 *
411 * @param dsvString A string, which must be in the delimiter-separated values format with the appropriate delimiter.
412 * @param row A row conversion function which is invoked for each row, being passed an array representing the current row (d), the index (i)
413 * starting at zero for the first row, and the array of column names. If the returned value is null or undefined,
414 * the row is skipped and will be omitted from the array returned by dsv.parse; otherwise, the returned value defines the corresponding row object.
415 * In effect, row is similar to applying a map and filter operator to the returned rows.
416 */
417 parseRows<ParsedRow extends object>(
418 dsvString: string,
419 row: (rawRow: string[], index: number) => ParsedRow | undefined | null
420 ): ParsedRow[];
421
422 /**
423 * Formats the specified array of object rows as delimiter-separated values, returning a string.
424 * This operation is the inverse of dsv.parse. Each row will be separated by a newline (\n),
425 * and each column within each row will be separated by the delimiter (such as a comma, ,).
426 * Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
427 *
428 * If columns is not specified, the list of column names that forms the header row is determined by the union of all properties on all objects in rows;
429 * the order of columns is nondeterministic.
430 *
431 * @param rows Array of object rows.
432 * @param columns An array of strings representing the column names.
433 */
434 format<T extends object>(rows: readonly T[], columns?: ReadonlyArray<keyof T>): string;
435
436 /**
437 * Equivalent to dsv.format, but omits the header row.
438 * This is useful, for example, when appending rows to an existing file.
439 *
440 * @param rows Array of object rows.
441 * @param columns An array of strings representing the column names.
442 */
443 formatBody<T extends object>(rows: readonly T[], columns?: ReadonlyArray<keyof T>): string;
444
445 /**
446 * Formats the specified array of array of string rows as delimiter-separated values, returning a string.
447 * This operation is the reverse of dsv.parseRows. Each row will be separated by a newline (\n),
448 * and each column within each row will be separated by the delimiter (such as a comma, ,).
449 * Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
450 *
451 * To convert an array of objects to an array of arrays while explicitly specifying the columns, use array.map.
452 * If you like, you can also array.concat this result with an array of column names to generate the first row.
453 *
454 * @param rows An array of array of string rows.
455 */
456 formatRows(rows: readonly string[][]): string;
457
458 /**
459 * Formats a single array row of strings as delimiter-separated values, returning a string.
460 * Each column within the row will be separated by the delimiter (such as a comma, ,).
461 * Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
462 *
463 * @param row An array of strings representing a row.
464 */
465 formatRow(row: readonly string[]): string;
466
467 /**
468 * Format a single value or string as a delimiter-separated value, returning a string.
469 * A value that contains either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
470 *
471 * @param value A value.
472 */
473 formatValue(value: string): string;
474}
475
476/**
477 * Constructs a new DSV parser and formatter for the specified delimiter.
478 *
479 * @param delimiter A delimiter character. The delimiter must be a single character (i.e., a single 16-bit code unit);
480 * so, ASCII delimiters are fine, but emoji delimiters are not.
481 */
482export function dsvFormat(delimiter: string): DSV;
483
484/**
485 * Infers the types of values on the object and coerces them accordingly, returning the mutated object.
486 * This function is intended to be used as a row accessor function in conjunction with dsv.parse and dsv.parseRows.
487 *
488 * @param object An object (or array) representing a parsed row
489 */
490export function autoType<ParsedRow extends object | undefined | null, Columns extends string>(
491 // eslint-disable-next-line no-unnecessary-generics
492 object: DSVRowString<Columns> | readonly string[]
493// eslint-disable-next-line no-unnecessary-generics
494): ParsedRow;