-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rendering_csv.gleam
58 lines (54 loc) · 1.41 KB
/
rendering_csv.gleam
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
//// # Rendering CSV
////
//// The gsv package can be used to parse and render CSV on all targets.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/gsv
import gleam/dict
import gleeunit/should
import gsv
pub fn main_test() {
// If you have your data as a list of dicts then you can use the
// `from_dicts` function to create a CSV.
// The headers will be taken automatically from the keys of all of the dicts.
[
dict.from_list([#("name", "Lucy"), #("score", "100"), #("colour", "Pink")]),
dict.from_list([#("name", "Louis"), #("youtube", "@lpil"), #("score", "99")]),
]
|> gsv.from_dicts(",", gsv.Unix)
|> should.equal(
"colour,name,score,youtube
Pink,Lucy,100,
,Louis,99,@lpil
",
)
// If you want to have control over the order of the columns than you can use
// the `from_lists` function.
[
["name", "score", "youtube", "colour"],
["Lucy", "100", "", "Pink"],
["Louis", "99", "@lpil", ""],
]
|> gsv.from_lists(",", gsv.Unix)
|> should.equal(
"name,score,youtube,colour
Lucy,100,,Pink
Louis,99,@lpil,
",
)
// You can specify a different line ending and separator with either
// function. Here a `;` is used to render TSV.
[
["name", "score", "youtube", "colour"],
["Lucy", "100", "", "Pink"],
["Louis", "99", "@lpil", ""],
]
|> gsv.from_lists(";", gsv.Unix)
|> should.equal(
"name;score;youtube;colour
Lucy;100;;Pink
Louis;99;@lpil;
",
)
}