RxJS Ninja
RxJS Ninja is a set of libraries that provide operators and observables for RxJS.
There are 124 functions provided as operators or Observable generators, split down into separate modules for each domain.
Packages
RxJS Ninja is composed of libraries separated into sets of functionality, you don't need to install all the operators at one time.
Below is each package npm name and version, once installed you can import any operator or observable into your project. You can also check out the source on GitHub.
Arrays
@rxjs-ninja/rxjs-array
provides operators for RxJS for creating Observables of Array values, and for querying,
filtering and modifying Arrays
and Sets with all results
returned as Arrays where the operator provides them.
Function and Operator categories
- Convert - Functions that convert source Observable
Set
,Map
andObject
toArray
- Filter - Operators that return source Arrays, or items from arrays using filtering functions or properties
- Modify - Operators that modify Arrays or their values
- Query - Operators that return non-Array values based on querying an arrays values
- Set - Operators for working with
Set
objects (currently onlytoSet
but more operators to come!)
For example, you could sortMap
an array of values from number into boolean and them flipArray
the values:
import { of } from 'rxjs';
import { sortMap, flipArray } from '@rxjs-ninja/rxjs-array';
of([10, 4, 7, 3, 1, 29, 5])
.pipe(
/**
* Out of the box `sortMap` does a basic sort on an array so the
* result will be [1, 3, 4, 5, 7, 10, 29]
* Then the map function will be called with the result, here we do a modulus 2 check
* so the result is [false, false, true, false, false, true, false]
*/
sortMap((value) => value % 2),
// Now we flip the array
flipArray(),
)
.subscribe(); // [true, true, false, true, true, false, true]
Booleans
@rxjs-ninja/rxjs-boolean
provides operators for querying, filtering and modifying boolean values, and Observable for
generating boolean emitters.
Function and Operator categories
- Create - Functions and Operators for creating Observable boolean values
- Filter - Operators for filtering Observable sources for truthy values
- Modify - Operators for modifying boolean values
- Validation - Operators that provide boolean output based on checks against source values
For example, you can use the firstTruthy
or lastTruthy
value from an array:
import { from } from 'rxjs';
import { firstTruthy, lastTruthy } from '@rxjs-ninja/rxjs-array';
const inputObs$ = from(['', '', 'Hello', 'There', 'RxJS', 'Ninja', '', '']);
inputObs$.pipe(firstTruthy()).subscribe(); // ['Hello']
inputObs$.pipe(lastTruthy()).subscribe(); // ['Ninja']
Numbers
@rxjs-ninja/rxjs-number
provides operators for querying, filtering and modifying number values, and Observable for
generating number emitters.
Function and Operator categories
- Create - Functions and Operators for creating Observable number values
- Filter - Operators for filtering Observable number sources for truthy queries
- Formatting - Operators for formatting numbers to strings
- Math - Operators for some math operations such as add, subtract, multiply and raise by power
- Parsing - Operators for parsing strings to numbers
- Query - Operators for generating number sources and getting boolean values
For example, you can use the fromNumber
to generate a sequence of numbers and filter out ones that are out of range.
import { fromNumber, filterInRange, fitlerOutOfRange } from '@rxjs-ninja/rxjs-number';
const inputObs$ = fromNumber([10, 4, 3, 6, 12, 2, 1, 5]);
inputObs$.pipe(filterInRange(4, 10)).subscribe(); // 10, 4, 6, 5
inputObs$.pipe(filterOutOfRange(4, 10)).subscribe(); // 3, 12, 2, 1
Randomness
@rxjs-ninja/rxjs-random
Observable for generating random emitters with both numbers and strings.
Function and Operator categories
- Random Numbers - Generates random number streams
- Random Strings - Generates random string streams
For example, you can use the fromRandomCrypto
to generate a sequence of random number between -127
and 127
, then
use inRange
from @rxjs-ninja/rxjs-number
and flip
from @rxjs-ninja/rxjs-boolean
.
import { fromRandomCrypto } from '@rxjs-ninja/rxjs-random';
import { inRange } from '@rxjs-ninja/rxjs-number';
import { flip } from '@rxjs-ninja/rxjs-boolean';
/**
* In this example the random crypto might create
* values like:
* 12, -114, -89, 1, 18, -90, 56....
*/
fromRandomCrypto(0, { bytes: 1, unsigned: true })
.pipe(
/**
* Here we check they are in range and return `true` or `false`
* instead of the value (use `filterInRange` to do this)
* In this example we get:
* ...true, false, false, true, true, false, true....
*/
inRange(-64, 64),
// Flip each value
flip(),
)
.subscribe(); // ...false, true, true, false, false, true, false...
Strings
@rxjs-ninja/rxjs-string
provides operators for querying, filtering and modifying string values, and Observable for
generating string emitters.
Function and Operator categories
- Create - Functions and Operators for creating Observable string values
- Convert - Operators to convert strings to other types
- Filter - Operators for filtering Observable string sources for truthy values
- Mapping - Operators that provide mapping to strings from other value types
- Modify - Operators for modifying string values
- Query - Operators that return non-string values based on querying string values
For example, you can use the fromString
to generate a sequence of strings and check they include a string
using include
. We can also titlize
strings.
import { fromString, includes, titleize } from '@rxjs-ninja/rxjs-string';
const inputObs$ = fromNumber(['full power', 'half power', 'quarter power']);
inputObs$.pipe(includes('half')).subscribe(); // false, true, false
inputObs$.pipe(titleize()).subscribe(); // Full Power, Half Power, Quarter Power
Utilities
@rxjs-ninja/rxjs-utility
provides operators for working with Observable values to view them, and modify them and don't
all into the other module categories.
Function and Operator categories
- HTTP - Functions for working with HTTP Observables
- Mapping - Operators use for mapping to different values
- Side Effects - Operators for handling side effects
- Streams - Operators and Functions for working with other types of streams of data
For example, using the Side Effects category:
import { fromEvent } from 'rxjs';
import { startWithTap, tapIf, tapOnSubscribe } from '@rxjs-ninja/rxjs-utility';
const inputObs$ = fromEvent(document, 'click').pipe(
startWithTap(() => console.log('This will only fire once')),
tapOnSubscribe(() => console.log('This will tab on every subscribe')),
tapIf(
(event) => event.target.id === 'some-div',
() => console.log('This will tap if the user clicks on the target element'),
),
);
inputObs$.subscribe(); // This will only fire once, This will tab on every subscribe
inputObs$.subscribe(); // This will tab on every subscribe
Additional Information
- Logo created by DesignEvo logo maker
Generated using TypeDoc, the 1/19/2021 at 12:42:11 AM