-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
105 lines (89 loc) · 2.38 KB
/
index.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
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
const fileName = Bun.argv[2];
type AggregationsMap = Map<
string,
{ min: number; max: number; sum: number; count: number }
>;
const aggregations: AggregationsMap = new Map();
readLineByLine(
Bun.file(fileName).stream(),
(line) => {
const [stationName, temperatureStr] = line.split(';');
// use integers for computation to avoid loosing precision
const temperature = Math.floor(parseFloat(temperatureStr) * 10);
const existing = aggregations.get(stationName);
if (existing) {
existing.min = Math.min(existing.min, temperature);
existing.max = Math.max(existing.max, temperature);
existing.sum += temperature;
existing.count++;
} else {
aggregations.set(stationName, {
min: temperature,
max: temperature,
sum: temperature,
count: 1,
});
}
},
() => {
printCompiledResults(aggregations);
}
);
function printCompiledResults(aggregations: AggregationsMap) {
const sortedStations = Array.from(aggregations.keys()).sort();
let result =
'{' +
sortedStations
.map((station) => {
const data = aggregations.get(station)!;
return `${station}=${round(data.min / 10)}/${round(
data.sum / 10 / data.count
)}/${round(data.max / 10)}`;
})
.join(', ') +
'}';
console.log(result);
}
/**
* @example
* round(1.2345) // "1.2"
* round(1.55) // "1.6"
* round(1) // "1.0"
*
* @param {number} num
*
* @returns {string}
*/
function round(num: number): string {
const fixed = Math.round(10 * num) / 10;
return fixed.toFixed(1);
}
async function readLineByLine(
stream: ReadableStream<Uint8Array>,
cb: (line: string) => void,
done: () => void
) {
const nlChar = '\n'.charCodeAt(0);
const textDecoder = new TextDecoder();
// max line length is 128
let lineBuffer = new Uint8Array(128);
let lineBufferLen = 0;
for await (const chunk of stream) {
for (let i = 0; i < chunk.length; i++) {
if (chunk[i] === nlChar) {
const line = textDecoder.decode(lineBuffer.slice(0, lineBufferLen));
lineBufferLen = 0;
cb(line);
} else {
lineBuffer[lineBufferLen] = chunk[i];
lineBufferLen++;
}
}
}
// dont emit an empty string for files that end with a new line
if (lineBufferLen > 0) {
cb(textDecoder.decode(lineBuffer.slice(0, lineBufferLen)));
}
done();
return;
}