常用函数式编程库

Rxjs

事件流

1
2
3
4
5
6
7
8
9
import { range } from "rxjs";
import { map, filter } from "rxjs/operators";

range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x));
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
import { Subject, from } from "rxjs";
import { debounceTime, distinctUntilChanged, switchMap } from "rxjs/operators";

const searchItems = new Subject();

searchItems
.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap((val) => from(getSuggestList(val)))
)
.subscribe((x) => console.log(x));

const input = document.getElementById("input");

input.oninput = search;

function search(val) {
searchItems.next(val.target.value);
}

function getSuggestList(val) {
return new Promise((resolve) => {
console.log(val);
setTimeout(() => {
resolve([
{ id: 1, name: "zhangsan" },
{ id: 2, name: "lisi" },
]);
});
});
}


lodash