UNPKG

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