-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathapi.sync.ts
More file actions
122 lines (112 loc) · 3.41 KB
/
api.sync.ts
File metadata and controls
122 lines (112 loc) · 3.41 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import "should";
import dedent from "dedent";
import { parse } from "../lib/sync.js";
describe("API sync", function () {
describe("content", function () {
it("take a string and return records", function () {
const records = parse("field_1,field_2\nvalue 1,value 2");
records.should.eql([
["field_1", "field_2"],
["value 1", "value 2"],
]);
});
it("take a buffer and return records", function () {
const records = parse(Buffer.from("field_1,field_2\nvalue 1,value 2"));
records.should.eql([
["field_1", "field_2"],
["value 1", "value 2"],
]);
});
it("take a Uint8Array and return records", function () {
const records = parse(
new TextEncoder().encode("field_1,field_2\nvalue 1,value 2"),
);
records.should.eql([
["field_1", "field_2"],
["value 1", "value 2"],
]);
});
});
describe("options", function () {
it("`columns` option without generic", function () {
// Parse returns unknown[]
const records = parse("field_1,field_2\nvalue 1,value 2", {
columns: true,
});
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
});
it("`columns` option with generic", function () {
// Parse returns Record[]
interface Record {
field_1: string;
field_2: string;
}
const records: Record[] = parse<Record>(
"field_1,field_2\nvalue 1,value 2",
{
columns: true,
},
);
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
});
it("`columns` and `on_record` options with generic", function () {
// Parse returns Record[]
interface RecordOriginal {
field_a: string;
field_b: string;
}
interface Record {
field_1: string;
field_2: string;
}
const records: Record[] = parse<Record, RecordOriginal>(
"field_a,field_b\nvalue 1,value 2",
{
columns: true,
on_record: (record: RecordOriginal) => ({
field_1: record.field_a,
field_2: record.field_b,
}),
},
);
records.should.eql([{ field_1: "value 1", field_2: "value 2" }]);
});
it("`objname` option", function () {
// Not good, parse returns unknown[]
const records = parse("field_1,field_2\nname 1,value 1\nname 2,value 2", {
objname: "field_1",
columns: true,
});
records.should.eql({
"name 1": { field_1: "name 1", field_2: "value 1" },
"name 2": { field_1: "name 2", field_2: "value 2" },
});
});
it("`to_line` option", function () {
const records = parse("1\n2\n3\n4", { to_line: 2 });
records.should.eql([["1"], ["2"]]);
});
});
describe("errors", function () {
it("catch errors", function () {
try {
parse("A,B\nB\nC,K", { trim: true });
throw Error("Error not catched");
} catch (err) {
if (!err) throw Error("Invalid assessment");
(err as Error).message.should.eql(
"Invalid Record Length: expect 2, got 1 on line 2",
);
}
});
it("catch err in last line while flushing", function () {
(() => {
parse(dedent`
headerA, headerB
A2, B2
A1, B1, C2, D2
`);
}).should.throw("Invalid Record Length: expect 2, got 4 on line 3");
});
});
});