Hullo JS
Search…
|> distinct
import { distinct } from '@hullo/core'
Use this operator to filter only wanted messages where that is established by comparing last released and the next candidate.
1
import { Observable, distinct } from '@hullo/core';
2
​
3
new Observable(observer => {
4
observer.next(Math.round(Math.random() * 100));
5
})
6
.pipe(
7
// pick only those higher than the last by two
8
distinct((last, next) => last + 2 < next)
9
)
10
.subscribe({
11
next: v => { console.log(v, 'is an even number') }
12
});
Copied!
There are also two additional variants: distinctEqual, distinctStrictEqual where, respectively, regular and strict comparison is used.
1
import { Observable, distinctEqual } from '@hullo/core';
2
​
3
new Observable(observer => {
4
observer.next(2);
5
observer.next(2);
6
observer.next("2");
7
observer.next(3);
8
})
9
.pipe(distinctEqual)
10
.subscribe({ next: v => { console.log(v) } });
11
​
12
// outputs 2, 3
Copied!
...where...
1
import { Observable, distinctStrictEqual } from '@hullo/core';
2
​
3
new Observable(observer => {
4
observer.next(2);
5
observer.next(2);
6
observer.next("2");
7
observer.next(3);
8
})
9
.pipe(distinctStrictEqual)
10
.subscribe({ next: v => { console.log(v) } });
11
​
12
// outputs 2, "2", 3
Copied!
Copy link