-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_streams.js
47 lines (40 loc) · 1.18 KB
/
8_streams.js
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
(
function() {
/*
Stream is sequential method that loads huge file in small
chunks so that processing of the file becomes easy and fast.
Simple implementation of stream by FFF (so that you can understand):
*/
const stupidNumberStream = {
each: (callback) => {
setTimeout(() => callback(1), 1000);
setTimeout(() => callback(2), 2000);
setTimeout(() => callback(3), 3000);
}
}
/*
Real example of streams
*/
const fs = require('fs')
const highland = require('highland')
highland(fs.createReadStream('customers.csv', 'utf-8'))
.split()
.map(line => line.split(','))
.map(parts => {
return({
name: parts[0],
numPurchases: parts[1]
})
})
.filter(
obj => obj.name !== ""
)
.filter(
customer => customer.numPurchases > 2
)
.map (
customer => customer.name
)
.each(x => console.log('each: ', x))
}
)();