@luolapeikko/ts-common
    Preparing search index...

    Class ArrayCore

    Array core functions

    v1.0.0

    Index

    Methods

    • Assert that a value is an array

      Parameters

      • value: unknown

        The value to check

      Returns asserts value is AnyArrayType<unknown>

      If the value is not an array

      v1.0.0

    • Assert that a value is NOT an array

      Type Parameters

      • T

      Parameters

      • value: unknown

        The value to check

      Returns asserts value is Exclude<T, AnyArrayType<unknown>>

      If the value is an array

      v1.0.0

    • Async filter version of Array.prototype.filter(), with the same signature as its synchronous counterpart.

      Type Parameters

      • T

        The type of the array

      Parameters

      • list: Loadable<Iterable<T, any, any>>

        The iterable to filter

      • asyncPredicate: (item: T, index: number, array: T[]) => boolean | Promise<boolean>

        The predicate function

      Returns Promise<T[]>

      A promise that resolves to the filtered array

      ArrayCore.asyncFilter([1, 2, 3], (item) => Promise.resolve(item % 2 === 0)); // [2]
      

      v1.0.0

    • Builds an type error Invalid Iterable: ${JSON.stringify(value)}.

      Parameters

      • value: unknown

        The invalid value.

      Returns TypeError

      The created error.

      v1.0.0

    • Array filter function with iterable which can be also iterable from a function callback.

      Type Parameters

      • T

        The type of the array

      Parameters

      • list: Iterable<T, any, any> | (() => Iterable<T>)

        The iterable to filter

      • predicate: (item: T, index: number, array: T[]) => boolean

        The predicate function

      Returns T[]

      The filtered array

      ArrayCore.filter(() => [1, 2, 3], (item) => item % 2 === 0); // [2]
      

      v1.0.0

    • Type guard to check if a value is an array

      Parameters

      • array: unknown

        The value to check

      Returns array is AnyArrayType<unknown>

      True if the value is an array; otherwise, false

      v1.0.0

    • Type guard to check if an array is empty

      Parameters

      • array: unknown

        The array to check

      Returns array is []

      True if the array is empty, false otherwise

      v1.0.0

    • Type guard to check if a value is not an array

      Type Parameters

      • T

      Parameters

      • value: unknown

        The value to check

      Returns value is Exclude<T, AnyArrayType<unknown>>

      True if the value is not an array; otherwise, false

      v1.0.0

    • Type guard to check if an array is not empty

      Type Parameters

      • T

      Parameters

      • array: unknown

        The array to check

      Returns array is NonEmptyArray<T>

      True if the array is not empty, false otherwise

      v1.0.0

    • Array map function with overload for NonEmptyArray

      Type Parameters

      Parameters

      Returns NonEmptyArray<Target>

      Mapped array

      const data = [{value: 'value'}] as const;
      const result1: NonEmptyReadonlyArray<'value'> = arrayMap(data, (value) => value.value); // pick type from data
      const result2: NonEmptyReadonlyArray<'value'> = arrayMap<'value', typeof data>(data, (value) => value.value); // enforce output type

      v1.0.0

    • Array map function with overload for NonEmptyArray

      Type Parameters

      • Target

        The type of the array to map to

      • Source extends unknown[] | readonly unknown[]

        The type of the array to map from

      Parameters

      Returns Target[]

      Mapped array

      const data = [{value: 'value'}] as const;
      const result1: NonEmptyReadonlyArray<'value'> = arrayMap(data, (value) => value.value); // pick type from data
      const result2: NonEmptyReadonlyArray<'value'> = arrayMap<'value', typeof data>(data, (value) => value.value); // enforce output type

      v1.0.0