-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtestableFiles.mjs
More file actions
32 lines (29 loc) · 1 KB
/
Copy pathtestableFiles.mjs
File metadata and controls
32 lines (29 loc) · 1 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
import { readFile } from "node:fs/promises";
import { parse } from "csv-parse/sync";
// To test file access, the test would need to write a file. That makes each
// test more complicated than it needs to be: there's the unnecessary indirection
// of having to write a file to pass the data to the function under test.
//
// Here we have decoupled the file reading and CSV parsing, so we can easily test
// the CSV parsing with multiple parameters. parsePeopleCsv also no longer needs
// to be asynchronous, which makes calling it easier.
export async function readUtf8File(path) {
return await readFile(path, { encoding: "utf8" });
}
export function parsePeopleCsv(csvData) {
const records = parse(csvData, {
skip_empty_lines: true,
trim: true,
});
return records.map(([firstName, lastName, age, gender]) => {
const person = {
firstName,
lastName,
gender: gender.charAt(0).toLowerCase(),
};
if (age !== "") {
person.age = parseInt(age);
}
return person;
});
}