Skip to content

Commit 2accb2b

Browse files
authored
docs(csv): more examples for stringify and CsvStringifyStream (denoland#5606)
1 parent 12d6097 commit 2accb2b

File tree

2 files changed

+228
-26
lines changed

2 files changed

+228
-26
lines changed

csv/stringify.ts

Lines changed: 173 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,99 @@ function getValuesFromItem(
219219
/**
220220
* Converts an array of objects into a CSV string.
221221
*
222-
* @example Usage
222+
* @example Default options
223+
* ```ts
224+
* import { stringify } from "@std/csv/stringify";
225+
* import { assertEquals } from "@std/assert/equals";
226+
*
227+
* const data = [
228+
* ["Rick", 70],
229+
* ["Morty", 14],
230+
* ];
231+
*
232+
* assertEquals(stringify(data), `Rick,70\r\nMorty,14\r\n`);
233+
* ```
234+
*
235+
* @example Give an array of objects and specify columns
236+
* ```ts
237+
* import { stringify } from "@std/csv/stringify";
238+
* import { assertEquals } from "@std/assert/equals";
239+
*
240+
* const data = [
241+
* { name: "Rick", age: 70 },
242+
* { name: "Morty", age: 14 },
243+
* ];
244+
*
245+
* const columns = ["name", "age"];
246+
*
247+
* assertEquals(stringify(data, { columns }), `name,age\r\nRick,70\r\nMorty,14\r\n`);
248+
* ```
249+
*
250+
* @example Give an array of objects without specifying columns
251+
* ```ts
252+
* import { stringify } from "@std/csv/stringify";
253+
* import { assertThrows } from "@std/assert/throws";
254+
*
255+
* const data = [
256+
* { name: "Rick", age: 70 },
257+
* { name: "Morty", age: 14 },
258+
* ];
259+
*
260+
* assertThrows(
261+
* () => stringify(data),
262+
* TypeError,
263+
* "No property accessor function was provided for object",
264+
* );
265+
* ```
266+
*
267+
* @example Give an array of objects and specify columns with `headers: false`
268+
* ```ts
269+
* import { stringify } from "@std/csv/stringify";
270+
* import { assertEquals } from "@std/assert/equals";
271+
*
272+
* const data = [
273+
* { name: "Rick", age: 70 },
274+
* { name: "Morty", age: 14 },
275+
* ];
276+
*
277+
* const columns = ["name", "age"];
278+
*
279+
* assertEquals(
280+
* stringify(data, { columns, headers: false }),
281+
* `Rick,70\r\nMorty,14\r\n`,
282+
* );
283+
* ```
284+
*
285+
* @example Give an array of objects and specify columns with renaming
286+
* ```ts
287+
* import { stringify } from "@std/csv/stringify";
288+
* import { assertEquals } from "@std/assert/equals";
289+
*
290+
* const data = [
291+
* { name: "Rick", age: 70 },
292+
* { name: "Morty", age: 14 },
293+
* ];
294+
*
295+
* const columns = [
296+
* { prop: "name", header: "user name" },
297+
* "age",
298+
* ];
299+
*
300+
* assertEquals(
301+
* stringify(data, { columns }),
302+
* `user name,age\r\nRick,70\r\nMorty,14\r\n`,
303+
* );
304+
* ```
305+
*
306+
* @example Give an array of objects with nested property and specify columns
223307
* ```ts
224308
* import {
225309
* Column,
226310
* stringify,
227311
* } from "@std/csv/stringify";
228-
* import { assertEquals } from "@std/assert";
229-
*
230-
* type Character = {
231-
* age: number;
232-
* name: {
233-
* first: string;
234-
* last: string;
235-
* };
236-
* };
312+
* import { assertEquals } from "@std/assert/equals";
237313
*
238-
* const data: Character[] = [
314+
* const data = [
239315
* {
240316
* age: 70,
241317
* name: {
@@ -252,12 +328,96 @@ function getValuesFromItem(
252328
* },
253329
* ];
254330
*
255-
* let columns: Column[] = [
331+
* const columns: Column[] = [
256332
* ["name", "first"],
257333
* "age",
258334
* ];
259335
*
260-
* assertEquals(stringify(data, { columns }), `first,age\r\nRick,70\r\nMorty,14\r\n`);
336+
* assertEquals(
337+
* stringify(data, { columns }),
338+
* `first,age\r\nRick,70\r\nMorty,14\r\n`,
339+
* );
340+
* ```
341+
*
342+
* @example Give an array of objects with nested property and specify columns
343+
* with renaming
344+
* ```ts
345+
* import {
346+
* Column,
347+
* stringify,
348+
* } from "@std/csv/stringify";
349+
* import { assertEquals } from "@std/assert/equals";
350+
*
351+
* const data = [
352+
* {
353+
* age: 70,
354+
* name: {
355+
* first: "Rick",
356+
* last: "Sanchez",
357+
* },
358+
* },
359+
* {
360+
* age: 14,
361+
* name: {
362+
* first: "Morty",
363+
* last: "Smith",
364+
* },
365+
* },
366+
* ];
367+
*
368+
* const columns: Column[] = [
369+
* { prop: ["name", "first"], header: "first name" },
370+
* "age",
371+
* ];
372+
*
373+
* assertEquals(
374+
* stringify(data, { columns }),
375+
* `first name,age\r\nRick,70\r\nMorty,14\r\n`,
376+
* );
377+
* ```
378+
*
379+
* @example Give an array of string arrays and specify columns with renaming
380+
* ```ts
381+
* import { stringify } from "@std/csv/stringify";
382+
* import { assertEquals } from "@std/assert/equals";
383+
*
384+
* const data = [
385+
* ["Rick", 70],
386+
* ["Morty", 14],
387+
* ];
388+
*
389+
* const columns = [
390+
* { prop: 0, header: "name" },
391+
* { prop: 1, header: "age" },
392+
* ];
393+
*
394+
* assertEquals(
395+
* stringify(data, { columns }),
396+
* `name,age\r\nRick,70\r\nMorty,14\r\n`,
397+
* );
398+
* ```
399+
*
400+
* @example Emit TSV (tab-separated values) with `separator: "\t"`
401+
* ```ts
402+
* import { stringify } from "@std/csv/stringify";
403+
* import { assertEquals } from "@std/assert/equals";
404+
*
405+
* const data = [
406+
* ["Rick", 70],
407+
* ["Morty", 14],
408+
* ];
409+
*
410+
* assertEquals(stringify(data, { separator: "\t" }), `Rick\t70\r\nMorty\t14\r\n`);
411+
* ```
412+
*
413+
* @example Prepend a byte-order mark with `bom: true`
414+
* ```ts
415+
* import { stringify } from "@std/csv/stringify";
416+
* import { assertEquals } from "@std/assert/equals";
417+
*
418+
* const data = [["Rick", 70]];
419+
*
420+
* assertEquals(stringify(data, { bom: true }), "\ufeffRick,70\r\n");
261421
* ```
262422
*
263423
* @param data The source data to stringify. It's an array of items which are

csv/stringify_stream.ts

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,65 @@ export interface CsvStringifyStreamOptions {
2424
/**
2525
* Convert each chunk to a CSV record.
2626
*
27-
* @example Usage
28-
* ```ts no-assert
27+
* @example Write CSV to a file
28+
* ```ts
2929
* import { CsvStringifyStream } from "@std/csv/stringify-stream";
30+
* import { assertEquals } from "@std/assert/equals";
3031
*
31-
* const path = await Deno.makeTempFile();
32+
* async function writeCsvToTempFile(): Promise<string> {
33+
* const path = await Deno.makeTempFile();
34+
* using file = await Deno.open(path, { write: true });
3235
*
33-
* const file = await Deno.open(path, { create: true, write: true });
34-
* const readable = ReadableStream.from([
35-
* { id: 1, name: "one" },
36-
* { id: 2, name: "two" },
37-
* { id: 3, name: "three" },
38-
* ]);
36+
* const readable = ReadableStream.from([
37+
* { id: 1, name: "one" },
38+
* { id: 2, name: "two" },
39+
* { id: 3, name: "three" },
40+
* ]);
3941
*
40-
* await readable
41-
* .pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }))
42-
* .pipeThrough(new TextEncoderStream())
43-
* .pipeTo(file.writable);
42+
* await readable
43+
* .pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }))
44+
* .pipeThrough(new TextEncoderStream())
45+
* .pipeTo(file.writable);
46+
*
47+
* return path;
48+
* }
49+
*
50+
* const path = await writeCsvToTempFile();
51+
* const content = await Deno.readTextFile(path);
52+
* assertEquals(content, "id,name\r\n1,one\r\n2,two\r\n3,three\r\n");
53+
* ```
54+
*
55+
* @example Write TSV to a file
56+
* ```ts
57+
* import { CsvStringifyStream } from "@std/csv/stringify-stream";
58+
* import { assertEquals } from "@std/assert/equals";
59+
*
60+
* async function writeTsvToTempFile(): Promise<string> {
61+
* const path = await Deno.makeTempFile();
62+
* using file = await Deno.open(path, { write: true });
63+
*
64+
* const readable = ReadableStream.from([
65+
* { id: 1, name: "one" },
66+
* { id: 2, name: "two" },
67+
* { id: 3, name: "three" },
68+
* ]);
69+
*
70+
* await readable
71+
* .pipeThrough(
72+
* new CsvStringifyStream({
73+
* columns: ["id", "name"],
74+
* separator: "\t",
75+
* }),
76+
* )
77+
* .pipeThrough(new TextEncoderStream())
78+
* .pipeTo(file.writable);
79+
*
80+
* return path;
81+
* }
82+
*
83+
* const path = await writeTsvToTempFile();
84+
* const content = await Deno.readTextFile(path);
85+
* assertEquals(content, "id\tname\r\n1\tone\r\n2\ttwo\r\n3\tthree\r\n");
4486
* ```
4587
*
4688
* @typeParam TOptions The type of options for the stream.

0 commit comments

Comments
 (0)