Skip to content

Commit 6680dde

Browse files
committed
add stream code
1 parent 28b370c commit 6680dde

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

09-stream/06-my-readable.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
const { Readable } = require('stream');
44

5-
let c = 'a'.charCodeAt(0);
6-
75
class MyReadable extends Readable {
86
constructor() {
97
super();
108
}
119

1210
_read() {
13-
this.push(String.fromCharCode(c++));
14-
if (c>'z'.charCodeAt(0)) {
15-
this.push(null);
16-
}
11+
this.push(String.fromCharCode(this.#c++));
12+
if (this.#c>'z'.charCodeAt(0)) this.push(null);
1713
}
14+
15+
#c = 'a'.charCodeAt(0);
1816
}
1917

20-
let rs = new MyReadable();
18+
const rs = new MyReadable();
2119
rs.pipe(process.stdout);

09-stream/10-rgb.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env node
2+
3+
const { Readable } = require('stream');
4+
5+
class RGB extends Readable {
6+
constructor(num) {
7+
super();
8+
this.#num = num;
9+
}
10+
11+
_read() {
12+
this.#pos++ > this.#num ? this.push(null) : this.push(this.#rgb());
13+
}
14+
15+
#rgb() {
16+
let x = Math.round(Math.random()*100);
17+
return x <= 60 ? 'R' : ( x <= 75 ? 'G':'B');
18+
}
19+
20+
#num = 0;
21+
#pos = 0;
22+
}
23+
24+
const rs = new RGB(100);
25+
rs.pipe(process.stdout);

0 commit comments

Comments
 (0)