2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-06-28 06:07:36 +00:00
|
|
|
import { CsvParseStream } from "./parse_stream.ts";
|
|
|
|
import type { CsvParseStreamOptions } from "./parse_stream.ts";
|
2024-08-01 10:48:14 +00:00
|
|
|
import { assert, assertEquals, assertRejects } from "@std/assert";
|
2024-04-29 02:57:30 +00:00
|
|
|
import type { AssertTrue, IsExact } from "@std/testing/types";
|
|
|
|
import { fromFileUrl, join } from "@std/path";
|
|
|
|
import { delay } from "@std/async/delay";
|
2022-04-06 04:46:45 +00:00
|
|
|
|
2023-03-13 05:56:25 +00:00
|
|
|
const testdataDir = join(fromFileUrl(import.meta.url), "../testdata");
|
2022-06-22 13:21:33 +00:00
|
|
|
const encoder = new TextEncoder();
|
2022-04-06 04:46:45 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2023-12-28 04:40:00 +00:00
|
|
|
name: "CsvParseStream should work with Deno.FsFile's readable",
|
2022-04-06 04:46:45 +00:00
|
|
|
permissions: {
|
|
|
|
read: [testdataDir],
|
|
|
|
},
|
|
|
|
fn: async () => {
|
|
|
|
const file = await Deno.open(join(testdataDir, "simple.csv"));
|
|
|
|
const readable = file.readable
|
|
|
|
.pipeThrough(new TextDecoderStream())
|
2023-04-05 05:15:23 +00:00
|
|
|
.pipeThrough(new CsvParseStream());
|
2023-11-10 19:00:28 +00:00
|
|
|
const records = await Array.fromAsync(readable);
|
2022-04-06 04:46:45 +00:00
|
|
|
assertEquals(records, [
|
|
|
|
["id", "name"],
|
|
|
|
["1", "foobar"],
|
|
|
|
["2", "barbaz"],
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-28 04:40:00 +00:00
|
|
|
name: "CsvParseStream throws at invalid csv line",
|
2022-04-06 04:46:45 +00:00
|
|
|
fn: async () => {
|
2023-07-17 06:08:20 +00:00
|
|
|
const readable = ReadableStream.from([
|
2022-04-06 04:46:45 +00:00
|
|
|
encoder.encode("id,name\n"),
|
|
|
|
encoder.encode("\n"),
|
|
|
|
encoder.encode("1,foo\n"),
|
|
|
|
encoder.encode('2,"baz\n'),
|
|
|
|
]).pipeThrough(new TextDecoderStream()).pipeThrough(
|
2023-04-05 05:15:23 +00:00
|
|
|
new CsvParseStream(),
|
2022-04-06 04:46:45 +00:00
|
|
|
);
|
|
|
|
const reader = readable.getReader();
|
|
|
|
assertEquals(await reader.read(), { done: false, value: ["id", "name"] });
|
|
|
|
assertEquals(await reader.read(), { done: false, value: ["1", "foo"] });
|
2024-07-16 07:35:40 +00:00
|
|
|
await assertRejects(
|
|
|
|
() => reader.read(),
|
|
|
|
SyntaxError,
|
2024-08-26 05:11:55 +00:00
|
|
|
`Syntax error on line 4; parse error on line 5, column 1: extraneous or missing " in quoted-field`,
|
2024-07-16 07:35:40 +00:00
|
|
|
);
|
2022-06-22 13:21:33 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-28 04:40:00 +00:00
|
|
|
name: "CsvParseStream handles various inputs",
|
2022-06-22 13:21:33 +00:00
|
|
|
permissions: "none",
|
|
|
|
fn: async (t) => {
|
|
|
|
// These test cases were originally ported from Go:
|
|
|
|
// https://github.com/golang/go/blob/go1.12.5/src/encoding/csv/
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved. BSD license.
|
|
|
|
// https://github.com/golang/go/blob/master/LICENSE
|
|
|
|
const testCases = [
|
|
|
|
{
|
|
|
|
name: "CRLF",
|
|
|
|
input: "a,b\r\nc,d\r\n",
|
|
|
|
output: [["a", "b"], ["c", "d"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BareCR",
|
|
|
|
input: "a,b\rc,d\r\n",
|
|
|
|
output: [["a", "b\rc", "d"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NoEOLTest",
|
|
|
|
input: "a,b,c",
|
|
|
|
output: [["a", "b", "c"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Semicolon",
|
|
|
|
input: "a;b;c\n",
|
|
|
|
output: [["a", "b", "c"]],
|
|
|
|
separator: ";",
|
|
|
|
},
|
2024-06-27 03:48:19 +00:00
|
|
|
{
|
|
|
|
name: "Separator is undefined",
|
2024-09-03 08:57:27 +00:00
|
|
|
input: "a,b,c\n",
|
|
|
|
output: [["a", "b", "c"]],
|
2024-06-27 03:48:19 +00:00
|
|
|
separator: undefined,
|
|
|
|
},
|
2022-06-22 13:21:33 +00:00
|
|
|
{
|
|
|
|
name: "MultiLine",
|
|
|
|
input: `"two
|
|
|
|
line","one line","three
|
|
|
|
line
|
|
|
|
field"`,
|
|
|
|
output: [["two\nline", "one line", "three\nline\nfield"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BlankLine",
|
|
|
|
input: "a,b,c\n\nd,e,f\n\n",
|
|
|
|
output: [
|
|
|
|
["a", "b", "c"],
|
|
|
|
["d", "e", "f"],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "LeadingSpace",
|
|
|
|
input: " a, b, c\n",
|
|
|
|
output: [[" a", " b", " c"]],
|
|
|
|
},
|
2024-06-27 03:48:19 +00:00
|
|
|
{
|
|
|
|
name: "trimLeadingSpace = true",
|
|
|
|
input: " a, b, c\n",
|
|
|
|
output: [["a", "b", "c"]],
|
|
|
|
trimLeadingSpace: true,
|
|
|
|
},
|
2022-06-22 13:21:33 +00:00
|
|
|
{
|
|
|
|
name: "Comment",
|
|
|
|
input: "#1,2,3\na,b,c\n#comment",
|
|
|
|
output: [["a", "b", "c"]],
|
|
|
|
comment: "#",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NoComment",
|
|
|
|
input: "#1,2,3\na,b,c",
|
|
|
|
output: [
|
|
|
|
["#1", "2", "3"],
|
|
|
|
["a", "b", "c"],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2024-08-01 10:48:14 +00:00
|
|
|
name: "fieldsPerRecord - variable number of fields is allowed",
|
2022-06-22 13:21:33 +00:00
|
|
|
input: "a,b,c\nd,e",
|
|
|
|
output: [
|
|
|
|
["a", "b", "c"],
|
|
|
|
["d", "e"],
|
|
|
|
],
|
|
|
|
},
|
2024-08-01 10:48:14 +00:00
|
|
|
{
|
|
|
|
name: "fieldsPerRecord = -42 - variable number of fields is allowed",
|
|
|
|
input: "a,b,c\nd,e",
|
|
|
|
output: [
|
|
|
|
["a", "b", "c"],
|
|
|
|
["d", "e"],
|
|
|
|
],
|
|
|
|
fieldsPerRecord: -42,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name:
|
|
|
|
"fieldsPerRecord = 0 - the number of fields is inferred from the first row",
|
|
|
|
input: "a,b,c\nd,e,f",
|
|
|
|
output: [
|
|
|
|
["a", "b", "c"],
|
|
|
|
["d", "e", "f"],
|
|
|
|
],
|
|
|
|
fieldsPerRecord: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name:
|
|
|
|
"fieldsPerRecord = 0 - inferred number of fields does not match subsequent rows",
|
|
|
|
input: "a,b,c\nd,e",
|
|
|
|
fieldsPerRecord: 0,
|
|
|
|
error: {
|
|
|
|
klass: SyntaxError,
|
2024-08-26 05:11:55 +00:00
|
|
|
msg: "Syntax error on line 2: expected 3 fields but got 2",
|
2024-08-01 10:48:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name:
|
|
|
|
"fieldsPerRecord = 3 - SyntaxError is thrown when the number of fields is not 2",
|
|
|
|
input: "a,b,c\nd,e",
|
|
|
|
fieldsPerRecord: 3,
|
|
|
|
error: {
|
|
|
|
klass: SyntaxError,
|
2024-08-26 05:11:55 +00:00
|
|
|
msg: "Syntax error on line 2: expected 3 fields but got 2",
|
2024-08-01 10:48:14 +00:00
|
|
|
},
|
|
|
|
},
|
2022-06-22 13:21:33 +00:00
|
|
|
{
|
|
|
|
name: "TrailingCommaEOF",
|
|
|
|
input: "a,b,c,",
|
|
|
|
output: [["a", "b", "c", ""]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TrailingCommaEOL",
|
|
|
|
input: "a,b,c,\n",
|
|
|
|
output: [["a", "b", "c", ""]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NotTrailingComma3",
|
|
|
|
input: "a,b,c, \n",
|
|
|
|
output: [["a", "b", "c", " "]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CommaFieldTest",
|
|
|
|
input: `x,y,z,w
|
|
|
|
x,y,z,
|
|
|
|
x,y,,
|
|
|
|
x,,,
|
|
|
|
,,,
|
|
|
|
"x","y","z","w"
|
|
|
|
"x","y","z",""
|
|
|
|
"x","y","",""
|
|
|
|
"x","","",""
|
|
|
|
"","","",""
|
|
|
|
`,
|
|
|
|
output: [
|
|
|
|
["x", "y", "z", "w"],
|
|
|
|
["x", "y", "z", ""],
|
|
|
|
["x", "y", "", ""],
|
|
|
|
["x", "", "", ""],
|
|
|
|
["", "", "", ""],
|
|
|
|
["x", "y", "z", "w"],
|
|
|
|
["x", "y", "z", ""],
|
|
|
|
["x", "y", "", ""],
|
|
|
|
["x", "", "", ""],
|
|
|
|
["", "", "", ""],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CRLFInQuotedField", // Issue 21201
|
|
|
|
input: 'A,"Hello\r\nHi",B\r\n',
|
|
|
|
output: [["A", "Hello\nHi", "B"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BinaryBlobField", // Issue 19410
|
|
|
|
input: "x09\x41\xb4\x1c,aktau",
|
|
|
|
output: [["x09A\xb4\x1c", "aktau"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "TrailingCR",
|
|
|
|
input: "field1,field2\r",
|
|
|
|
output: [["field1", "field2"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "QuotedTrailingCR",
|
|
|
|
input: '"field"\r',
|
|
|
|
output: [["field"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FieldCR",
|
|
|
|
input: "field\rfield\r",
|
|
|
|
output: [["field\rfield"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FieldCRCR",
|
|
|
|
input: "field\r\rfield\r\r",
|
|
|
|
output: [["field\r\rfield\r"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FieldCRCRLF",
|
|
|
|
input: "field\r\r\nfield\r\r\n",
|
|
|
|
output: [["field\r"], ["field\r"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FieldCRCRLFCR",
|
|
|
|
input: "field\r\r\n\rfield\r\r\n\r",
|
|
|
|
output: [["field\r"], ["\rfield\r"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "MultiFieldCRCRLFCRCR",
|
|
|
|
input: "field1,field2\r\r\n\r\rfield1,field2\r\r\n\r\r,",
|
|
|
|
output: [
|
|
|
|
["field1", "field2\r"],
|
|
|
|
["\r\rfield1", "field2\r"],
|
|
|
|
["\r\r", ""],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NonASCIICommaAndCommentWithQuotes",
|
|
|
|
input: 'a€" b,"€ c\nλ comment\n',
|
|
|
|
output: [["a", " b,", " c"]],
|
|
|
|
separator: "€",
|
|
|
|
comment: "λ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// λ and θ start with the same byte.
|
|
|
|
// This tests that the parser doesn't confuse such characters.
|
|
|
|
name: "NonASCIICommaConfusion",
|
|
|
|
input: '"abθcd"λefθgh',
|
|
|
|
output: [["abθcd", "efθgh"]],
|
|
|
|
separator: "λ",
|
|
|
|
comment: "€",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NonASCIICommentConfusion",
|
|
|
|
input: "λ\nλ\nθ\nλ\n",
|
|
|
|
output: [["λ"], ["λ"], ["λ"]],
|
|
|
|
comment: "θ",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "QuotedFieldMultipleLF",
|
|
|
|
input: '"\n\n\n\n"',
|
|
|
|
output: [["\n\n\n\n"]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "MultipleCRLF",
|
|
|
|
input: "\r\n\r\n\r\n\r\n",
|
|
|
|
output: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DoubleQuoteWithTrailingCRLF",
|
|
|
|
input: '"foo""bar"\r\n',
|
|
|
|
output: [[`foo"bar`]],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "EvenQuotes",
|
|
|
|
input: `""""""""`,
|
|
|
|
output: [[`"""`]],
|
|
|
|
},
|
2023-02-17 07:57:34 +00:00
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
input: "a,b,c",
|
|
|
|
output: [["a", "b", "c"]],
|
|
|
|
skipFirstRow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiline",
|
|
|
|
input: "a,b,c\ne,f,g\n",
|
|
|
|
output: [
|
|
|
|
["a", "b", "c"],
|
|
|
|
["e", "f", "g"],
|
|
|
|
],
|
|
|
|
skipFirstRow: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "header mapping boolean",
|
|
|
|
input: "a,b,c\ne,f,g\n",
|
|
|
|
output: [{ a: "e", b: "f", c: "g" }],
|
|
|
|
skipFirstRow: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "header mapping array",
|
|
|
|
input: "a,b,c\ne,f,g\n",
|
|
|
|
output: [
|
|
|
|
{ this: "a", is: "b", sparta: "c" },
|
|
|
|
{ this: "e", is: "f", sparta: "g" },
|
|
|
|
],
|
|
|
|
columns: ["this", "is", "sparta"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "provides both opts.skipFirstRow and opts.columns",
|
|
|
|
input: "a,b,1\nc,d,2\ne,f,3",
|
|
|
|
output: [
|
|
|
|
{ foo: "c", bar: "d", baz: "2" },
|
|
|
|
{ foo: "e", bar: "f", baz: "3" },
|
|
|
|
],
|
|
|
|
skipFirstRow: true,
|
|
|
|
columns: ["foo", "bar", "baz"],
|
|
|
|
},
|
|
|
|
{
|
2024-08-02 04:24:15 +00:00
|
|
|
name: "mismatching number of headers and fields 1",
|
2023-02-17 07:57:34 +00:00
|
|
|
input: "a,b,c\nd,e",
|
|
|
|
skipFirstRow: true,
|
|
|
|
columns: ["foo", "bar", "baz"],
|
2024-08-01 10:48:14 +00:00
|
|
|
error: {
|
|
|
|
klass: Error,
|
2024-08-26 05:11:55 +00:00
|
|
|
msg:
|
|
|
|
"Syntax error on line 2: The record has 2 fields, but the header has 3 fields",
|
2024-08-02 04:24:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "mismatching number of headers and fields 2",
|
|
|
|
input: "a,b,c\nd,e,,g",
|
|
|
|
skipFirstRow: true,
|
|
|
|
columns: ["foo", "bar", "baz"],
|
|
|
|
error: {
|
|
|
|
klass: Error,
|
2024-08-26 05:11:55 +00:00
|
|
|
msg:
|
|
|
|
"Syntax error on line 2: The record has 4 fields, but the header has 3 fields",
|
2024-08-01 10:48:14 +00:00
|
|
|
},
|
2023-02-17 07:57:34 +00:00
|
|
|
},
|
2024-06-27 03:48:19 +00:00
|
|
|
{
|
|
|
|
name: "bad quote in bare field",
|
|
|
|
input: `a "word",1,2,3`,
|
2024-08-01 10:48:14 +00:00
|
|
|
error: {
|
|
|
|
klass: SyntaxError,
|
|
|
|
msg:
|
2024-08-26 05:11:55 +00:00
|
|
|
'Syntax error on line 1; parse error on line 1, column 3: bare " in non-quoted-field',
|
2024-08-01 10:48:14 +00:00
|
|
|
},
|
2024-06-27 03:48:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad quote in quoted field",
|
|
|
|
input: `"wo"rd",1,2,3`,
|
2024-08-01 10:48:14 +00:00
|
|
|
error: {
|
|
|
|
klass: SyntaxError,
|
|
|
|
msg:
|
2024-08-26 05:11:55 +00:00
|
|
|
'Syntax error on line 1; parse error on line 1, column 4: extraneous or missing " in quoted-field',
|
2024-08-01 15:07:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad quote at line 1 in quoted field with newline",
|
|
|
|
input: `"w\n\no"rd",1,2,3`,
|
|
|
|
error: {
|
|
|
|
klass: SyntaxError,
|
|
|
|
msg:
|
2024-08-26 05:11:55 +00:00
|
|
|
'Syntax error on line 1; parse error on line 3, column 2: extraneous or missing " in quoted-field',
|
2024-08-01 15:07:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad quote at line 2 in quoted field with newline",
|
|
|
|
input: `a,b,c,d\n"w\n\no"rd",1,2,3`,
|
|
|
|
error: {
|
|
|
|
klass: SyntaxError,
|
|
|
|
msg:
|
2024-08-26 05:11:55 +00:00
|
|
|
'Syntax error on line 2; parse error on line 4, column 2: extraneous or missing " in quoted-field',
|
2024-08-01 10:48:14 +00:00
|
|
|
},
|
2024-06-27 03:48:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "lazy quote",
|
|
|
|
input: `a "word","1"2",a","b`,
|
|
|
|
output: [[`a "word"`, `1"2`, `a"`, `b`]],
|
|
|
|
lazyQuotes: true,
|
|
|
|
},
|
2022-06-22 13:21:33 +00:00
|
|
|
];
|
|
|
|
for (const testCase of testCases) {
|
|
|
|
await t.step(testCase.name, async () => {
|
2023-04-05 05:15:23 +00:00
|
|
|
const options: CsvParseStreamOptions = {};
|
2024-06-27 03:48:19 +00:00
|
|
|
if ("separator" in testCase) {
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
// @ts-expect-error: explicitly giving undefined
|
2022-06-22 13:21:33 +00:00
|
|
|
options.separator = testCase.separator;
|
|
|
|
}
|
2024-06-27 03:48:19 +00:00
|
|
|
if ("comment" in testCase) {
|
2022-06-22 13:21:33 +00:00
|
|
|
options.comment = testCase.comment;
|
|
|
|
}
|
2024-06-27 03:48:19 +00:00
|
|
|
if ("trimLeadingSpace" in testCase) {
|
|
|
|
options.trimLeadingSpace = testCase.trimLeadingSpace;
|
|
|
|
}
|
|
|
|
if ("lazyQuotes" in testCase) {
|
|
|
|
options.lazyQuotes = testCase.lazyQuotes;
|
|
|
|
}
|
2024-08-01 10:48:14 +00:00
|
|
|
if ("fieldsPerRecord" in testCase) {
|
|
|
|
options.fieldsPerRecord = testCase.fieldsPerRecord;
|
|
|
|
}
|
|
|
|
if ("skipFirstRow" in testCase) {
|
|
|
|
options.skipFirstRow = testCase.skipFirstRow;
|
|
|
|
}
|
|
|
|
if ("columns" in testCase) {
|
|
|
|
options.columns = testCase.columns;
|
|
|
|
}
|
2024-06-27 03:48:19 +00:00
|
|
|
|
2024-08-06 09:21:57 +00:00
|
|
|
const readable = ReadableStream.from([testCase.input])
|
2023-04-05 05:15:23 +00:00
|
|
|
.pipeThrough(new CsvParseStream(options));
|
2023-02-17 07:57:34 +00:00
|
|
|
|
|
|
|
if (testCase.output) {
|
2023-11-10 19:00:28 +00:00
|
|
|
const actual = await Array.fromAsync(readable);
|
2023-02-17 07:57:34 +00:00
|
|
|
assertEquals(actual, testCase.output);
|
|
|
|
} else {
|
2024-08-01 10:48:14 +00:00
|
|
|
assert(testCase.error);
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
for await (const _ of readable);
|
|
|
|
},
|
|
|
|
testCase.error.klass,
|
|
|
|
testCase.error.msg,
|
|
|
|
);
|
2022-06-22 13:21:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-04-06 04:46:45 +00:00
|
|
|
},
|
|
|
|
});
|
2022-06-22 13:21:33 +00:00
|
|
|
|
2023-02-03 03:59:31 +00:00
|
|
|
Deno.test({
|
2023-04-05 05:15:23 +00:00
|
|
|
name:
|
2023-12-28 04:40:00 +00:00
|
|
|
"CsvParseStream.cancel() does not leak file when called in the middle of iteration",
|
2023-02-03 03:59:31 +00:00
|
|
|
permissions: { read: [testdataDir] },
|
|
|
|
fn: async () => {
|
|
|
|
const file = await Deno.open(join(testdataDir, "large.csv"));
|
2023-11-17 09:05:06 +00:00
|
|
|
const readable = file.readable
|
|
|
|
.pipeThrough(new TextDecoderStream())
|
2023-04-05 05:15:23 +00:00
|
|
|
.pipeThrough(new CsvParseStream());
|
2023-02-03 03:59:31 +00:00
|
|
|
for await (const _record of readable) {
|
|
|
|
break;
|
|
|
|
}
|
2023-10-23 10:00:05 +00:00
|
|
|
// FIXME(kt3k): Remove this delay.
|
|
|
|
await delay(100);
|
2023-02-03 03:59:31 +00:00
|
|
|
},
|
|
|
|
});
|
2023-02-17 07:57:34 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2023-12-28 04:40:00 +00:00
|
|
|
name: "CsvParseStream is correctly typed",
|
2023-02-17 07:57:34 +00:00
|
|
|
fn() {
|
2023-04-04 04:53:16 +00:00
|
|
|
// If no option is passed, defaults to ReadableStream<string[]>.
|
2023-02-17 07:57:34 +00:00
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream();
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<IsExact<typeof readable, ReadableStream<string[]>>>;
|
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream(undefined);
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<IsExact<typeof readable, ReadableStream<string[]>>>;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// `skipFirstRow` may be `true` or `false`.
|
2023-06-21 16:27:37 +00:00
|
|
|
// `columns` may be `undefined` or `string[]`.
|
2023-04-04 04:53:16 +00:00
|
|
|
// If you don't know exactly what the value of the option is,
|
2024-08-02 05:07:10 +00:00
|
|
|
// the return type is ReadableStream<string[] | Record<string, string>>
|
2023-04-05 05:15:23 +00:00
|
|
|
const options: CsvParseStreamOptions = {};
|
|
|
|
const { readable } = new CsvParseStream(options);
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<
|
|
|
|
IsExact<
|
|
|
|
typeof readable,
|
2024-08-02 05:07:10 +00:00
|
|
|
ReadableStream<string[] | Record<string, string>>
|
2023-04-04 04:53:16 +00:00
|
|
|
>
|
|
|
|
>;
|
2023-02-17 07:57:34 +00:00
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream({});
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<IsExact<typeof readable, ReadableStream<string[]>>>;
|
2023-02-17 07:57:34 +00:00
|
|
|
}
|
2023-04-04 04:53:16 +00:00
|
|
|
|
|
|
|
// skipFirstRow option
|
2023-02-17 07:57:34 +00:00
|
|
|
{
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const { readable } = new CsvParseStream({});
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<IsExact<typeof readable, ReadableStream<string[]>>>;
|
2023-02-17 07:57:34 +00:00
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream({ skipFirstRow: false });
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<IsExact<typeof readable, ReadableStream<string[]>>>;
|
2023-02-17 07:57:34 +00:00
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream({ skipFirstRow: true });
|
2023-02-17 07:57:34 +00:00
|
|
|
type _ = AssertTrue<
|
2023-04-04 04:53:16 +00:00
|
|
|
IsExact<
|
|
|
|
typeof readable,
|
2024-08-02 05:07:10 +00:00
|
|
|
ReadableStream<Record<string, string>>
|
2023-04-04 04:53:16 +00:00
|
|
|
>
|
2023-02-17 07:57:34 +00:00
|
|
|
>;
|
|
|
|
}
|
2023-04-04 04:53:16 +00:00
|
|
|
|
|
|
|
// columns option
|
2023-02-17 07:57:34 +00:00
|
|
|
{
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const { readable } = new CsvParseStream({});
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<IsExact<typeof readable, ReadableStream<string[]>>>;
|
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream({ columns: ["aaa", "bbb"] });
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<
|
|
|
|
IsExact<typeof readable, ReadableStream<Record<"aaa" | "bbb", string>>>
|
|
|
|
>;
|
2023-02-17 07:57:34 +00:00
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream({ columns: ["aaa"] as string[] });
|
2023-02-17 07:57:34 +00:00
|
|
|
type _ = AssertTrue<
|
2023-04-04 04:53:16 +00:00
|
|
|
IsExact<
|
|
|
|
typeof readable,
|
2024-08-02 05:07:10 +00:00
|
|
|
ReadableStream<Record<string, string>>
|
2023-04-04 04:53:16 +00:00
|
|
|
>
|
2023-02-17 07:57:34 +00:00
|
|
|
>;
|
|
|
|
}
|
2023-04-04 04:53:16 +00:00
|
|
|
|
|
|
|
// skipFirstRow option + columns option
|
2023-02-17 07:57:34 +00:00
|
|
|
{
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const { readable } = new CsvParseStream({ skipFirstRow: false });
|
2023-04-04 04:53:16 +00:00
|
|
|
type _ = AssertTrue<IsExact<typeof readable, ReadableStream<string[]>>>;
|
2023-02-17 07:57:34 +00:00
|
|
|
}
|
|
|
|
{
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const { readable } = new CsvParseStream({ skipFirstRow: true });
|
2023-02-17 07:57:34 +00:00
|
|
|
type _ = AssertTrue<
|
2023-04-04 04:53:16 +00:00
|
|
|
IsExact<
|
|
|
|
typeof readable,
|
2024-08-02 05:07:10 +00:00
|
|
|
ReadableStream<Record<string, string>>
|
2023-04-04 04:53:16 +00:00
|
|
|
>
|
2023-02-17 07:57:34 +00:00
|
|
|
>;
|
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream({
|
2023-02-17 07:57:34 +00:00
|
|
|
skipFirstRow: false,
|
|
|
|
columns: ["aaa"],
|
|
|
|
});
|
|
|
|
type _ = AssertTrue<
|
2023-04-04 04:53:16 +00:00
|
|
|
IsExact<typeof readable, ReadableStream<Record<"aaa", string>>>
|
2023-02-17 07:57:34 +00:00
|
|
|
>;
|
|
|
|
}
|
|
|
|
{
|
2023-04-05 05:15:23 +00:00
|
|
|
const { readable } = new CsvParseStream({
|
2023-02-17 07:57:34 +00:00
|
|
|
skipFirstRow: true,
|
|
|
|
columns: ["aaa"],
|
|
|
|
});
|
|
|
|
type _ = AssertTrue<
|
2023-04-04 04:53:16 +00:00
|
|
|
IsExact<typeof readable, ReadableStream<Record<"aaa", string>>>
|
2023-02-17 07:57:34 +00:00
|
|
|
>;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|