Skip to main content
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects } from "../testing/asserts.ts";
import { Column, NEWLINE, stringify, StringifyError } from "./csv_stringify.ts";
Deno.test({ name: "[CSV_stringify] Access array index using string", async fn() { const columns = ["a"]; const data = [["foo"], ["bar"]]; const errorMessage = 'Property accessor is not of type "number"'; await assertRejects( async () => await stringify(data, columns), StringifyError, errorMessage, ); },});Deno.test( { name: "[CSV_stringify] Double quote in separator",
async fn() { const columns = [0]; const data = [["foo"], ["bar"]]; const errorMessage = [ "Separator cannot include the following strings:", ' - U+0022: Quotation mark (")', " - U+000D U+000A: Carriage Return + Line Feed (\\r\\n)", ].join("\n"); const options = { separator: '"' }; await assertRejects( async () => await stringify(data, columns, options), StringifyError, errorMessage, ); }, },);Deno.test( { name: "[CSV_stringify] CRLF in separator", async fn() { const columns = [0]; const data = [["foo"], ["bar"]]; const errorMessage = [ "Separator cannot include the following strings:", ' - U+0022: Quotation mark (")', " - U+000D U+000A: Carriage Return + Line Feed (\\r\\n)", ].join("\n"); const options = { separator: "\r\n" }; await assertRejects( async () => await stringify(data, columns, options), StringifyError, errorMessage, ); }, },);Deno.test( { name: "[CSV_stringify] Transform function", async fn() { const columns: Column[] = [ { fn: (obj: string) => obj.toUpperCase(), prop: "msg", }, ]; const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; await assertRejects( async () => await stringify(data, columns), TypeError, ); }, },);Deno.test( { name: "[CSV_stringify] No data, no columns",
async fn() { const columns: string[] = []; const data: string[][] = []; const output = NEWLINE; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] No data, no columns, no headers", async fn() { const columns: string[] = []; const data: string[][] = []; const output = ``; const options = { headers: false }; assertEquals(await stringify(data, columns, options), output); }, },);Deno.test( { name: "[CSV_stringify] No data, columns", async fn() { const columns = ["a"]; const data: string[][] = []; const output = `a${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] No data, columns, no headers",
async fn() { const columns = ["a"]; const data: string[][] = []; const output = ``; const options = { headers: false }; assertEquals(await stringify(data, columns, options), output); }, },);Deno.test( { name: "[CSV_stringify] Data, no columns", async fn() { const columns: string[] = []; const data = [{ a: 1 }, { a: 2 }]; const output = `${NEWLINE}${NEWLINE}${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Separator: CR", async fn() { const columns = [0, 1]; const data = [["foo", "bar"], ["baz", "qux"]]; const output = `0\r1${NEWLINE}foo\rbar${NEWLINE}baz\rqux${NEWLINE}`; const options = { separator: "\r" }; assertEquals(await stringify(data, columns, options), output); }, },);Deno.test( { name: "[CSV_stringify] Separator: LF",
async fn() { const columns = [0, 1]; const data = [["foo", "bar"], ["baz", "qux"]]; const output = `0\n1${NEWLINE}foo\nbar${NEWLINE}baz\nqux${NEWLINE}`; const options = { separator: "\n" }; assertEquals(await stringify(data, columns, options), output); }, },);Deno.test( { name: "[CSV_stringify] Column: number accessor", async fn() { const columns = [1]; const data = [{ 1: 1 }, { 1: 2 }]; const output = `1${NEWLINE}1${NEWLINE}2${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Explicit header value, no headers",
async fn() { const columns = [{ header: "Value", prop: "value" }]; const data = [{ value: "foo" }, { value: "bar" }]; const output = `foo${NEWLINE}bar${NEWLINE}`; const options = { headers: false }; assertEquals(await stringify(data, columns, options), output); }, },);Deno.test( { name: "[CSV_stringify] Column: number accessor,const data = array", async fn() { const columns = [1]; const data = [["key", "foo"], ["key", "bar"]]; const output = `1${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Column: array number accessor",
async fn() { const columns = [[1]]; const data = [{ 1: 1 }, { 1: 2 }]; const output = `1${NEWLINE}1${NEWLINE}2${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Column: array number accessor,const data = array", async fn() { const columns = [[1]]; const data = [["key", "foo"], ["key", "bar"]]; const output = `1${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Column: array number accessor,const data = array",
async fn() { const columns = [[1, 1]]; const data = [["key", ["key", "foo"]], ["key", ["key", "bar"]]]; const output = `1${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Column: string accessor", async fn() { const columns = ["value"]; const data = [{ value: "foo" }, { value: "bar" }]; const output = `value${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Column: array string accessor", async fn() { const columns = [["value"]]; const data = [{ value: "foo" }, { value: "bar" }]; const output = `value${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Column: array string accessor", async fn() { const columns = [["msg", "value"]]; const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; const output = `value${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Explicit header", async fn() { const columns = [ { header: "Value", prop: ["msg", "value"], }, ]; const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; const output = `Value${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Transform function 1", async fn() { const columns = [ { fn: (str: string) => str.toUpperCase(), prop: ["msg", "value"], }, ]; const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; const output = `value${NEWLINE}FOO${NEWLINE}BAR${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Transform function 1 async", async fn() { const columns = [ { fn: (str: string) => Promise.resolve(str.toUpperCase()), prop: ["msg", "value"], }, ]; const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; const output = `value${NEWLINE}FOO${NEWLINE}BAR${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);
Deno.test( { name: "[CSV_stringify] Transform function 2", async fn() { const columns = [ { fn: (obj: { value: string }) => obj.value, prop: "msg", }, ]; const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; const output = `msg${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Transform function 2, explicit header", async fn() { const columns = [ { fn: (obj: { value: string }) => obj.value, header: "Value", prop: "msg", }, ]; const data = [{ msg: { value: "foo" } }, { msg: { value: "bar" } }]; const output = `Value${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: object", async fn() { const columns = [0]; const data = [[{ value: "foo" }], [{ value: "bar" }]]; const output = `0${NEWLINE}"{""value"":""foo""}"${NEWLINE}"{""value"":""bar""}"${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: arary of objects", async fn() { const columns = [0]; const data = [ [[{ value: "foo" }, { value: "bar" }]], [[{ value: "baz" }, { value: "qux" }]], ]; const output = `0${NEWLINE}"[{""value"":""foo""},{""value"":""bar""}]"${NEWLINE}"[{""value"":""baz""},{""value"":""qux""}]"${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: array", async fn() { const columns = [0]; const data = [[["foo", "bar"]], [["baz", "qux"]]]; const output = `0${NEWLINE}"[""foo"",""bar""]"${NEWLINE}"[""baz"",""qux""]"${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: array, separator: tab",
async fn() { const columns = [0]; const data = [[["foo", "bar"]], [["baz", "qux"]]]; const output = `0${NEWLINE}"[""foo"",""bar""]"${NEWLINE}"[""baz"",""qux""]"${NEWLINE}`; const options = { separator: "\t" }; assertEquals(await stringify(data, columns, options), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: undefined", async fn() { const columns = [0]; const data = [[], []]; const output = `0${NEWLINE}${NEWLINE}${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: null", async fn() { const columns = [0]; const data = [[null], [null]]; const output = `0${NEWLINE}${NEWLINE}${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: hex number", async fn() { const columns = [0]; const data = [[0xa], [0xb]]; const output = `0${NEWLINE}10${NEWLINE}11${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: BigInt", async fn() { const columns = [0]; const data = [[BigInt("1")], [BigInt("2")]]; const output = `0${NEWLINE}1${NEWLINE}2${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: boolean", async fn() { const columns = [0]; const data = [[true], [false]]; const output = `0${NEWLINE}true${NEWLINE}false${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: string", async fn() { const columns = [0]; const data = [["foo"], ["bar"]]; const output = `0${NEWLINE}foo${NEWLINE}bar${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: symbol", async fn() { const columns = [0]; const data = [[Symbol("foo")], [Symbol("bar")]]; const output = `0${NEWLINE}Symbol(foo)${NEWLINE}Symbol(bar)${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Targeted value: function", async fn() { const columns = [0]; const data = [[(n: number) => n]]; const output = `0${NEWLINE}(n)=>n${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Value with double quote", async fn() { const columns = [0]; const data = [['foo"']]; const output = `0${NEWLINE}"foo"""${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Value with CRLF", async fn() { const columns = [0]; const data = [["foo\r\n"]]; const output = `0${NEWLINE}"foo\r\n"${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Value with CR", async fn() { const columns = [0]; const data = [["foo\r"]]; const output = `0${NEWLINE}foo\r${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Value with LF", async fn() { const columns = [0]; const data = [["foo\n"]]; const output = `0${NEWLINE}foo\n${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Value with comma", async fn() { const columns = [0]; const data = [["foo,"]]; const output = `0${NEWLINE}"foo,"${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, },);Deno.test( { name: "[CSV_stringify] Value with comma, tab separator", async fn() { const columns = [0]; const data = [["foo,"]]; const output = `0${NEWLINE}foo,${NEWLINE}`;
const options = { separator: "\t" }; assertEquals(await stringify(data, columns, options), output); }, },);