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

The RxJS Ninja Array module contains operators for working with, and returning Array values. There are also methods for working with Set, Map and Object converting to and from Array types.

Array Module

Type aliases

BinarySearchResult<T, K>: [index: number, searchValue: T, sortedArray: K[], searchArray: K[]]

The return value of the binarySearch operator, contains in order

  • The index of the found item, or -1 if not found
  • The value that was passed for search
  • The sorted Array used for searching
  • The original Array unsorted

Type parameters

  • T: unknown

    The type of the value being searched for

  • K: unknown

    The type of value in the search Array

MapFn<T, K>: (value: T) => K

A function used to map values to a modified value, either of the same type (String.toUpperCase) or of a different type (Number.parseInt)

example

Map a string to a number

const mapToNumber: MapFn<string, number> = (input: string): number => parseInt(input);
example

Map any string to an upper case string

const mapToNumber: MapFn<string> = (input: string): string => input.toUpperCase();

Type parameters

  • T = unknown

    The type of value as input to the method

  • K = T | unknown

    The type of value returned from the method

Type declaration

    • (value: T): K
    • Parameters

      • value: T

      Returns K

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

SortFn<T>: (first: T, second: T) => number

A function used to sort data using a calculation defined in the method, returning either -1, 0 or 1 based on the ordering requirements

example

Sort strings using String.localeCompare, otherwise directly compare

function defaultSortFn<T extends unknown>(first: T, second: T): number {
 if (typeof first === 'string') {
   return first.localeCompare(second);
 }
 if (first === second) return 0;
 return first < second ? -1 : 1;
}

Type parameters

  • T: unknown

    The Type of the value to sort

Type declaration

    • (first: T, second: T): number
    • Parameters

      • first: T
      • second: T

      Returns number

Filter Functions

  • difference<T>(compare: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable that emits an Array containing unique values from the source that are not in the compare parameter

    see

    Operator differenceAll when you want to get all differences in both source and input array

    see

    Operator filterDifference when you want to emit a result with duplicate values

    example

    Returns values from the source Array not contained in the compare Array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(difference(['a', 'c'])).subscribe();
    

    Output: ['b', 'd']

    Type parameters

    • T: unknown

      Type of value contained in the source Array or Set

    Parameters

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

      Array or Set value to compare the source value with

    Returns OperatorFunction < Iterable < T > , T [] >

    Observable that emits an Array containing items from the source not in the comparison value

  • differenceAll<T>(compare: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, T[][]>
  • Returns an Observable that emits an tuple containing two Array both containing differences in the source and compare value. The first array contains items from the source not contained in compare and the second values from compare not in the source.

    see

    Operator difference for only the values from the source

    see

    Operator filterDifference when you want to emit a result with duplicate values

    example

    Returns both differences between the source and array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(differenceAll(['a', 'c', 'g'])).subscribe();
    

    Output: [ ['b', 'd'], ['g'] ]

    Type parameters

    • T: unknown

      Type of value contained in the source Array or Set

    Parameters

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

      Array or Set value to compare the source value with

    Returns OperatorFunction < Iterable < T > , T [] [] >

    Observable that emits an tuple containing two Array values with the source difference and input difference

  • filterDifference<T>(compare: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable Array containing filtered values that are not in the provided input Array or Set

    see

    Operator difference for only the values from the source

    see

    Operator differenceAll when you want to get all differences in both source and input array

    example

    Returns the difference between the source array and the passed static array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(filterDifference(['a', 'c'])).subscribe();
    

    Output: 'b', 'd', 'b'

    example

    Returns the difference between the source array and the passed Observable array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(filterDifference(of(['a', 'c']))).subscribe();
    

    Output: 'b', 'd', 'b'

    Type parameters

    • T: unknown

      Type of value contained in the source Array or Set

    Parameters

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

      Array or Set value to compare the source value with

    Returns OperatorFunction < Iterable < T > , T [] >

    An Observable that emits an Array with the difference between source and input

  • filterEvery<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable that emits an array when all values in the source array return truthy using Array.every. When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    see

    The every operator returns the boolean value instead of the array

    example

    Returns a array where every string item in the array is truthy

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

    Output: ['Hello', 'RxJS', 'Ninja']

    example

    Returns a array where every string item in the array length < 4

    const input = [ ['', '', ''], ['', 'Foo', 'Bar'], ['Foo', 'Bar', 'Baz'] ];
    from(input).pipe(filterEvery(v => v.length < 4)).subscribe();
    

    Output: ['Foo', 'Bar', 'Baz']

    example

    Returns a array where every number in the array is less than 2

    const input = [ [1, 0, 1, 0, 1, 0], [1, 0, 2, 1, 0, 2], [0, 1, 0, 1, 0, 2] ];
    from(input).pipe(filterEvery(v => v < 2)).subscribe();
    

    Output: [1, 0, 1, 0, 1, 0]

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    Returns OperatorFunction < Iterable < T > , T [] >

    An Observable that emits a boolean when all values in source array return truthy with the PredicateFn

  • filterIntersects<T>(input: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable Array containing filtered values that are in both the source in the provided input Array or Set

    see

    intersects operator for an Array of unique intersection items

    example

    Returns the intersection between the source array and the passed static array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(filterIntersects(['a', 'd'])).subscribe();
    

    Output: 'a', 'd', 'a'

    example

    Returns the intersection between the source array and the passed Observable array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(filterIntersects(of(['a', 'd']))).subscribe();
    

    Output: 'a', 'd', 'a'

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

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

      Array or Set or Observable value to compare against for the intersection

    Returns OperatorFunction < Iterable < T > , T [] >

    An Observable that emits an Array of the intersection of input and source arrays.

  • filterSome<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable that emits an array when one of the values in the source array return truthy using Array.some When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    see

    The some operator returns the boolean value instead of the array

    example

    Returns a array where at least one string item in the array is truthy

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

    Output: ['', 'Hello', 'RxJS'], ['Hello', 'RxJS', 'Ninja']

    example

    Returns a array where at least string item in the array length < 4

    const input = [ ['', '', ''], ['', 'Foo', 'Bar'], ['Foo', 'Bar', 'Baz'] ];
    from(input).pipe(filterEvery(v => v.length < 4)).subscribe();
    

    Output: ['', 'Foo', 'Bar'], ['Foo', 'Bar', 'Baz']

    example

    Returns a array where at least one number in the array is less than 2

    const input = [ [1, 0, 1, 0, 1, 0], [1, 0, 2, 1, 0, 2], [0, 1, 0, 1, 0, 2] ];
    from(input).pipe(filterSome(v => v < 2)).subscribe();
    

    Output: [1, 0, 1, 0, 1, 0], [1, 0, 2, 1, 0, 2], [0, 1, 0, 1, 0, 2]

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    Returns OperatorFunction < Iterable < T > , T [] >

    An Observable that emits a boolean when all values in source array return truthy with the PredicateFn

  • find<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, T | undefined>
  • Returns an Observable value of the first truthy value found in a source array, or undefined using Array.find. When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    example

    Return the first truthy string in the array

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

    Output: 'Hello'

    example

    Return the first truthy string that has a length < 5

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

    Output: 'RxJS'

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    Returns OperatorFunction < Iterable < T > , T | undefined >

    An Observable that emits the first found value from the array, or undefined

  • findAll<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable array of truthy values from a source Array or Set When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    example

    Return an array of all numbers that are truthy

    const input = [0, 10, 1, 0, 6, 6, 0, 1];
    of(input).pipe(findAll()).subscribe();
    

    Output: [10, 1, 6, 6, 1]

    example

    Return an array of values where the source array value length >= 5

    const input = ['', '', 'Hello', 'RxJS', 'Ninja'];
    of(input).pipe(findAll(v => v.length >= 5)).subscribe();
    

    Output: ['Hello', 'Ninja']

    Type parameters

    • T: unknown

    Parameters

    Returns OperatorFunction < Iterable < T > , T [] >

    An Observable that emits an array containing all truthy values from a source array

  • findLast<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, T | undefined>
  • Returns an Observable value of the last truthy value found in a source array, or undefined using Array.find When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    example

    Return the last truthy string in the array

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

    Output: 'Ninja'

    example

    Return the last truthy string that has a length >= 5

    const input = ['', '', 'Hello', 'RxJS', 'Ninja', 'Docs'];
    of(input).pipe(findLast(v => v.length >= 5)).subscribe();
    

    Output: 'Ninja'

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    Returns OperatorFunction < Iterable < T > , T | undefined >

    An Observable that emits the last found value from the array, or undefined

  • intersects<T>(input: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable Array containing unique values that are in both the source and provided input Array or Set

    see

    filterIntersects operator for an Array containing potential duplicate intersections

    example

    Returns the intersection between the source array and the passed static array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(intersects(['a', 'd'])).subscribe();
    

    Output: 'a', 'd'

    example

    Returns the intersection between the source array and the passed Observable array

    const input = ['a', 'b', 'd', 'a', 'b'];
    of(input).pipe(intersects(of(['a', 'd']))).subscribe();
    

    Output: 'a', 'd'

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

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

      Array or Set or Observable value to compare against for the intersection

    Returns OperatorFunction < Iterable < T > , T [] >

    An Observable that emits an array of the intersection of input and source arrays.

Map Functions

  • fromMap<K, V>(input: Subscribable<Iterable<Map<K, V>> | Map<K, V>> | Iterable<Map<K, V>> | Map<K, V>): Observable<[K, V][]>
  • Returns an Observable that emits an Array from a Map

    example

    Create Array from Map

    const input = new Map([ [1, 'a'], [2, 'b'], [3, 'c'] ]);
    fromMap(input).subscribe();
    

    Output: [1, 'a'], [2, 'b'], [3, 'c']

    Type parameters

    • K: unknown

      The type of value in the Map key

    • V: unknown

      Type of the value in the Map value

    Parameters

    • input: Subscribable<Iterable<Map<K, V>> | Map<K, V>> | Iterable<Map<K, V>> | Map<K, V>

      Input to create the emit values from, can be argument list of Map, an array of Map or an Observable or Promise source

    Returns Observable < [ K , V ] [] >

    Observable that emits an Array from the input Map

  • mapToArray<K, T>(): OperatorFunction<Map<K, T>, [K, T][]>
  • Returns an Observable that emits an array from a source Map object.

    example

    Convert a Map into an Array

    const input = new Map([ [1, 'a'], [2, 'b'], [3, 'c'] ]);
    of(input).pipe(mapToArray()).subscribe();
    

    Output: [ [1, 'a'], [2, 'b'], [3, 'c'] ]

    Type parameters

    • K: unknown

      The key type of the source Map

    • T: unknown

      The value type of the source Map

    Returns OperatorFunction < Map < K , T > , [ K , T ] [] >

    Observable that emits a Array from a source Map

  • toMap<K, V>(): OperatorFunction<[K, V][], Map<K, V>>
  • Returns an Observable that emits a Map object from a source array.

    example

    Convert an Array into a Map

    const input = [ [1, 'a'], [2, 'b'], [3, 'c'] ];
    of(input).pipe(toMap()).subscribe();
    

    Output: Map(3) [1, 'a'], [2, 'b'], [3, 'c']

    Type parameters

    • K: unknown

      The type of Map key

    • V: unknown

      The type of Map value

    Returns OperatorFunction < [ K , V ] [] , Map < K , V > >

    Observable that emits a Map from a source array

Modify Functions

  • fill<T, K>(fillWith: Subscribable<K> | K, startIndex?: Subscribable<number> | number, endIndex?: Subscribable<number> | number): OperatorFunction<Iterable<T>, K[]>
  • Returns an Observable array of values filled with Array.fill. Using the source array length, some or all the values are replaced with the fillWith parameter.

    example

    Return an array with all values replaced

    const input = ['The', 'Cake', 'is', 'a', 'lie'];
    of(input).pipe(fill('CAKE!')).subscribe();
    

    Output: 'CAKE!', 'CAKE!', 'CAKE!', 'CAKE!', 'CAKE!'

    example

    Return an array where all items at and after index 2 are replaced

    const input = ['The', 'Cake', 'is', 'a', 'lie'];
    of(input).pipe(fill('CAKE!', 2)).subscribe();
    

    Output: 'The', 'Cake', 'CAKE!', 'CAKE!', 'CAKE!'

    example

    Return an array where all items at index 2 and upto index 4 are replaced

    const input = ['The', 'Cake', 'is', 'a', 'lie'];
    of(input).pipe(fill('CAKE!', 2, 4)).subscribe();
    

    Output: 'The', 'Cake', 'CAKE!', 'CAKE!', 'lie'

    Type parameters

    • T: unknown

      Item type contained in the input Array or Set

    • K: unknown

      Item type container in the output Array or Set

    Parameters

    • fillWith: Subscribable<K> | K

      The value to fill the array with

    • startIndex: Subscribable<number> | number = 0

      Optional start index to fill the array from

    • Optional endIndex: Subscribable<number> | number

      Optional index of the item to stop filling at, the last item filled is fillTo - 1

    Returns OperatorFunction < Iterable < T > , K [] >

    An Observable that emits an Array of values where some or all of the source array values are replaced with the fillValue

  • flipArray(): OperatorFunction<Iterable<boolean>, boolean[]>
  • Returns an Observable array where the source array contains boolean values, and flips the value to the opposite boolean.

    example

    Returns an array of all binary values flipped

    const input = [false, true, false];
    of(input).pipe(flipArray()).subscribe();
    

    Output: [true, false, true]

    Returns OperatorFunction < Iterable < boolean > , boolean [] >

    Observable array of boolean values that are flipped from their original value

  • join<T>(separator?: Subscribable<string> | string): OperatorFunction<Iterable<T>, string>
  • Returns an Observable that emits a joining the values of the Array or Set using the separator character using Array.join

    example

    Returns a string of an array joined with spaces

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

    Output: 'Hello RxJS Ninja'

    example

    Returns a string of an array joined with a comma and space

    const input = ['Name', 'Age', 'Location'];
    of(input).pipe(join(', ')).subscribe();
    

    Output: 'Name, Age, Location'

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    • separator: Subscribable<string> | string = ' '

      Separator to be used to join strings. Default value is a space ( ) character.

    Returns OperatorFunction < Iterable < T > , string >

    Observable string from the joined values in the source array

  • reverse<T>(): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable that emits array taking the source and running the result of Array.reverse

    example

    Reverse an array of values

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

    Output: ['Ninja', 'RxJS', 'Hello']

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Returns OperatorFunction < Iterable < T > , T [] >

    Observable that emits an array which is reversed from the source array

  • shuffle<T>(): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable that emits an array taking a source array and randomly shuffling the elements

    example

    Return a randomly shuffled array

    const input = [1, 2, 3, 4, 5, 6];
    of(input).pipe(shuffle()).subscribe();
    

    Output: [4, 2, 5, 1, 6, 3]

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Returns OperatorFunction < Iterable < T > , T [] >

    Observable that emits an array of values shuffled from the source array

  • sort<T>(sortFn?: SortFn<T>): OperatorFunction<Iterable<T>, T[]>
  • Returns an Observable that emits an array of sorted values from the source Array or Set using the SortFn

    example

    Returns a sorted array of numbers

    const input = [2, 4, 6, 1, 3, 5];
    of(input).pipe(sort()).subscribe();
    

    Output: [1, 2, 3, 4, 5, 6]

    example

    Returns a sorted array of tuples, sorting on index 1

    const input = [
     [10, 2], [20, 4], [30, 6],
     [40, 1], [50, 3], [60, 5]
    ];
    const sortTuple = (a, b) => {
     if (a[1] === b[1]) return 0;
     return a[1] < b[1] ? -1 : 1;
    }
    
    of(input).pipe(sort(sortTuple)).subscribe();
    

    Output: [ [40, 1], [10, 2], [50, 3], [20, 4], [60, 5], [30, 6] ]

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    • Optional sortFn: SortFn<T>

      Optional SortFn used to sort the array, if not provided the defaultSortFn is used.

    Returns OperatorFunction < Iterable < T > , T [] >

    Observable array of values from source array sorted via SortFn

  • sortMap<T, K>(mapFn: MapFn<T, K>, sortFn?: SortFn<T>): OperatorFunction<Iterable<T>, K[]>
  • Returns an Observable that emits an array of sorted mapped values from a source array where values are mapped to type K using a MapFn.

    example

    Returns a sorted array of binary values, sorting by number then converting to boolean

    const input = [5, 8, 2, 7, 1, 6];
    of(input).pipe(sortMap(value => value >= 5 ? true : false)).subscribe();
    

    Output: [false, false, true, true, true, true]

    example

    Returns a sorted array of strings from an object, with SortFn and MapFn

    const sortFn = (a: any, b: any) => {
     if (a.index === b.index) return 0;
     return a.index < b.index ? -1 : 0
    }
    
    const mapFn = (item: any): string => item.label;
    
    const input = [
     { index: 5, label: 'Moo' }, { index:8, label: 'Baz' },
     { index: 2, label: 'Bar' }, { index: 7: label: 'Buzz' },
     { index: 1, label: 'Foo' } , { index: 6, label: 'Fizz' }
    ];
    of(input).pipe(sortMap(sortFn, mapFn)).subscribe();
    

    Output: ['Foo', 'Bar', 'Moo', 'Fizz', 'Buzz', 'Baz']

    Type parameters

    • T: unknown

      The input type of the source Array or Set

    • K: unknown

      The type of data in the emitted array

    Parameters

    • mapFn: MapFn<T, K>

      The MapFn to map the value in the array

    • Optional sortFn: SortFn<T>

      Optional SortFn used to sort the array, if not provided the defaultSortFn is used.

    Returns OperatorFunction < Iterable < T > , K [] >

    Observable that emits an array of sorted mapped values

Object Functions

  • objectEntriesToArray<K, T>(): OperatorFunction<Record<K, T>, [string, T][]>
  • Returns an Observable that emits an Array from a source Object using Object.entries, the Array contains tuples of the key as a string and the value

    remarks

    Regardless of Object key type the result Array will have a string key value

    example

    Convert an Object into an array of entries

    const input = { 1: 'a', 2: 'b', 3: 'c' };
    of(input).pipe(objectEntriesToArray()).subscribe();
    

    Output: [ ['1', 'a'], ['2', 'b'], ['3', 'c'] ]

    Type parameters

    • K: string | number | symbol

      The key type of the source Object

    • T: unknown

      The value type of the source Object

    Returns OperatorFunction < Record < K , T > , [ string , T ] [] >

    Observable that emits a Array from a source Object entries

  • objectKeysToArray<K, T>(): OperatorFunction<Record<K, T>, string[]>
  • Returns an Observable that emits an array from a source Object using Object.keys, the array contains the object keys as strings.

    remarks

    Regardless of Object key type the result Array will have a string key value

    example

    Convert an Object into an array of keys

    const input = { 1: 'a', 2: 'b', 3: 'c' };
    of(input).pipe(objectKeysToArray()).subscribe();
    

    Output: [ '1', '2', '3' ]

    Type parameters

    • K: string | number | symbol

      The key type of the source Object

    • T: unknown

      The value type of the source Object

    Returns OperatorFunction < Record < K , T > , string [] >

    Observable that emits a Array of strings from a source Object keys

Query Functions

  • binarySearch<T, V>(search: Subscribable<T> | T, property?: Subscribable<string | number> | string | number, sortFn?: SortFn<V>): OperatorFunction<Iterable<V>, BinarySearchResult<T, V>>
  • Returns an Observable that emits a BinarySearchResult. It take a source Array or Set and runs a SortFn over it, then searches it for the passed search value. The BinarySearchResult contains the index in the sorted array, the value searched and the sorted and unsorted array. If not found the index is -1.

    see

    Binary search algorithm

    example

    Return the index of the word bravo in the sorted Array from a source array

    const input = ['bravo', 'delta', 'alpha', 'echo', 'charlie'];
    of(input).pipe(binarySearch('bravo')).subscribe();
    

    Output: <BinarySearchResult>[1, 'bravo', [...sortedArray], [...searchArray]]

    example

    Return the index of the number 30 in the sorted Array from the source array

    const input = [100, 90, 10, 20, 40, 80, 30, 25];
    of(input).pipe(binarySearch(30)).subscribe();
    

    Output: <BinarySearchResult>[3, 30, [...sortedArray], [...searchArray]]

    example

    Return the index of the object that has label of Baz, sorted using an index value

    const input = [
     { index: 5, label: 'Angular' }, { index: 7, label: 'RxJS' },
     { index: 8, label: 'Ninja' }, { index: 10, label: 'TypeScript' },
     { index: 1, label: 'JavaScript' }, { index: 4, label: 'ECMAScript' },
    ];
    const sortObj = (a:, b) => {
     if (a.index === b.index) return 0;
     return a.index < b.index ? -1 : 1;
    };
    of(input).pipe(binarySearch('Ninja', sortObj, 'label')).subscribe();
    

    Output: <BinarySearchResult>[4, 'Ninja', [...sortedArray], [...searchArray]]

    example

    Return the index of the tuple in the Array where the value at index 0 is 2, sorted by the index 1

    const input = [
     [1, 1], [2, 4], [3, 7], [4, 2], [5, 5],
     [6, 6], [7, 3], [8, 8], [9, 10], [10, 9]
    ];
    const sortArray = (a: [number, number], b: [number, number]) => {
     if (a[1] === b[1]) return 0;
     return a[1] < b[1] ? -1 : 1;
    };
    from(input).pipe(binarySearch(2, sortArray, 0)).subscribe();
    

    Output: <BinarySearchResult>[4, 2, [...sortedArray], [...searchArray]]

    Type parameters

    • T: unknown

      The type of the search value

    • V: unknown

      The type of item in the Array if different to search type

    Parameters

    • search: Subscribable<T> | T

      The value to search for in the Array

    • Optional property: Subscribable<string | number> | string | number

      Optional property for searching tuples and objects - if an tuple use a number if an Object use a string

    • Optional sortFn: SortFn<V>

      Optional SortFn for sorting more complex types

    Returns OperatorFunction < Iterable < V > , BinarySearchResult < T , V > >

    An Observable that emits a BinarySearchResult

  • every<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, boolean>
  • Returns an Observable that emits a boolean when all values in the source array return truthy using Array.every. When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    example

    Returns a boolean where every string item in the Array is truthy

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

    Output: false, false, true

    example

    Returns a boolean where every string item in the Array length < 4

    const input = [ ['', '', ''], ['', 'Foo', 'Bar'], ['Foo', 'Bar', 'Baz'] ];
    from(input).pipe(every(v => v.length < 4)).subscribe();
    

    Output: false, false, true

    example

    Returns a boolean where every number in the array is less than 2

    const input = [ [1, 0, 1, 0, 1, 0], [1, 0, 2, 1, 0, 2], [0, 1, 0, 1, 0, 2] ];
    from(input).pipe(every(v => v < 2)).subscribe();
    

    Output: true, false, false

    Type parameters

    • T: unknown

      Type of value contained in the source Array or Set

    Parameters

    Returns OperatorFunction < Iterable < T > , boolean >

    An Observable that emits a boolean when all values in source array return truthy

  • findIndex<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, number>
  • Returns an Observable number which is the index of the first value found in an array using Array.findIndex When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    example

    Returns the index of the first value that is > 2

    const input = [1, 2, 3, 4, 5]
    of(input).pipe(findIndex(v => v > 2)).subscribe();
    

    Output: 2

    example

    Returns the index of the first string where the length < 5

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

    Output: 1

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    Returns OperatorFunction < Iterable < T > , number >

    An Observable that emits a number value, the index of first value where PredicateFn is true

  • indexOf<T>(input: Subscribable<Iterable<T> | T> | Iterable<T> | T, startIndex?: Subscribable<number> | number): OperatorFunction<Iterable<T>, number[]>
  • Returns an Observable Number if the input is a single value, or Array of numbers in the input is an Array. These are the index numbers of first truthy value in the source array using Array.indexOf

    example

    Returns the first index of the word RxJS in the array

    const input = [ ['RxJS', 'Ninja' ], ['Learn', 'RxJS'], ['Foo', 'Bar'] ];
    from(input).pipe(indexOf('RxJS')).subscribe();
    

    Output: 0, 1, -1

    example

    Returns an array of the first index of the words RxJS and Ninja in the array

    const input = [ ['RxJS', 'Ninja' ], ['Learn', 'RxJS'], ['Foo', 'Bar'] ];
    of(input).pipe(indexOf(['RxJS', 'Ninja'])).subscribe()
    

    Output: [0, 1], [1, -1], [-1, -1]

    example

    Returns the first index of the word RxJS in the array starting from index 1

    const input = [ ['RxJS', 'Ninja' ], ['Learn', 'RxJS'], ['Foo', 'Bar'] ];
    of(input).pipe(indexOf('RxJS', 1)).subscribe()
    

    Output: -1, 1, -1

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

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

      A value or array of values to get the index of in the source array

    • Optional startIndex: Subscribable<number> | number

      Optional index to start searching from in the array, starts from 0

    Returns OperatorFunction < Iterable < T > , number [] >

    Observable number or array of numbers containing the index of the first found value

  • isEqualSet<T>(input: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, boolean>
  • Returns an Observable that emits a boolean value if the source Observable Array or Set has equal non-duplicate content of the input Array or Set

    remarks

    The source set (A) is equal in content to the input set (B) (A == B)

    example

    Return if the source array is a subset of the input array

    const input = [ ['a', 'b', 'c'],  ['a', 'c', 'b', 'a'], ['a', 'b', 'z', 'x' ] ];
    from(input).pipe(isEqualSet(['a', 'b', 'c'])).subscribe()
    

    Output: true, true, false

    Type parameters

    • T: unknown

      The input type of the source Array or Set

    Parameters

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

      The Array or Set to check if the set is equal

    Returns OperatorFunction < Iterable < T > , boolean >

    Observable that emits a boolean of the source array has equal content to the input array

  • isSubsetOf<T>(input: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, boolean>
  • Returns an Observable that emits a boolean value if the source Observable Array or Set is a subset of the input Array or Set

    remarks

    The source set (A) is subset of the input set (B) when B contains all elements of A (A ⊆ B)

    example

    Return if the source array is a subset of the input array

    const input = [ ['a', 'c'], b: ['a', 'e'], c: ['x', 'z'] ]
    from(input).pipe(isSubsetOf(['a', 'b', 'c'])).subscribe()
    

    Output: true, false, false

    Type parameters

    • T: unknown

      The input type of the source Array or Set

    Parameters

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

      The Array or Set to check if the value is a subset of it

    Returns OperatorFunction < Iterable < T > , boolean >

    Observable that emits a boolean of the source array being a subset of the input array

  • isSupersetOf<T>(input: Subscribable<Iterable<T>> | Iterable<T>): OperatorFunction<Iterable<T>, boolean>
  • Returns an Observable that emits a boolean value if the source Observable Array or Set is a superset of the input Array or Set

    remarks

    The source set (A) is superset of the input set (B) when A contains all elements of B. (A ⊇ B)

    example

    Return if the source array is a subset of the input array

    const input = [ ['a', 'b', 'c'], b: ['a', 'b', 'e'], c: ['x', 'y', 'z'] ]
    from(input).pipe(isSupersetOf(['a', 'c'])).subscribe()
    

    Output: true, false, false

    Type parameters

    • T: unknown

      The input type of the source Array or Set

    Parameters

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

      The Array or Set to check if the value is a subset of it

    Returns OperatorFunction < Iterable < T > , boolean >

    Observable that emits a boolean of the source array being a superset of the input array

  • lastIndexOf<T>(input: Subscribable<Iterable<T> | T> | Iterable<T> | T, fromIndex?: Subscribable<number> | number): OperatorFunction<Iterable<T>, number[]>
  • Returns an Observable number or array of numbers. These are the index numbers of first truthy value in the source array using Array.lastIndexOf

    example

    Returns the last index of the word RxJS in the array

    const input = [ ['RxJS', 'Ninja', 'RxJS' ], ['Learn', 'RxJS'], ['Foo', 'Bar'] ];
    from(input).pipe(lastIndexOf('RxJS')).subscribe();
    

    Output: 2, 1, -1

    example

    Returns an array of the last index of the words RxJS and Ninja in the array

    const input = [ ['RxJS', 'Ninja', 'RxJS' ], ['Ninja', 'Learn', 'RxJS'], ['Foo', 'Bar'] ];
    of(input).pipe(lastIndexOf(['RxJS', 'Ninja'])).subscribe()
    

    Output: [2, 1], [0, 2], [-1, -1]

    example

    Returns the last index of the word RxJS in the array starting from index 1

    const input = [ ['RxJS', 'Ninja', 'RxJS', 'Ninja', 'Ninja' ], ['Learn', 'RxJS'], ['Foo', 'Bar'] ];
    of(input).pipe(lastIndexOf('RxJS', 1)).subscribe()
    

    Output: 2, 1, -1

    example

    Returns the last index of the word RxJS in the array comparing with lower case

    const input = [ ['RxJS', 'Ninja', 'RxJS'], ['Learn', 'RxJS'], ['Foo', 'Bar'] ];
    of(input).pipe(lastIndexOf('rxjs', 0, v => v.toLowerCase())).subscribe()
    

    Output: 2, 1, -1

    Type parameters

    • T: unknown

      The input type of the source Array or Set

    Parameters

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

      A value or array of values to get the index of in the source array

    • Optional fromIndex: Subscribable<number> | number

      Optional index to start searching from in the array

    Returns OperatorFunction < Iterable < T > , number [] >

    Observable number or array of numbers containing the index of the last found value

  • some<T>(predicate?: PredicateFn<T>): OperatorFunction<Iterable<T>, boolean>
  • Returns an Observable that emits a boolean when all values in the source Array or Set return truthy using Array.some When working with data, if the array contains numbers 0 will be returned as a value to the PredicateFn, but all other falsy values will be ignored

    see

    The filterSome operator returns the array value instead of boolean

    example

    Return a boolean value if some of the elements are truthy

    const input = [ [0, 0, 0], [0, 0, 1], [1, 1, 1] ]
    from(input).pipe(some()).subscribe()
    

    Output: false, true, true

    example

    Return a boolean value if some of the elements are truthy with a predicate

    const input = [ ['RxJS', 'Rocks'], ['RxJS', 'Ninja'], ['Foo', 'Bar'] ]
    fromArray(input).pipe(some(v => v === 'RxJS')).subscribe()
    

    Output: true, true, false

    Type parameters

    • T: unknown

      Item type contained in the Array or Set

    Parameters

    Returns OperatorFunction < Iterable < T > , boolean >

    An Observable that emits a boolean when all values in source array return truthy

Set Functions

  • fromSet<T>(input: Subscribable<Iterable<Set<T>> | Set<T>> | Iterable<Set<T>> | Set<T>): Observable<T[]>
  • Returns an Observable that emits an Array from a Set

    example

    Create Array from Set

    const input = new Set(1, 1, 2, 2, 3, 3, 4, 4);
    fromSet(input).subscribe();
    

    Output: [1, 2, 3, 4]

    Type parameters

    • T: unknown

      The type of value contained in the Set

    Parameters

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

      Input to create the emit values from, can be argument list of Set, an array of Set or an Observable or Promise source

    Returns Observable < T [] >

    Observable that emits an Array from the input Set

  • setToArray<T>(): OperatorFunction<Set<T>, T[]>
  • Returns an Observable that emits an Array from a source Set.

    example

    Convert a Set into an Array

    const input = new Set([1, 1, 2, 3, 3, 4, 5]);
    of(input).pipe(setToArray()).subscribe();
    

    Output: [1, 2, 3, 4, 5]

    Type parameters

    • T: unknown

      The type of value contained in the Set

    Returns OperatorFunction < Set < T > , T [] >

    Observable that emits a Array from a source Set

  • toSet<T>(): OperatorFunction<T[], Set<T>>
  • Returns an Observable that emits a Set from a source Array.

    example

    Convert an Array into a Set

    const input = [1, 1, 2, 3, 3, 4, 5];
    of(input).pipe(toSet()).subscribe();
    

    Output: Set(5) {1, 2, 3, 4, 5}

    Type parameters

    • T: unknown

      The input type of the source Array or Set

    Returns OperatorFunction < T [] , Set < T > >

    Observable that emits a Set from a source Array

Generated using TypeDoc, the 18/11/2022 at 13:22:57