本文整理匯總了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);
}