本文整理汇总了TypeScript中rxjs/operators.groupBy函数的典型用法代码示例。如果您正苦于以下问题:TypeScript groupBy函数的具体用法?TypeScript groupBy怎么用?TypeScript groupBy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了groupBy函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it('should enforce types of duration selector', () => {
const o = of(1, 2, 3).pipe(groupBy(value => value.toString(), undefined, value => 'foo')); // $ExpectError
const p = of(1, 2, 3).pipe(groupBy(value => value.toString(), undefined, (value: GroupedObservable<number, number>) => value)); // $ExpectError
const q = of(1, 2, 3).pipe(groupBy(value => value.toString(), undefined, (value: GroupedObservable<string, string>) => value)); // $ExpectError
const r = of(1, 2, 3).pipe(groupBy(value => value.toString(), value => Boolean(value), (value: GroupedObservable<string, string>) => value)); // $ExpectError
const s = of(1, 2, 3).pipe(groupBy(value => value.toString(), value => Boolean(value), (value: GroupedObservable<boolean, boolean>) => value)); // $ExpectError
});
示例2: groupBy
observable1 = groupByZone1.run(() => {
return observable1.pipe(
groupBy((person: any) => {
expect(Zone.current.name).toEqual(groupByZone1.name);
return person.age;
}),
// return as array of each group
flatMap((group: any) => {
return group.pipe(reduce((acc: any, curr: any) => [...acc, curr], []));
}));
});
示例3: from
public static doGroupRows<T>(data: T[], groupRowsBy) {
const grouped = [];
from(data).pipe(
groupBy((row) => row[groupRowsBy]),
flatMap((group) => group.pipe(
reduce((acc: T[], curr: T) => [...acc, curr], []),
)),
).subscribe((row) => grouped.push(row));
return grouped;
}
示例4: toActions
/**
* @internal
*/
toActions(): Observable<Action> {
return this.pipe(
groupBy(getSourceForInstance),
mergeMap(source$ =>
source$.pipe(
exhaustMap(resolveEffectSource),
map(output => {
verifyOutput(output, this.errorHandler);
return output.notification;
}),
filter(
(notification): notification is Notification<Action> =>
notification.kind === 'N'
),
dematerialize()
)
)
);
}
示例5: groupBy1
groupBy1() {
const people = [
{ name: 'Sue', age: 25 },
{ name: 'Joe', age: 30 },
{ name: 'Frank', age: 25 },
{ name: 'Sarah', age: 35 }
];
// emit each person
const source = from(people);
// group by age
const example = source.pipe(
groupBy(person => person.age),
// return each item in group as array
mergeMap(group => group.pipe(toArray()))
);
/*
output:
[{age: 25, name: "Sue"},{age: 25, name: "Frank"}]
[{age: 30, name: "Joe"}]
[{age: 35, name: "Sarah"}]
*/
const subscribe = example.subscribe(val => console.log(val));
}
示例6: executeCellEpic
export function executeCellEpic(
action$: ActionsObservable<ExecuteCell | ExecuteFocusedCell>,
state$: any
) {
return action$.pipe(
ofType(actions.EXECUTE_CELL, actions.EXECUTE_FOCUSED_CELL),
mergeMap((action: ExecuteCell | ExecuteFocusedCell) => {
if (action.type === actions.EXECUTE_FOCUSED_CELL) {
const contentRef = action.payload.contentRef;
const state = state$.value;
const model = selectors.model(state, { contentRef });
// If it's not a notebook, we shouldn't be here
if (!model || model.type !== "notebook") {
return empty();
}
const id = model.cellFocused;
if (!id) {
throw new Error("attempted to execute without an id");
}
return of(
actions.executeCell({ id, contentRef: action.payload.contentRef })
);
}
return of(action);
}),
tap((action: ExecuteCell) => {
if (!action.payload.id) {
throw new Error("execute cell needs an id");
}
}),
// Split stream by cell IDs
groupBy((action: ExecuteCell) => action.payload.id),
// Work on each cell's stream
map(cellAction$ =>
cellAction$.pipe(
// When a new EXECUTE_CELL comes in with the current ID, we create a
// a new stream and unsubscribe from the old one.
switchMap((action: ExecuteCell) => {
const { id } = action.payload;
const state = state$.value;
const contentRef = action.payload.contentRef;
const model = selectors.model(state, { contentRef });
// If it's not a notebook, we shouldn't be here
if (!model || model.type !== "notebook") {
return empty();
}
const cell = selectors.notebook.cellById(model, {
id
});
if (!cell) {
return empty();
}
// We only execute code cells
if ((cell as any).get("cell_type") === "code") {
const source = cell.get("source", "");
const message = createExecuteRequest(source);
return createExecuteCellStream(
action$,
state,
message,
id,
action.payload.contentRef
).pipe(
catchError((error, source) =>
merge(
of(
actions.executeFailed({
error,
contentRef: action.payload.contentRef
})
),
source
)
)
);
}
return empty();
})
)
),
// Bring back all the inner Observables into one stream
mergeAll(),
catchError((error: Error, source) => {
// Either we ensure that all errors are caught when the action.payload.contentRef
// is in scope or we make this be a generic ERROR
// $FlowFixMe: see above
return merge(
of(
actions.executeFailed({
error
})
//.........这里部分代码省略.........