Skip to main content
Module

std/encoding/csv_test.ts

Deno standard library
Go to Latest
File
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
// Test ported from Golang// https://github.com/golang/go/blob/2cc15b1/src/encoding/csv/reader_test.go// Copyright 2011 The Go Authors. All rights reserved. BSD license.// https://github.com/golang/go/blob/master/LICENSE// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertRejects, assertThrows,} from "../testing/asserts.ts";import { Column, NEWLINE, parse, ParseError, stringify, StringifyError,} from "./csv.ts";
Deno.test({ name: "parse", async fn(t) { await t.step({ name: "Simple", fn() { const input = "a,b,c\n"; assertEquals( parse(input), [["a", "b", "c"]], ); }, }); await t.step({ name: "CRLF", fn() { const input = "a,b\r\nc,d\r\n"; assertEquals( parse(input), [ ["a", "b"], ["c", "d"], ], ); }, });
await t.step({ name: "BareCR", fn() { const input = "a,b\rc,d\r\n"; assertEquals( parse(input), [["a", "b\rc", "d"]], ); }, });
await t.step({ name: "RFC4180test", fn() { const input = '#field1,field2,field3\n"aaa","bbb","ccc"\n"a,a","bbb","ccc"\nzzz,yyy,xxx'; assertEquals( parse(input), [ ["#field1", "field2", "field3"], ["aaa", "bbb", "ccc"], ["a,a", `bbb`, "ccc"], ["zzz", "yyy", "xxx"], ], ); }, }); await t.step({ name: "NoEOLTest", fn() { const input = "a,b,c"; assertEquals( parse(input), [["a", "b", "c"]], ); }, });
await t.step({ name: "Semicolon", fn() { const input = "a;b;c\n"; assertEquals( parse(input, { separator: ";" }), [["a", "b", "c"]], ); }, });
await t.step({ name: "MultiLine", fn() { const input = '"two\nline","one line","three\nline\nfield"'; assertEquals( parse(input), [["two\nline", "one line", "three\nline\nfield"]], ); }, });
await t.step({ name: "BlankLine", fn() { const input = "a,b,c\n\nd,e,f\n\n"; assertEquals( parse(input), [ ["a", "b", "c"], ["d", "e", "f"], ], ); }, });
await t.step({ name: "BlankLineFieldCount", fn() { const input = "a,b,c\n\nd,e,f\n\n"; assertEquals( parse(input, { fieldsPerRecord: 0 }), [ ["a", "b", "c"], ["d", "e", "f"], ], ); }, });
await t.step({ name: "TrimSpace", fn() { const input = " a, b, c\n"; assertEquals( parse(input, { trimLeadingSpace: true }), [["a", "b", "c"]], ); }, });
await t.step({ name: "LeadingSpace", fn() { const input = " a, b, c\n"; const output = [[" a", " b", " c"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "Comment", fn() { const input = "#1,2,3\na,b,c\n#comment"; const output = [["a", "b", "c"]]; assertEquals(parse(input, { comment: "#" }), output); }, }); await t.step({ name: "NoComment", fn() { const input = "#1,2,3\na,b,c"; const output = [ ["#1", "2", "3"], ["a", "b", "c"], ]; assertEquals(parse(input), output); }, }); await t.step({ name: "LazyQuotes", fn() { const input = `a "word","1"2",a","b`; const output = [[`a "word"`, `1"2`, `a"`, `b`]]; assertEquals(parse(input, { lazyQuotes: true }), output); }, }); await t.step({ name: "BareQuotes", fn() { const input = `a "word","1"2",a"`; const output = [[`a "word"`, `1"2`, `a"`]]; assertEquals(parse(input, { lazyQuotes: true }), output); }, }); await t.step({ name: "BareDoubleQuotes", fn() { const input = `a""b,c`; const output = [[`a""b`, `c`]]; assertEquals(parse(input, { lazyQuotes: true }), output); }, }); await t.step({ name: "BadDoubleQuotes", fn() { const input = `a""b,c`; assertThrows( () => parse(input), ParseError, 'parse error on line 1, column 1: bare " in non-quoted-field', ); }, }); await t.step({ name: "TrimQuote", fn() { const input = ` "a"," b",c`; const output = [["a", " b", "c"]]; assertEquals(parse(input, { trimLeadingSpace: true }), output); }, }); await t.step({ name: "BadBareQuote", fn() { const input = `a "word","b"`; assertThrows( () => parse(input), ParseError, 'parse error on line 1, column 2: bare " in non-quoted-field', ); }, }); await t.step({ name: "BadTrailingQuote", fn() { const input = `"a word",b"`; assertThrows( () => parse(input), ParseError, 'parse error on line 1, column 10: bare " in non-quoted-field', ); }, }); await t.step({ name: "ExtraneousQuote", fn() { const input = `"a "word","b"`; assertThrows( () => parse(input), ParseError, `parse error on line 1, column 3: extraneous or missing " in quoted-field`, ); }, }); await t.step({ name: "BadFieldCount", fn() { const input = "a,b,c\nd,e"; assertThrows( () => parse(input, { fieldsPerRecord: 0 }), ParseError, "record on line 2: wrong number of fields", ); }, }); await t.step({ name: "BadFieldCount1", fn() { const input = `a,b,c`; assertThrows( () => parse(input, { fieldsPerRecord: 2 }), ParseError, "record on line 1: wrong number of fields", ); }, }); await t.step({ name: "FieldCount", fn() { const input = "a,b,c\nd,e"; const output = [ ["a", "b", "c"], ["d", "e"], ]; assertEquals(parse(input), output); }, }); await t.step({ name: "TrailingCommaEOF", fn() { const input = "a,b,c,"; const output = [["a", "b", "c", ""]]; assertEquals(parse(input), output); }, }); await t.step({ name: "TrailingCommaEOL", fn() { const input = "a,b,c,\n"; const output = [["a", "b", "c", ""]]; assertEquals(parse(input), output); }, }); await t.step({ name: "TrailingCommaSpaceEOF", fn() { const input = "a,b,c, "; const output = [["a", "b", "c", ""]]; assertEquals(parse(input, { trimLeadingSpace: true }), output); }, }); await t.step({ name: "TrailingCommaSpaceEOL", fn() { const input = "a,b,c, \n"; const output = [["a", "b", "c", ""]]; assertEquals(parse(input, { trimLeadingSpace: true }), output); }, }); await t.step({ name: "TrailingCommaLine3", fn() { const input = "a,b,c\nd,e,f\ng,hi,"; const output = [ ["a", "b", "c"], ["d", "e", "f"], ["g", "hi", ""], ]; assertEquals(parse(input, { trimLeadingSpace: true }), output); }, }); await t.step({ name: "NotTrailingComma3", fn() { const input = "a,b,c, \n"; const output = [["a", "b", "c", " "]]; assertEquals(parse(input), output); }, }); await t.step({ name: "CommaFieldTest", fn() { const input = `x,y,z,w\nx,y,z,\nx,y,,\nx,,,\n,,,\n"x","y","z","w"\n"x","y","z",""\n"x","y","",""\n"x","","",""\n"","","",""\n`; const output = [ ["x", "y", "z", "w"], ["x", "y", "z", ""], ["x", "y", "", ""], ["x", "", "", ""], ["", "", "", ""], ["x", "y", "z", "w"], ["x", "y", "z", ""], ["x", "y", "", ""], ["x", "", "", ""], ["", "", "", ""], ]; assertEquals(parse(input), output); }, }); await t.step({ name: "TrailingCommaIneffective1", fn() { const input = "a,b,\nc,d,e"; const output = [ ["a", "b", ""], ["c", "d", "e"], ]; assertEquals(parse(input, { trimLeadingSpace: true }), output); }, }); await t.step({ name: "ReadAllReuseRecord", fn() { const input = "a,b\nc,d"; const output = [ ["a", "b"], ["c", "d"], ]; assertEquals(parse(input), output); // ReuseRecord: true, }, }); await t.step({ name: "StartLine1", // Issue 19019 fn() { const input = 'a,"b\nc"d,e'; assertThrows( () => parse(input, { fieldsPerRecord: 2 }), ParseError, 'record on line 1; parse error on line 2, column 1: extraneous or missing " in quoted-field', ); }, }); await t.step({ name: "StartLine2", fn() { const input = 'a,b\n"d\n\n,e'; assertThrows( () => parse(input, { fieldsPerRecord: 2 }), ParseError, 'record on line 2; parse error on line 5, column 0: extraneous or missing " in quoted-field', ); }, }); await t.step({ name: "CRLFInQuotedField", // Issue 21201 fn() { const input = 'A,"Hello\r\nHi",B\r\n'; const output = [["A", "Hello\nHi", "B"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "BinaryBlobField", // Issue 19410 fn() { const input = "x09\x41\xb4\x1c,aktau"; const output = [["x09A\xb4\x1c", "aktau"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "TrailingCR", fn() { const input = "field1,field2\r"; const output = [["field1", "field2"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "QuotedTrailingCR", fn() { const input = '"field"\r'; const output = [["field"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "QuotedTrailingCRCR", fn() { const input = '"field"\r\r'; assertThrows( () => parse(input, { fieldsPerRecord: 2 }), ParseError, 'parse error on line 1, column 6: extraneous or missing " in quoted-field', ); }, }); await t.step({ name: "FieldCR", fn() { const input = "field\rfield\r"; const output = [["field\rfield"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "FieldCRCR", fn() { const input = "field\r\rfield\r\r"; const output = [["field\r\rfield\r"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "FieldCRCRLF", fn() { const input = "field\r\r\nfield\r\r\n"; const output = [["field\r"], ["field\r"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "FieldCRCRLFCR", fn() { const input = "field\r\r\n\rfield\r\r\n\r"; const output = [["field\r"], ["\rfield\r"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "FieldCRCRLFCRCR", fn() { const input = "field\r\r\n\r\rfield\r\r\n\r\r"; const output = [["field\r"], ["\r\rfield\r"], ["\r"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "MultiFieldCRCRLFCRCR", fn() { const input = "field1,field2\r\r\n\r\rfield1,field2\r\r\n\r\r,"; const output = [ ["field1", "field2\r"], ["\r\rfield1", "field2\r"], ["\r\r", ""], ]; assertEquals(parse(input), output); }, }); await t.step({ name: "NonASCIICommaAndComment", fn() { const input = "a£b,c£ \td,e\n€ comment\n"; const output = [["a", "b,c", "d,e"]]; assertEquals( parse(input, { trimLeadingSpace: true, separator: "£", comment: "€", }), output, ); }, }); await t.step({ name: "NonASCIICommaAndCommentWithQuotes", fn() { const input = 'a€" b,"€ c\nλ comment\n'; const output = [["a", " b,", " c"]]; assertEquals( parse(input, { separator: "€", comment: "λ" }), output, ); }, }); await t.step( { // λ and θ start with the same byte. // This tests that the parser doesn't confuse such characters. name: "NonASCIICommaConfusion", fn() { const input = '"abθcd"λefθgh'; const output = [["abθcd", "efθgh"]]; assertEquals( parse(input, { separator: "λ", comment: "€" }), output, ); }, }, ); await t.step({ name: "NonASCIICommentConfusion", fn() { const input = "λ\nλ\nθ\nλ\n"; const output = [["λ"], ["λ"], ["λ"]]; assertEquals(parse(input, { comment: "θ" }), output); }, }); await t.step({ name: "QuotedFieldMultipleLF", fn() { const input = '"\n\n\n\n"'; const output = [["\n\n\n\n"]]; assertEquals(parse(input), output); }, }); await t.step({ name: "MultipleCRLF", fn() { const input = "\r\n\r\n\r\n\r\n"; const output: string[][] = []; assertEquals(parse(input), output); }, /** * The implementation may read each line in several chunks if * it doesn't fit entirely. * in the read buffer, so we should test the code to handle that condition. */ } /* TODO(kt3k): Enable this test case) await t.step({ name: "HugeLines", fn() { const input = "#ignore\n".repeat(10000) + "@".repeat(5000) + "," "*".repeat(5000), const output = [["@".repeat(5000), "*".repeat(5000)]] assertEquals(parse(input), output) Comment: "#", }, }*/); await t.step({ name: "QuoteWithTrailingCRLF", fn() { const input = '"foo"bar"\r\n'; assertThrows( () => parse(input), ParseError, `parse error on line 1, column 4: extraneous or missing " in quoted-field`, ); }, }); await t.step({ name: "LazyQuoteWithTrailingCRLF", fn() { const input = '"foo"bar"\r\n'; const output = [[`foo"bar`]]; assertEquals(parse(input, { lazyQuotes: true }), output); }, }); await t.step({ name: "DoubleQuoteWithTrailingCRLF", fn() { const input = '"foo""bar"\r\n'; const output = [[`foo"bar`]]; assertEquals(parse(input), output); }, }); await t.step({ name: "EvenQuotes", fn() { const input = `""""""""`; const output = [[`"""`]]; assertEquals(parse(input), output); }, }); await t.step({ name: "OddQuotes", fn() { const input = `"""""""`; assertThrows( () => parse(input), ParseError, `parse error on line 1, column 7: extraneous or missing " in quoted-field`, ); }, }); await t.step({ name: "LazyOddQuotes", fn() { const input = `"""""""`; const output = [[`"""`]]; assertEquals(parse(input, { lazyQuotes: true }), output); }, }); await t.step({ name: "BadComma1", fn() { const input = ""; assertThrows( () => parse(input, { separator: "\n" }), Error, "Invalid Delimiter", ); }, }); await t.step({ name: "BadComma2", fn() { const input = ""; assertThrows( () => parse(input, { separator: "\r" }), Error, "Invalid Delimiter", ); }, }); await t.step({ name: "BadComma3", fn() { const input = ""; assertThrows( () => parse(input, { separator: '"' }), Error, "Invalid Delimiter", ); }, }); await t.step({ name: "BadComment1", fn() { const input = ""; assertThrows( () => parse(input, { comment: "\n" }), Error, "Invalid Delimiter", ); }, }); await t.step({ name: "BadComment2", fn() { const input = ""; assertThrows( () => parse(input, { comment: "\r" }), Error, "Invalid Delimiter", ); }, }); await t.step({ name: "BadCommaComment", fn() { const input = ""; assertThrows( () => parse(input, { separator: "X", comment: "X" }), Error, "Invalid Delimiter", ); }, });
await t.step({ name: "simple", fn() { const input = "a,b,c"; const output = [["a", "b", "c"]]; assertEquals(parse(input, { skipFirstRow: false }), output); }, }); await t.step({ name: "simple Bufreader", fn() { const input = "a,b,c"; const output = [["a", "b", "c"]]; assertEquals(parse(input, { skipFirstRow: false }), output); }, }); await t.step({ name: "multiline", fn() { const input = "a,b,c\ne,f,g\n"; const output = [ ["a", "b", "c"], ["e", "f", "g"], ]; assertEquals(parse(input, { skipFirstRow: false }), output); }, }); await t.step({ name: "header mapping boolean", fn() { const input = "a,b,c\ne,f,g\n"; const output = [{ a: "e", b: "f", c: "g" }]; assertEquals(parse(input, { skipFirstRow: true }), output); }, }); await t.step({ name: "header mapping array", fn() { const input = "a,b,c\ne,f,g\n"; const output = [ { this: "a", is: "b", sparta: "c" }, { this: "e", is: "f", sparta: "g" }, ]; assertEquals( parse(input, { columns: ["this", "is", "sparta"] }), output, ); }, });
await t.step({ name: "provides both opts.skipFirstRow and opts.columns", fn() { const input = "a,b,1\nc,d,2\ne,f,3"; const output = [ { foo: "c", bar: "d", baz: "2" }, { foo: "e", bar: "f", baz: "3" }, ]; assertEquals( parse(input, { skipFirstRow: true, columns: ["foo", "bar", "baz"], }), output, ); }, }); await t.step({ name: "mismatching number of headers and fields", fn() { const input = "a,b,c\nd,e"; assertThrows( () => parse(input, { skipFirstRow: true, columns: ["foo", "bar", "baz"], }), Error, "Error number of fields line: 1\nNumber of fields found: 3\nExpected number of fields: 2", ); }, }); },});
Deno.test({ name: "stringify", async fn(t) { await t.step({ name: "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, ); }, }); await t.step( { name: "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, ); }, }, ); await t.step( { name: "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, ); }, }, ); await t.step( { name: "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, ); }, }, ); await t.step( { name: "No data, no columns",
async fn() { const columns: string[] = []; const data: string[][] = []; const output = NEWLINE; assertEquals(await stringify(data, columns), output); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "No data, columns", async fn() { const columns = ["a"]; const data: string[][] = []; const output = `a${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, );
await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "Targeted value: undefined", async fn() { const columns = [0]; const data = [[], []]; const output = `0${NEWLINE}${NEWLINE}${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, }, ); await t.step( { name: "Targeted value: null", async fn() { const columns = [0]; const data = [[null], [null]]; const output = `0${NEWLINE}${NEWLINE}${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "Value with double quote", async fn() { const columns = [0]; const data = [['foo"']]; const output = `0${NEWLINE}"foo"""${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "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); }, }, ); await t.step( { name: "Value with comma", async fn() { const columns = [0]; const data = [["foo,"]]; const output = `0${NEWLINE}"foo,"${NEWLINE}`; assertEquals(await stringify(data, columns), output); }, }, ); await t.step( { name: "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); }, }, ); },});