Options
All
  • Public
  • Public/Protected
  • All
Menu

Module Common

Index

Functions

all

  • all<T>(collection: Iterable<T>, predicate: (collectionElement: T) => boolean): boolean
  • Checks if all elements of the collection return true for the provided predicate.

    example

    Used on an array where all values match the predicate:

    import { all } from '@hubhazard/core';
    const collection = [6, 74, 43, 331, 20];
    const result = all(collection, element => element > 5);
    // result equals true
    example

    Used on a set with at least one value not matching the predicate:

    import { all } from '@hubhazard/core';
    const collection = new Set([30, 21, 6, 100392]);
    const isEven = (value) => value % 2 === 0;
    const result = all(collection, isEven);
    // result equals false

    Type parameters

    • T

    Parameters

    • collection: Iterable<T>

      A collection of which values will be tested by the predicate.

    • predicate: (collectionElement: T) => boolean

      A function that for each collection item needs to return a boolean value. Similar to Javascript's filter().

        • (collectionElement: T): boolean
        • Parameters

          • collectionElement: T

          Returns boolean

    Returns boolean

any

  • any<T>(collection: Iterable<T>, predicate: (collectionElement: T) => boolean): boolean
  • Check if any element of the collection returns true for the provided predicate.

    example

    Used on an array where all values match the predicate:

    import { any } from '@hubhazard/core';
    const collection = ['three', 'three', 'three', 'three', 'three'];
    const result = any(collection, element => element === 'one');
    // result equals true
    example

    Used on a set where only one value matches the predicate:

    import { any } from '@hubhazard/core';
    const collection = new Set(['three', 'three', 'one', 'three']);
    const result = any(collection, element => element === 'one');
    // result equals true

    Type parameters

    • T

    Parameters

    • collection: Iterable<T>

      A collection of which values will be tested by the predicate.

    • predicate: (collectionElement: T) => boolean

      A function that for each collection item needs to return a boolean value. Similar to Javascript's filter().

        • (collectionElement: T): boolean
        • Parameters

          • collectionElement: T

          Returns boolean

    Returns boolean

delay

  • delay(ms: number): Promise<void>
  • An async version of node's setTimeout. Time is specified in milliseconds.

    example
    import { delay } from '@hubhazard/core';
    await doSomething();
    await delay(3000); // Wait 3 seconds
    await doSomething();

    Parameters

    • ms: number

    Returns Promise<void>

doTimes

  • doTimes<T>(times: number, action: (index: number) => T): T[]
  • Performs provided action specified number of times. An index of the iteration is passed to the action. If the action returns a value, the function will returns an array of returned values.

    example

    Generate a list of values:

    import { doTimes } from '@hubhazard/core';
    const collection = doTimes(5, () => 'hello');
    // collection: ['hello', 'hello', 'hello', 'hello', 'hello']
    example

    Use the iteration's index number:

    import { doTimes } from '@hubhazard/core';
    const numbers = doTimes(6, index => index);
    // collection: [0, 1, 2 ,3 ,4, 5]
    example

    Perform an action 3 times:

    import { doTimes } from '@hubhazard/core';
    doTimes(3, index => {
      console.log(`Iteration #${index}`);
    });
    // Console output:
    // Iteration #0
    // Iteration #1
    // Iteration #2

    Type parameters

    • T

    Parameters

    • times: number

      Number of times to call the action.

    • action: (index: number) => T

      A function called times times. It can optionally receive an iteration number in form of an index argument (starts from 0). Can return a value.

        • (index: number): T
        • Parameters

          • index: number

          Returns T

    Returns T[]

    An array of values returned by performed actions. If actions don't return anything, the void[] will be returned.

pickRandom

  • pickRandom<T>(collection: T[]): T | undefined
  • Pick random element of provided collection. Will return undefined if collection is empty or invalid.

    example
    import { pickRandom } from '@hubhazard/core';
    const collection = ['one', 'two', 'three', 'four'];
    const result = pickRandom(collection);

    Type parameters

    • T

    Parameters

    • collection: T[]

    Returns T | undefined

randomIntRange

  • randomIntRange(min: number, max: number): number
  • Returns a random integer between min and max values. Both min and max are inclusive.

    example
    import { randomIntRange } from '@hubhazard/core';
    const x = randomIntRange(1, 5);
    // Possible x values: 1, 2, 3, 4, 5

    Parameters

    • min: number
    • max: number

    Returns number

repeat

  • repeat(times: number): null[]
  • Create an array of null elements. The array will contain specified number of elements. If specified number is lesser or equal to 0 empty array will be returned. Non-integer numbers will be rounded down.

    deprecated

    Replaced by doTimes. There's no need for this function.

    example
    import { repeat } from '@hubhazard/core';
    const collection = repeat(5).map(() => 'hello');
    // collection: ['hello', 'hello', 'hello', 'hello', 'hello']

    Parameters

    • times: number

    Returns null[]

Generated using TypeDoc