-
Notifications
You must be signed in to change notification settings - Fork 0
/
operatorTime.js
106 lines (96 loc) · 2.63 KB
/
operatorTime.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
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
106
const { Observable, from } = require('rxjs')
const createPipeOperable = (fn) => {
return (source) => {
return new Observable((subscribe) => {
source.subscribe(fn(source, subscribe))
});
}
}
const first = () => createPipeOperable((source, subscribe) =>
({
next(value) {
subscribe.next(value);
subscribe.complete();
}
})
);
const last = () => createPipeOperable((source, subscribe) => {
let last = null;
return ({
next(value) {
last = value;
},
complete() {
subscribe.next(last);
subscribe.complete();
}
})
});
const withDelay = (time, ...items) => {
return new Observable((subscribe) => {
(items || []).forEach((item, index) => {
setTimeout(() => {
subscribe.next(item);
console.info('time:', time * (index + 1));
if(items.length === (index + 1)) {
subscribe.complete();
}
}, time * (index + 1));
})
})
}
const firstObs = () => {
return (source) => {
return new Observable((subscribe) => {
source.subscribe({
next(value) {
subscribe.next(value);
subscribe.complete();
}
})
})
}
}
const lastObs = () => (source) => new Observable((subscribe) => {
let last = null;
source.subscribe({
next(value) {
last = value;
},
complete() {
subscribe.next(last);
subscribe.complete();
}
})
})
const hasContent = (filterValue) => {
return (source) => {
return new Observable((subscribe) => {
source.subscribe({
next(value) {
if(Array.isArray(value)) {
subscribe.next(
value.filter(item => item.endsWith(filterValue))
)
}else if (value.endsWith(filterValue)) {
subscribe.next(value)
}
},
erro(err) {
subscribe.error(err)
},
complete() {
subscribe.complete()
}
})
})
}
}
withDelay(200, 1,2,4,5,6).subscribe(console.info);
from([1,2,4,5,6])
.pipe(first())
.pipe(last())
.subscribe(console.info)
from(['Ichigo Kurosaki', 'Orihime Inoue', 'Aizen Sousuke', 'Grimmjow', 'Kenpachi Zaraki'])
.pipe(hasContent('Kurosaki'))
.subscribe(console.info)