@@ -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
0 commit comments