GithubNPM
Options
All
  • Public
  • Public/Protected
  • All
Menu

The RxJS Ninja Boolean module contains operators for working with, and returning boolean values, and for filtering any source with predicate methods to check for both truthy and falsy values.

Boolean Module

Type aliases

PredicateFn<T>: (...args: T[]) => boolean

A function that takes one or more parameters and returns a boolean value based on the function calculation

example

Return is a number is boolean true

const isTruthy: PredicateFn<number> = (item: number) => Boolean(number);
example

Return is a number is greater than 10

const isTruthy: PredicateFn<number> = (item: number) => item > 10;
example

Return if two numbers match

const isTruthy: PredicateFn<number> = (item1: number, item2: number) => item1 === item2;

Type parameters

  • T: unknown

    The type of the value being checked

Type declaration

    • (...args: T[]): boolean
    • Parameters

      • Rest ...args: T[]

      Returns boolean

Create Functions

  • fromBoolean<T>(input: Subscribable<Iterable<T> | T> | Iterable<T> | T): Observable<boolean>
  • Returns an Observable that emits boolean values from passed source of values

    example

    Create an Observable that emits a single boolean value from a string, flip the value

    fromBoolean('Hello RxJS Ninja').pipe(flip()).subscribe();
    

    Output: false

    example

    Create an Observable that emits a single boolean from an array of values

    fromBoolean(['', 'Hello', 'RxJS', '']).subscribe();
    

    Output: false, true, true, false;

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    • input: Subscribable<Iterable<T> | T> | Iterable<T> | T

      Input values to create the Observable source from

    Returns Observable < boolean >

    Observable that emits a boolean value of the source value Boolean conversion

  • toBoolean<T>(predicateFn?: PredicateFn<T>): OperatorFunction<T, boolean>
  • Returns an Observable that emits a boolean based on the input value:

    • If no predicate method, the value will be converted to it's Boolean value
    • If a PredicateFn is passed it's used to convert the source value to a boolean based on the function condition
    example

    Returns boolean values for source strings

    const input = ['', 'Hello', 'RxJS', ''];
    from(input).pipe(toBoolean()).subscribe()
    

    Output: false, true, true, false

    example

    Returns boolean values for source strings with predicate

    const input = ['', 'Hello', 'RxJS', ''];
    from(input).pipe(toBoolean(v => v.length < 5)).subscribe()
    

    Output: true, false, true, true

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    Returns OperatorFunction < T , boolean >

    Observable that emits a boolean value of a source value Boolean conversion

Filter Functions

  • filterFalsy<T>(predicate: PredicateFn<T>): MonoTypeOperatorFunction<T>
  • Returns an Observable that emits only values from a source that does not pass the passed predicate

    example

    Returns a number value from a source where the number does not pass the predicate

    const mod2 = (num: number) => num % 2 === 0
    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(filterFalsy(mod2)).subscribe();
    

    Output: 1, 3, 5

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    Returns MonoTypeOperatorFunction < T >

    Observable that emits only values that don't pass the PredicateFn

  • filterTruthy<T>(predicate?: PredicateFn<T>): MonoTypeOperatorFunction<T>
  • Returns an Observable that emits only truthy values from a source when the value is truthy for Boolean conversion of the value - with optional PredicateFn to further filter the truthy values with a stricter check

    example

    Returns a string value from a source where the string is truthy

    const input = ['', 'RxJS', '', 'Ninja', ''];
    from(input).pipe(filterTruthy()).subscribe();
    

    Output: 'RxJS', 'Ninja'

    example

    Returns a string value from a source where the string passes the predicate

    const input = ['', 'RxJS', '', 'Ninja', ''];
    from(input).pipe(filterTruthy(v => v.length < 5 )).subscribe();
    

    Output: 'RxJS'

    example

    Returns a number value from a source where the number is truthy

    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(filterTruthy()).subscribe();
    

    Output: 2, 4

    example

    Returns a number value from a source where the number passes the predicate

    const mod3 = (num: number) => num % 3 === 0
    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(filterTruthy(mod3)).subscribe();
    

    Output: 3, 6

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    Returns MonoTypeOperatorFunction < T >

    Observable that emits only truthy values or values that pass the optional PredicateFn

  • firstFalsy<T>(predicate: PredicateFn<T>): MonoTypeOperatorFunction<T>
  • Returns an Observable that emits the first value that does not pass the predicate

    example

    Return the first falsy number for the predicate

    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(firstTruthy((value) => value % 2 === 0)).subscribe();
    

    Output: 1

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    Returns MonoTypeOperatorFunction < T >

    Observable that emits the first values to not pass the predicate

  • firstTruthy<T>(predicate?: PredicateFn<T>): MonoTypeOperatorFunction<T>
  • Returns an Observable that emits the first truthy value from a source that is truthy for Boolean conversion of the value - with optional PredicateFn to further filter the truthy values with a stricter check

    example

    Return the first truthy string

    const input = ['', '', 'Hello', 'RxJS', 'Ninja'];
    from(input).pipe(firstTruthy()).subscribe();
    

    Output: 'Hello'

    example

    Return the first truthy number

    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(firstTruthy()).subscribe();
    

    Output: 1

    example

    Return the first truthy number with predicate

    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(firstTruthy((value) => value % 2 === 0)).subscribe();
    

    Output: 2

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    Returns MonoTypeOperatorFunction < T >

    Observable that emits the first truthy value

  • lastFalsy<T>(predicate: PredicateFn<T>): MonoTypeOperatorFunction<T>
  • Returns an Observable that emits the last value that does not pass the predicate

    example

    Return the last falsy number for the predicate

    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(lastFalsy((value) => value % 2 === 0)).subscribe();
    

    Output: 5

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    Returns MonoTypeOperatorFunction < T >

    Observable that emits the last values to not pass the predicate

  • lastTruthy<T>(predicate?: PredicateFn<T>): MonoTypeOperatorFunction<T>
  • Returns an Observable that emits the last truthy value from a source, the value is truthy for Boolean conversion of the value - with optional PredicateFn to further filter the truthy values with a stricter check

    example

    Return the last truthy string

    const input = ['', '', 'Hello', 'RxJS', 'Ninja'];
    from(input).pipe(lastTruthy()).subscribe();
    

    Output: 'Ninja'

    example

    Return the last truthy number

    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(lastTruthy()).subscribe();
    

    Output: 6

    example

    Return the last truthy number with predicate

    const input = [0, 1, 2, 3, 4, 5, 6];
    from(input).pipe(lastTruthy((value) => value % 2 === 0 && value % 3 !== 0)).subscribe();
    

    Output: 4

    Type parameters

    • T: unknown

      Type of the value from the source Observable

    Parameters

    Returns MonoTypeOperatorFunction < T >

    Observable that emits the last truthy value

Modify Functions

  • flip(): MonoTypeOperatorFunction<boolean>
  • Returns an Observable that emits a flipped boolean value from the source value

    example

    Returns flipped boolean values

    const input = [false, true, false];
    from(input).pipe(flip()).subscribe()
    

    Output: true, false, true

    Returns MonoTypeOperatorFunction < boolean >

    Observable that emits a boolean where the source Observable value have been flipped

Validation Functions

  • luhnCheck<T>(): OperatorFunction<T, boolean>
  • Returns an Observable that emits a boolean value.

    The source should emit a string or number that can be checked with the Luhn Algorithm used to validate identification numbers such as bank cards and IMEI numbers.

    example

    Returns if the value passes the Luhn check.

    const input = ['4485275742308327', '1111222233334444', '111133332224444'];
    from(input).pipe(luhnCheck()).subscribe();
    

    Output: [true, true, false]

    Type parameters

    • T: string | number

      string or number value to do the comparison against

    Returns OperatorFunction < T , boolean >

    Observable that emits an boolean if the source value passes the Luhn check

Generated using TypeDoc, the 08/02/2021 at 13:47:36