@jbobo/rx
TypeScript icon, indicating that this package has built-in type declarations

1.9.1 • Public • Published

Jbobo Rx

Statements Branches Functions Lines

@jbobo/rx is a simple RxJS utility library.

Table of contents

Installation

Using npm package manager.

npm install @jbobo/rx

API Reference

Operators

RxJS Utility operators

arrayFilter

Collects all the source emission (emitting arrays) and emits them after filtering, when the source completes.

import { of } from 'rxjs';
import { arrayFilter } from '@jbobo/rx'

...
const source = of(['abc', 'abd', 'abe', 'fgh', 'ilm'])
const filterStrings = function(x) {
 return x.startsWith('a');
}
const example = source.pipe(
  sort(filterStrings),
);
// output:
// ['abc', 'abd', 'abe']
const subscribe = example.subscribe(val => {
 ...
});
...
Parameter Type Description
fn Function Required. The filtering condition.

distinctSwitchMap

Projects each values with projectionFn discarding emitted values that take less than an optional dueTime between output. Filters consecutive duplicated emissions based on an optional compareFn. Has the same cancelling effect of switchMap.

import { of } from 'rxjs';
import { distinctSwitchMap } from '@jbobo/rx'

...
const source = of(1, 1, 2, 2, 3);
const example = source.pipe(
  distinctSwitchMap((v) => of(`switching from ${v}`)),
);
// output:
// switching from 1
// switching from 2
// switching from 3
const subscribe = example.subscribe(val => {
 ...
});
...
Parameter Type Description
projectionFn Function Required. A function applied to the values emitted by the source Observable that provides an Observable.
compareFn Function Optional. A comparison function between current and previous value. Default Strict equality check (===).
dueTime number Optional. The dueTime in milliseconds required to wait for emission silence before emitting the most recent source value.

equals

Emits all the values equal to the provided one.

import { of } from 'rxjs';
import { equals } from '@jbobo/rx'

...
const source = of(1, 2, 3, 4, 5, 5, 5, 6, 7);
const example = source.pipe(
  equals(5),
);
// output:
// 5
// 5
// 5
const subscribe = example.subscribe(val => {
 ...
});
...
Parameter Type Description
value string or number or boolean Required. The value to match.

filterNullish

Filters the undefined or null values.

import { of } from 'rxjs';
import { filterNullish } from '@jbobo/rx'

...
const source = of(null, undefined, 1, 2, 3);
const example = source.pipe(
  filterNullish(),
);
// output:
// 1
// 2
// 3
const subscribe = example.subscribe(val => {
...
});
...

filterFalsy

Filters the falsy values.

import { of } from 'rxjs';
import { filterFalsy } from '@jbobo/rx'

...
const source = of(null, undefined, 0, -0, NaN, '', false, 1, 2, 3);
const example = source.pipe(
  filterFalsy(),
);
// output:
// 1
// 2
// 3
const subscribe = example.subscribe(val => {
...
});
...

filterTruthy

Filters the truthy values.

import { of } from 'rxjs';
import { filterFalsy } from '@jbobo/rx'

...
const source = of(1, 2, 3, null, undefined, 0, -0, NaN, '', false);
const example = source.pipe(
 filterTruthy(),
);
// output:
// null
// undefined
// 0
// -0
// NaN
// ''
// false
const subscribe = example.subscribe(val => {
...
});
...

logger

Logs the observer callbacks each time they gets called.

import { of } from 'rxjs';
import { logger } from '@jbobo/rx'

...
const source = of(1, 2, 3);
const example = source.pipe(
  logger(),
);
// output:
// [Next] 1
// [Next] 2
// [Next] 3
const subscribe = example.subscribe(val => {
 ...
});
...

pluck

Emits the selected properties; reimplementation of the original RxJS pluck. It also supports nested properties separated by dots.

import { from } from 'rxjs';
import { pluck } from '@jbobo/rx'

...
const source1 = from([
 { name: 'Jack', age: 1 },
 { name: 'Bobo', age: 5 }
]);
const example1 = source1.pipe(
  pluck('name'),
);
// output:
// Jack
// Bobo
const subscribe1 = example1.subscribe(val => {
 ...
});

const source2 = from([
 { name: 'Jack', age: 1, job: { title: 'Dog', language: 'JavaScript' } },
 { name: 'Bobo', age: 5 }
]);
const example2 = source2.pipe(
  pluck('job', 'title'),
);
// output:
// Dog
// undefined
const subscribe2 = example2.subscribe(val => {
 ...
});
...

const source3 = from([
 { a: { b: { c: { d: '1' } } },
 { a: { b: { c: { d: '2' } } },
 { a: { b: { c: { d: '3' } } },
]);
const example3 = source3.pipe(
  pluck('a.b.c'),
  pluck('d')
);
// output:
// '1'
// '2'
// '3'
const subscribe3 = example3.subscribe(val => {
 ...
});
...
Parameter Type Description
props Array<string or number or symbol> Required. The property or the list of nested properties. Can also be provided as string separated by dots.

shuffle

Collects all the source emission and emits after randomly shuffling them, when the source completes. Based on the Fisher-Yates shuffle.

import { of } from 'rxjs';
import { shuffle } from '@jbobo/rx'

...
const source = of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
const example = source.pipe(
  shuffle(),
);
// output:
// [8, 2, 3, 4, 5, 6, 7, 0, 9, 1]
const subscribe = example.subscribe(val => {
 ...
});
...

sort

Collects all the source emission and emits them after sorting, when the source completes.

import { of } from 'rxjs';
import { sort } from '@jbobo/rx'

...
const source = from([
 { name: 'Zara', id: 10 },
 { name: 'Jack', id: 0 },
 { name: 'Oliver', id: 7 }
]);
const sortAscending = function(a, b) {
 return a.id - b.id;
}
const example = source.pipe(
  sort(sortAscending),
);
// output:
// { name: 'Jack', id: 0 }
// { name: 'Oliver', id: 7 }
// { name: 'Zara', id: 10 }
const subscribe = example.subscribe(val => {
 ...
});
...
Parameter Type Description
fn Function Required. The comparison function.

sum

Collects all the source emission and emits the sum, when the source completes. In case the source emits objects it is possibile to specify an object property (or multiple properties separated by a dot) on which the sum will be performed.

import { of } from 'rxjs';
import { sum } from '@jbobo/rx'

...
const source1 = from([0, 5, 7, 3, 2, 9, 1, 6, 8, 4]);
const example1 = source1.pipe(
  sum(),
);
// output:
// 45
const subscribe1 = example1.subscribe(val => {
 ...
});
...

const source2 = from([{ a: 5 }, { a: 7 }, { a: 9 }]);
const example2 = source2.pipe(
  sum(0, 'a')
);
// output:
// 21
const subscribe2 = example2.subscribe(val => {
 ...
});
...

const source3 = from([{ a: { b: 3 } }, { a: { b: 9 } }, { a: { b: 2 } }]);
const example3 = source3.pipe(
  sum(0, 'a.b')
);
// output:
// 14
const subscribe3 = example3.subscribe(val => {
 ...
});
...
Parameter Type Description
initial number Optional. The initial value.
Parameter Type Description
initial number Required. The initial value.
prop string Required. The property on which perform the sum. Can Also be a string composed by nested properties separated by a dot.

tapN

Like the standard rxjs tap operator but executes the provided callback a defined number of times.

import { of } from 'rxjs';
import { tapN } from '@jbobo/rx'

...
const source = of(3, 4, 5, 6, 7, 8, 9, 10, 11);
const example = source.pipe(
  tapN((val, counter) => console.log(`Value: ${val} Counter: ${counter}`), 7),
);
//output:
// Value: 3 Counter: 1
// Value: 4 Counter: 2
// Value: 5 Counter: 3
// ...
const subscribe = example.subscribe(val => console.log(val));
...
Parameter Type Description
fn Function Required. The function to execute, provides also the actual execution counter.
times number Optional. The number of times that the callback will be executed. Default. 1.

Subscriptions

Utility functions/decorators to deal with subscriptions.

AutoUnsubscribe

Class decorator used to unsubscribe from observables "before" or "after" a specified function.

import { AutoUnsubscribe } from '@jbobo/rx'

@AutoUnsubscribe('ngOnDestroy')
@Component({
   ...
})
Parameter Type Description
functionName string Required. The name of the function.
when AutoUnsubscribeWhen Optional. The moment when the unsubscribe will occur; AutoUnsubscribeWhen.AFTER or AutoUnsubscribeWhen.BEFORE the functionName. Default. AutoUnsubscribeWhen.AFTER.
blackList string[] Optional. List of observable that will not be unsubscribed.

createSubsCounter

Utility function to keep track of rxjs subscription and unsubscribe from all in one place.

import { createSubsCounter } from '@jbobo/rx'

...
const counter = createSubsCounter();
counter.add(
 obs1.subscribe(),
 obs2.subscribe(),
);
counter.unsubscribe();
...
Parameter Type Description
initialSubscriptions Subscription[] Optional. The subscriptions list.

License

MIT

Package Sidebar

Install

npm i @jbobo/rx

Weekly Downloads

21

Version

1.9.1

License

MIT

Unpacked Size

66.2 kB

Total Files

93

Last publish

Collaborators

  • jbobo