https://github.com/tomaskraus/split-if
Packs all the RxJS source Observable values into arrays. These arrays are emitted every time a predicate is fulfilled.
https://github.com/tomaskraus/split-if
array buffer functional operators partition partitionn partitionwith rxjs split splitwhen splitwith typescript
Last synced: 2 months ago
JSON representation
Packs all the RxJS source Observable values into arrays. These arrays are emitted every time a predicate is fulfilled.
- Host: GitHub
- URL: https://github.com/tomaskraus/split-if
- Owner: tomaskraus
- License: mit
- Created: 2022-12-02T22:00:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-15T10:01:48.000Z (about 1 year ago)
- Last Synced: 2024-04-18T03:18:05.229Z (about 1 year ago)
- Topics: array, buffer, functional, operators, partition, partitionn, partitionwith, rxjs, split, splitwhen, splitwith, typescript
- Language: TypeScript
- Homepage:
- Size: 1.64 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://github.com/google/gts)# split-if
RxJS operator. Packs all the source Observable values into arrays. New array is emitted every time a predicate is fulfilled.
See [RxJS](https://rxjs.dev/)
### Examples
#### Example 1 (split by value)
```js
const {from} = require('rxjs');
const {splitIf} = require('split-if');const src = from([4, 8, 1, 3, 5, 1, 1, 6, 8]).pipe(splitIf(x => x === 1));
const res = [];
src.subscribe(x => res.push(x));
console.log(res);
//=> [ [ 4, 8 ], [ 1, 3, 5 ], [ 1 ], [ 1, 6, 8 ] ]
```#### Example 2 (split by index)
We can easily make a `partitionN` RxJS operator using `splitIf`:
```ts
import {OperatorFunction} from 'rxjs';
import {splitIf} from 'split-if';const partitionN = (n: number): OperatorFunction =>
splitIf((_: T, i: number) => i % n === 0);
```Let's use it:
```ts
import {from} from 'rxjs';const src = from([4, 8, 1, 3, 5, 4, 1, 6, 8, -1, 2]).pipe(partitionN(3));
const res: number[][] = [];
src.subscribe((x: number[]) => res.push(x));
console.log(res);
//=> [ [ 4, 8, 1 ], [ 3, 5, 4 ], [ 1, 6, 8 ], [ -1, 2 ] ]
```## Why to use
- Typed. With `d.ts` for Javascript.
- Well tested. 100% code coverage.## Installation
```bash
$ npm install split-if
```## Usage
Typescript / ES module:
```ts
import {splitIf} from 'split-if';
```Javascript / CommonJS:
```js
const {splitIf} = require('split-if');
```## API
```ts
function splitIf(
predicate: (value: T, index: number) => boolean,
thisArg?: any
): OperatorFunction;
```Where its `OperatorFunction` can be substituted for:
```ts
(source: Observable) => Observable;
```**See**: RxJS [OperatorFunction](https://rxjs.dev/api/index/interface/OperatorFunction)
### Parameters
| | | |
| --------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| T | {any} | The type of Observable's value. |
| predicate | {(value: T, index: number) => boolean} | A function that evaluates each value emitted by the source `Observable`. If it returns `true`, a new array of previous 'buffered' values is emitted. The index parameter is the number i for the i-th source emission that has happened since the subscription, starting from the number 0. |
| thisArg | {any} | Optional. Default is undefined. An optional argument to determine the value of this in the predicate function. |### Returns
A function that returns an `Observable` that emits arrays of all items from the source Observable. These arrays are emitted every time a predicate is fulfilled.