2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2024-06-27 05:52:13 +00:00
|
|
|
/**
|
|
|
|
* Reads and writes comma-separated values (CSV) files.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* import { parse } from "@std/csv/parse";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-06-27 05:52:13 +00:00
|
|
|
*
|
|
|
|
* const string = "a,b,c\nd,e,f";
|
|
|
|
*
|
|
|
|
* assertEquals(parse(string, { skipFirstRow: false }), [["a", "b", "c"], ["d", "e", "f"]]);
|
|
|
|
* assertEquals(parse(string, { skipFirstRow: true }), [{ a: "d", b: "e", c: "f" }]);
|
|
|
|
* assertEquals(parse(string, { columns: ["x", "y", "z"] }), [{ x: "a", y: "b", z: "c" }, { x: "d", y: "e", z: "f" }]);
|
|
|
|
* ```
|
2023-03-24 03:07:30 +00:00
|
|
|
*
|
|
|
|
* There are many kinds of CSV files; this module supports the format described
|
2024-05-20 07:14:09 +00:00
|
|
|
* in {@link https://www.rfc-editor.org/rfc/rfc4180.html | RFC 4180}.
|
2023-03-24 03:07:30 +00:00
|
|
|
*
|
|
|
|
* A csv file contains zero or more records of one or more fields per record.
|
|
|
|
* Each record is separated by the newline character. The final record may
|
|
|
|
* optionally be followed by a newline character.
|
|
|
|
*
|
|
|
|
* ```csv
|
|
|
|
* field1,field2,field3
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* White space is considered part of a field.
|
|
|
|
*
|
|
|
|
* Carriage returns before newline characters are silently removed.
|
|
|
|
*
|
|
|
|
* Blank lines are ignored. A line with only whitespace characters (excluding
|
|
|
|
* the ending newline character) is not considered a blank line.
|
|
|
|
*
|
|
|
|
* Fields which start and stop with the quote character " are called
|
|
|
|
* quoted-fields. The beginning and ending quote are not part of the field.
|
|
|
|
*
|
|
|
|
* The source:
|
|
|
|
*
|
|
|
|
* ```csv
|
|
|
|
* normal string,"quoted-field"
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* results in the fields
|
|
|
|
*
|
2024-06-03 05:44:18 +00:00
|
|
|
* ```ts no-assert
|
2023-03-24 03:07:30 +00:00
|
|
|
* [`normal string`, `quoted-field`]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Within a quoted-field a quote character followed by a second quote character is considered a single quote.
|
|
|
|
*
|
|
|
|
* ```csv
|
|
|
|
* "the ""word"" is true","a ""quoted-field"""
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* results in
|
|
|
|
*
|
2024-08-02 04:24:15 +00:00
|
|
|
* ```ts no-assert
|
2023-03-24 03:07:30 +00:00
|
|
|
* [`the "word" is true`, `a "quoted-field"`]
|
2024-08-02 04:24:15 +00:00
|
|
|
* ```
|
2023-03-24 03:07:30 +00:00
|
|
|
*
|
|
|
|
* Newlines and commas may be included in a quoted-field
|
|
|
|
*
|
|
|
|
* ```csv
|
|
|
|
* "Multi-line
|
|
|
|
* field","comma is ,"
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* results in
|
|
|
|
*
|
2024-06-03 05:44:18 +00:00
|
|
|
* ```ts no-assert
|
2023-03-24 03:07:30 +00:00
|
|
|
* [`Multi-line
|
|
|
|
* field`, `comma is ,`]
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @module
|
|
|
|
*/
|
|
|
|
|
2023-03-13 05:56:25 +00:00
|
|
|
export * from "./parse.ts";
|
2024-06-28 06:07:36 +00:00
|
|
|
export * from "./parse_stream.ts";
|
|
|
|
export * from "./stringify.ts";
|
|
|
|
export * from "./stringify_stream.ts";
|