-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (32 loc) · 704 Bytes
/
index.js
File metadata and controls
38 lines (32 loc) · 704 Bytes
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
const { Readable } = require("stream");
const peaks = [
"Tallac",
"Ralston",
"Rubicon",
"Twin Peaks",
"Castle Peak",
"Rose",
"Freel Peak",
];
class StreamFromArray extends Readable {
constructor(array) {
super({ objectMode: true });
this.array = array;
this.index = 0;
}
_read() {
if (this.index <= this.array.length) {
const chunk = {
data: this.array[this.index],
index: this.index,
};
this.push(chunk);
this.index += 1;
} else {
this.push(null);
}
}
}
const peakStream = new StreamFromArray(peaks);
peakStream.on("data", (chunk) => console.log(chunk));
peakStream.on("end", () => console.log("done!"));