本文整理汇总了TypeScript中rxjs/operators.partition函数的典型用法代码示例。如果您正苦于以下问题:TypeScript partition函数的具体用法?TypeScript partition怎么用?TypeScript partition使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了partition函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: partition2
partition2() {
const source = from([1, 2, 3, 4, 5, 6]);
// if greater than 3 throw
const example = source.pipe<{success: number, error: number},{success: number, error: number}>(
map(val => {
if (val > 3) {
// throw `${val} greater than 3!`;
}
return { success: val, error: null };
}),
catchError(val => of({ success: null, error: val }))
);
// split on success or error
const [success, error] = partition<{success: number, error: number}>(res => res.success !== null)(example);
/*
Output:
"Success! 1"
"Success! 2"
"Success! 3"
"Error! 4 greater than 3!"
*/
const subscribe = merge(
success.pipe(map(val => `Success! ${val.success}`)),
error.pipe(map(val => `Error! ${val.error}`))
).subscribe(val => console.log(val));
}
示例2: partition1
partition1() {
const source = from([1, 2, 3, 4, 5, 6]);
// first value is true, second false
const [evens, odds] = partition<number>(val => val % 2 === 0)(source);
/*
Output:
"Even: 2"
"Even: 4"
"Even: 6"
"Odd: 1"
"Odd: 3"
"Odd: 5"
*/
const subscribe = merge(
evens.pipe(map(val => `Even: ${val}`)),
odds.pipe(map(val => `Odd: ${val}`))
).subscribe(val => console.log(val));
}
示例3: fetchNodesNetwork
export function fetchNodesNetwork(): Observable<
RequestResponse<NodesNetworkResponse>
> {
const [success, error] = partition(
(response: RequestResponse<NodesNetworkResponse>) => response.code < 300
)(request<NodeNetworkResponse[]>("/net/v1/nodes").pipe(share()));
return merge(
success,
error.pipe(
tap((response: RequestResponse<NodesNetworkResponse>) => {
const responseMessage =
response.response && typeof response.response === "object"
? JSON.stringify(response.response)
: response.response;
throw new Error(
`Network Nodes API request failed: ${response.code} ${
response.message
}:${responseMessage}`
);
})
)
);
}
示例4: expect
const part: any = partitionZone.run(() => {
return observable1.pipe(partition((val: any) => {
expect(Zone.current.name).toEqual(partitionZone.name);
return val % 2 === 0;
}));
});
示例5: higherOrder
export function partition<T>(this: Observable<T>, predicate: (value: T, index: number) => boolean, thisArg?: any): [Observable<T>, Observable<T>] {
return higherOrder(predicate, thisArg)(this);
}