Options
All
  • Public
  • Public/Protected
  • All
Menu

@spicy-hooks/utils

API reference

This is a detailed API reference of the @spicy-hooks/utils package.

For high-level information about this package and installation instructions, please see our GitHub repository.

Index

Type aliases

Comparison Functions

Other Functions

Test Helper Functions

Type Guard Functions

Type aliases

KeyOf

KeyOf<R, T>: T

A safer variant for defining a type for a sub-set of object keys.

Given a following interface:

interface Person {
  name: string;
  age: number;
  title: string;
}

a type for any key of that interface would be:

type PersonKey = keyof Person;

For a subset of keys you would do this:

type ModifiableKey = 'name' | 'title';

However that doesn't provide any immediate validation that name and title are in fact keys of the Person interface.

The KeyOf helper resolves this:

type ModifiableKey1 = KeyOf<Person, 'name' | 'title'>; // all OK
type ModifiableKey2 = KeyOf<Person, 'name' | 'title' | 'citizenship'>; // TS2344: Type '"name" | "title" | "citizenship"' does not satisfy...

Type parameters

  • R: Record<any, any>

    record containing the properties

  • T: keyof R

    selected key(s) of R

Nullishable

Nullishable<T>: T | null | undefined

Helper type for extending a type with | null | undefined.

Type parameters

  • T

    the raw type to be extended

Comparison Functions

isEqualArray

  • isEqualArray<T>(arrayA: T | Readonly<T> | null | undefined, arrayB: T | Readonly<T> | null | undefined): boolean
  • Checks whether the arrays are shallowly equal.

    Type parameters

    • T: any[]

      type of both arrays

    Parameters

    • arrayA: T | Readonly<T> | null | undefined

      first array to compare

    • arrayB: T | Readonly<T> | null | undefined

      second array to compare

    Returns boolean

isShallowEqual

  • isShallowEqual<T>(objectA: T, objectB: T): boolean
  • Checks whether the values are shallowly equal. Supports primitives, arrays and objects.

    Type parameters

    • T

      type of both objects

    Parameters

    • objectA: T

      first value to compare

    • objectB: T

      second value to compare

    Returns boolean

Other Functions

mergeArrays

  • mergeArrays<T>(originalArray: T, sparseArray: Partial<T>): T
  • Merges each index of an array separately, while keeping the original value if the new one is undefined.

    Note that extra elements in the sparseArray are ignored.

    Example:

    const mergedArray = mergeArrays([1,2,3], [9,undefined,10])
    // mergedArray === [9,2,10]

    Type parameters

    • T: any[]

    Parameters

    • originalArray: T

      original tuple

    • sparseArray: Partial<T>

      new sparse array with undefined values as placeholders

    Returns T

Test Helper Functions

latency

  • latency(minTimeout: number, maxTimeout: number, logMessage?: undefined | string): Promise<void>
  • latency(timeout: number, logMessage?: undefined | string): Promise<void>
  • Simulates latency by resolving the returned promise after randomly chosen delay constrained with minTimeout and maxTimeout.

    Parameters

    • minTimeout: number

      minimum number of milliseconds to wait

    • maxTimeout: number

      maximum number of milliseconds to wait

    • Optional logMessage: undefined | string

      this message when provided, will be logged into the console along with the actual delay

    Returns Promise<void>

  • Simulates latency by resolving the returned promise after exactly timeout milliseconds.

    category

    Test Helper

    Parameters

    • timeout: number

      number of milliseconds to wait

    • Optional logMessage: undefined | string

      this message when provided, will be logged into the console along with the actual delay

    Returns Promise<void>

Type Guard Functions

isFunction

  • isFunction<F>(valueOrFunction: F | Exclude<any, Function>): valueOrFunction is F
  • Checks whether the valueOrFunction is a function.

    Type parameters

    • F: Function

      type of the function

    Parameters

    • valueOrFunction: F | Exclude<any, Function>

      value that could be either a function of the specific type or any non-function value

    Returns valueOrFunction is F

isTruthy

  • isTruthy<T>(value: T): value is Exclude<T, false | null | undefined | "" | 0>
  • An alternative to Boolean for usage in [].filter(). In addition to the actual filtering it also ensures that the result of .filter() operation is typed properly - i.e. it skips false | null | undefined | '' | 0 from the input type.

    Type parameters

    • T

      type of the value

    Parameters

    • value: T

      value that could eventually be falsy

    Returns value is Exclude<T, false | null | undefined | "" | 0>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc