-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
83 lines (74 loc) · 1.97 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { Readable } = require("node:stream");
const TsvParser = require("./index");
const t = require("tap");
t.test("TsvParser test", (t) => {
t.plan(1);
let result = [];
const sampleInput = "name\tage\tcity\nJohn\t30\tNew York\nJane\t25\tLondon";
const expectedOutput = [
{ name: "John", age: "30", city: "New York" },
{ name: "Jane", age: "25", city: "London" },
];
const readable = new Readable({
read() {
this.push(sampleInput);
this.push(null);
},
});
readable
.pipe(TsvParser())
.on("data", (chunk) => {
result.push(chunk);
})
.on("end", () => {
t.same(result, expectedOutput, "should parse TSV correctly");
});
});
t.test("Custom header", (t) => {
t.plan(1);
let result = [];
const sampleInput = "name\tage\tcity\nJohn\t30\tNew York\nJane\t25\tLondon";
const expectedOutput = [
{ A: "name", B: "age", C: "city" },
{ A: "John", B: "30", C: "New York" },
{ A: "Jane", B: "25", C: "London" },
];
const readable = new Readable({
read() {
this.push(sampleInput);
this.push(null);
},
});
readable
.pipe(TsvParser({ headers: ["A", "B", "C"] }))
.on("data", (chunk) => {
result.push(chunk);
})
.on("end", () => {
t.same(result, expectedOutput, "should parse TSV correctly with custom header");
});
});
t.test("Headers false", (t) => {
t.plan(1);
let result = [];
const sampleInput = "name\tage\tcity\nJohn\t30\tNew York\nJane\t25\tLondon";
const expectedOutput = [
{ 0: "name", 1: "age", 2: "city" },
{ 0: "John", 1: "30", 2: "New York" },
{ 0: "Jane", 1: "25", 2: "London" },
];
const readable = new Readable({
read() {
this.push(sampleInput);
this.push(null);
},
});
readable
.pipe(TsvParser({ headers: false }))
.on("data", (chunk) => {
result.push(chunk);
})
.on("end", () => {
t.same(result, expectedOutput, "should parse TSV correctly with custom header");
});
});