|
1 | | -import { OperatorDoc } from '../operator.model'; |
| 1 | +import { OperatorDoc } from "../operator.model"; |
2 | 2 |
|
3 | 3 | export const groupBy: OperatorDoc = { |
4 | | - 'name': 'groupBy', |
5 | | - 'operatorType': 'transformation' |
| 4 | + name: "groupBy", |
| 5 | + operatorType: "transformation", |
| 6 | + signature: ` |
| 7 | + public groupBy(keySelector: (value: T) => K, |
| 8 | + elementSelector?: ((value: T) => R) | void, |
| 9 | + durationSelector?: (grouped: GroupedObservable<K, R>) => Observable<any>, |
| 10 | + subjectSelector?: () => Subject<R>) |
| 11 | + : Observable<any>): OperatorFunction<T, GroupedObservable<K, R>>`, |
| 12 | + parameters: [ |
| 13 | + { |
| 14 | + name: "keySelector", |
| 15 | + type: "(value: T) => K", |
| 16 | + attribute: "", |
| 17 | + description: `A function that extracts the key used for grouping for each item.` |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "elementSelector", |
| 21 | + type: "((value: T) => R) | void", |
| 22 | + attribute: "optional", |
| 23 | + description: `A function that extracts the emitted element for each item. Default is identity function.` |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "durationSelector", |
| 27 | + type: "(grouped: GroupedObservable<K, R>) => Observable<any>", |
| 28 | + attribute: "optional", |
| 29 | + description: `A function that returns an Observable to determine how long each group should exist.` |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "subjectSelector", |
| 33 | + type: "() => Subject<R>", |
| 34 | + attribute: "optional", |
| 35 | + description: `` |
| 36 | + } |
| 37 | + ], |
| 38 | + marbleUrl: "http://reactivex.io/rxjs/img/groupBy.png", |
| 39 | + shortDescription: { |
| 40 | + description: ` |
| 41 | + Group, according to a specified key, elements from items emitted by an Observable, |
| 42 | + and emit these grouped items as GroupedObservables, one GroupedObservable per group. |
| 43 | + `, |
| 44 | + extras: [] |
| 45 | + }, |
| 46 | + walkthrough: { |
| 47 | + description: ` |
| 48 | + <p>When the Observable emits an item, a key is computed for this item with the keySelector function.</p> |
| 49 | +
|
| 50 | + <p>If a GroupedObservable for this key exists, this GroupedObservable emits. Elsewhere, a new |
| 51 | + GroupedObservable for this key is created and emits.</p> |
| 52 | +
|
| 53 | + <p>A GroupedObservable represents values belonging to the same group represented by a common key. |
| 54 | + The common key is available as the key field of a GroupedObservable instance.</p> |
| 55 | +
|
| 56 | + <p>The elements emitted by GroupedObservables are by default the items emitted by the Observable, |
| 57 | + or elements returned by the elementSelector function. |
| 58 | + ` |
| 59 | + }, |
| 60 | + examples: [ |
| 61 | + { |
| 62 | + name: "Group objects by id and return as array", |
| 63 | + code: ` |
| 64 | + interface Obj { |
| 65 | + id: number; |
| 66 | + name: string; |
| 67 | + } |
| 68 | + Rx.Observable.of<Obj>({id: 1, name: 'aze1'}, |
| 69 | + {id: 2, name: 'sf2'}, |
| 70 | + {id: 2, name: 'dg2'}, |
| 71 | + {id: 1, name: 'erg1'}, |
| 72 | + {id: 1, name: 'df1'}, |
| 73 | + {id: 2, name: 'sfqfb2'}, |
| 74 | + {id: 3, name: 'qfs3'}, |
| 75 | + {id: 2, name: 'qsgqsfg2'}) |
| 76 | + .groupBy(p => p.id) |
| 77 | + .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], [])) |
| 78 | + .subscribe(p => console.log(p)); |
| 79 | + /* |
| 80 | + Output: |
| 81 | + [ { id: 1, name: 'aze1' }, |
| 82 | + { id: 1, name: 'erg1' }, |
| 83 | + { id: 1, name: 'df1' } ] |
| 84 | +
|
| 85 | + [ { id: 2, name: 'sf2' }, |
| 86 | + { id: 2, name: 'dg2' }, |
| 87 | + { id: 2, name: 'sfqfb2' }, |
| 88 | + { id: 2, name: 'qsgqsfg2' } ] |
| 89 | +
|
| 90 | + [ { id: 3, name: 'qfs3' } ] |
| 91 | + */ |
| 92 | + `, |
| 93 | + externalLink: { |
| 94 | + platform: "JSBin", |
| 95 | + url: "http://jsbin.com/linekelumo/1/embed?js,console" |
| 96 | + } |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Pivot data on the id field", |
| 100 | + code: ` |
| 101 | + interface Obj { |
| 102 | + id: number; |
| 103 | + name: string; |
| 104 | + } |
| 105 | + Rx.Observable.of<Obj>({id: 1, name: 'aze1'}, |
| 106 | + {id: 2, name: 'sf2'}, |
| 107 | + {id: 2, name: 'dg2'}, |
| 108 | + {id: 1, name: 'erg1'}, |
| 109 | + {id: 1, name: 'df1'}, |
| 110 | + {id: 2, name: 'sfqfb2'}, |
| 111 | + {id: 3, name: 'qfs1'}, |
| 112 | + {id: 2, name: 'qsgqsfg2'}) |
| 113 | + .groupBy(p => p.id, p => p.name) |
| 114 | + .flatMap( (group$) => group$.reduce((acc, cur) => [...acc, cur], ["" + group$.key])) |
| 115 | + .map(arr => ({'id': parseInt(arr[0]), 'values': arr.slice(1)})) |
| 116 | + .subscribe(p => console.log(p)); |
| 117 | + /* |
| 118 | + Output: |
| 119 | + { id: 1, values: [ 'aze1', 'erg1', 'df1' ] } |
| 120 | +
|
| 121 | + { id: 2, values: [ 'sf2', 'dg2', 'sfqfb2', 'qsgqsfg2' ] } |
| 122 | +
|
| 123 | + { id: 3, values: [ 'qfs1' ] } |
| 124 | + */ |
| 125 | + `, |
| 126 | + externalLink: { |
| 127 | + platform: "JSBin", |
| 128 | + url: "http://jsbin.com/racikizeji/embed?js,console" |
| 129 | + } |
| 130 | + } |
| 131 | + ], |
| 132 | + relatedOperators: [], |
| 133 | + additionalResources: [] |
6 | 134 | }; |
0 commit comments