-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.js
52 lines (45 loc) · 1 KB
/
pipeline.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
48
49
50
51
52
let Pipeline = {
data: [],
ops: [],
get: function () {
let res = []
for (let i = 0; i < this.data.length; i++) {
let val = this.data[i]
let passesFilters = true
for (let j = 0; passesFilters && j < this.ops.length; j++) {
let op = this.ops[j]
switch (op.type) {
case 'map':
val = op.func(val)
break;
case 'filter':
passesFilters = op.func(val)
break;
default:
throw new Exception('Illegal op type')
}
}
if (passesFilters) {
res.push(val)
}
}
return res
},
make_new: function (data) {
return Object.create(Pipeline, { data: { value: data }})
},
add_op: function (type, func) {
this.ops.push({
type: type,
func: func
})
},
map: function (func) {
this.add_op('map', func)
return this
},
filter: function (func) {
this.add_op('filter', func)
return this
}
}