-
Notifications
You must be signed in to change notification settings - Fork 5
/
deno-csv-readCSV.ts
40 lines (36 loc) · 923 Bytes
/
deno-csv-readCSV.ts
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
import { readCSV } from "../mod.ts";
import { Sha256 } from "../dev_deps.ts";
const file = await Deno.open(Deno.args[0]);
const calculateHash = !!Deno.env.get("CALCULATE_HASH");
const calculateRowHash = !!Deno.env.get("CALCULATE_ROW_HASH");
const hash = new Sha256();
let rowHash = new Sha256();
let lines = 0;
const start = performance.now();
for await (
const row of readCSV(file, {
lineSeparator: "\r\n",
})
) {
if (calculateRowHash) {
rowHash = new Sha256();
}
for await (const cell of row) {
if (calculateHash) {
hash.update(cell);
}
if (calculateRowHash) {
rowHash.update(cell);
}
}
if (calculateRowHash) {
console.log(lines, rowHash.hex(), row);
}
lines++;
}
const diff = performance.now() - start;
if (calculateHash) {
console.log(`Result hash: ${hash.hex()}`);
} else {
console.log(`Read ${lines} lines for ${(diff / 1000).toFixed(3)} seconds`);
}