Skip to content

Commit

Permalink
feat: ✨ 添加toOption操作符
Browse files Browse the repository at this point in the history
  • Loading branch information
hackers267 committed Aug 8, 2022
1 parent c890875 commit ae80b67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/operator/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { of, throwError } from "rxjs";
import { pickData, toDesc, toPage, toVirtualPage } from "./index";
import { pickData, toDesc, toOption, toPage, toVirtualPage } from "./index";

describe("pickData", function () {
it("should be right", function () {
Expand Down Expand Up @@ -65,3 +65,17 @@ describe("toVirtualPage", function () {
});
});
});

describe("toOption", function () {
it("should be right", function () {
const strings = ["a", "b"];
of(strings)
.pipe(toOption())
.subscribe((x) => {
expect(x).toStrictEqual([
{ value: "a", label: "a" },
{ value: "b", label: "b" },
]);
});
});
});
11 changes: 10 additions & 1 deletion src/operator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { catchError, map, of, pipe } from "rxjs";
import { catchError, from, map, mergeMap, of, pipe, toArray } from "rxjs";

type Page<T = any> = {
list: T[];
Expand Down Expand Up @@ -36,3 +36,12 @@ export function toVirtualPage<T = any>() {
catchError(() => of({ data: [], success: true, total: 0 }))
);
}

export function toOption() {
return pipe(
mergeMap((x: string[]) => from(x).pipe(map(string2Option), toArray()))
);
function string2Option(x: string) {
return { value: x, label: x };
}
}

0 comments on commit ae80b67

Please sign in to comment.