ArrayUtils: {
    average(arr): number;
    arrayEquals<T>(a1, a2): boolean;
    chunks<T>(arr, size): T[][];
    compareArrays<T>(arr1, arr2, options?): boolean;
    deduplicate<T>(array): T[];
    deepEqual(a, b): boolean;
    difference<T>(a, b, getId?): T[];
    findMax<T>(arr): undefined | T;
    findMin<T>(arr): undefined | T;
    findAndUpdate(collection, item): EmbeddedEntity[];
    flatten<T>(arr, depth?): T[];
    groupBy<T>(arr, keyOrFn): Record<string, T[]>;
    hasCommonElement<T>(array1, array2): boolean;
    intersection<T>(arr1, arr2): T[];
    moveItem<T>(arr, from, to): T[];
    random<T>(arr): undefined | T;
    remove<T>(arr, predicate): T[];
    shuffle<T>(arr): T[];
    sortCompare(x, y): number;
    sumArray(arr): number;
    takeFirst<T>(arr, n?): T[];
    takeLast<T>(arr, n?): T[];
    union<T>(...arrays): T[];
} = ...

Collection of array utility functions

Type declaration

  • average:function
    • Calculates the average of all numbers in an array

      Parameters

      • arr: number[]

        Array of numbers to calculate average from

      Returns number

      The average of all numbers, or 0 if array is empty

      Example

      const avg = ArrayUtils.average([1, 2, 3, 4, 5]); // Returns 3
      
  • arrayEquals:function
    • Compares two arrays for equality by sorting and comparing elements

      Type Parameters

      • T extends Comparable

      Parameters

      • a1: T[]

        First array to compare

      • a2: T[]

        Second array to compare

      Returns boolean

      True if arrays contain the same elements in any order

      Example

      const equal = ArrayUtils.arrayEquals([1, 2, 3], [3, 2, 1]); // Returns true
      
  • chunks:function
    • Splits an array into chunks of specified size

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to split into chunks

      • size: number

        Size of each chunk (must be positive)

      Returns T[][]

      Array of arrays, each of size 'size'

      Throws

      Error if size is not positive

  • compareArrays:function
    • Compares two arrays for equality based on the given options

      Type Parameters

      • T

      Parameters

      • arr1: T[]

        First array to compare

      • arr2: T[]

        Second array to compare

      • options: CompareOptions = {}

        Comparison options

      Returns boolean

      True if arrays are equal based on the options

  • deduplicate:function
    • Returns a new array with unique values

      Type Parameters

      • T

      Parameters

      • array: T[]

        Array to make unique

      Returns T[]

      Array with duplicate values removed

  • deepEqual:function
    • Deep equality comparison between two values

      Parameters

      • a: unknown

        First value

      • b: unknown

        Second value

      Returns boolean

      True if values are deeply equal

  • difference:function
    • Returns elements from array a that are not in array b based on id property

      Type Parameters

      • T

      Parameters

      • a: T[]

        First array

      • b: T[]

        Second array

      • getId: ((item) => string | number) = ...

        Optional function to extract id from items

          • (item): string | number
          • Parameters

            • item: T

            Returns string | number

      Returns T[]

      Array of elements from a that are not in b

  • findMax:function
    • Returns the maximum value in an array

      Type Parameters

      • T extends Comparable

      Parameters

      • arr: T[]

        Array of comparable values

      Returns undefined | T

      Maximum value or undefined if array is empty

  • findMin:function
    • Returns the minimum value in an array

      Type Parameters

      • T extends Comparable

      Parameters

      • arr: T[]

        Array of comparable values

      Returns undefined | T

      Minimum value or undefined if array is empty

  • findAndUpdate:function
    • Finds and updates an item in a collection based on id

      Parameters

      • collection: EmbeddedEntity[]

        Collection to update

      • item: EmbeddedEntity

        Item to update

      Returns EmbeddedEntity[]

      Updated collection

      Throws

      Error if item is not found

  • flatten:function
    • Flattens a nested array to a specified depth

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to flatten

      • depth: number = Infinity

        Maximum depth to flatten (default: Infinity)

      Returns T[]

      Flattened array

  • groupBy:function
    • Groups array elements by a key or function

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to group

      • keyOrFn: keyof T | ((item) => string | number)

        Key to group by or function that returns the group key

      Returns Record<string, T[]>

      Object with grouped arrays

  • hasCommonElement:function
    • Checks if two arrays have any common elements

      Type Parameters

      • T

      Parameters

      • array1: T[]

        First array

      • array2: T[]

        Second array

      Returns boolean

      True if arrays share at least one common element

  • intersection:function
    • Returns the intersection of two arrays

      Type Parameters

      • T

      Parameters

      • arr1: T[]

        First array

      • arr2: T[]

        Second array

      Returns T[]

      Array containing elements present in both arrays

  • moveItem:function
    • Moves an item from one position to another in an array

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to modify

      • from: number

        Source index

      • to: number

        Destination index

      Returns T[]

      New array with item moved

      Throws

      Error if indices are out of bounds

  • random:function
    • Returns a random element from the array

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to get element from

      Returns undefined | T

      Random element or undefined if array is empty

  • remove:function
    • Removes elements from an array that match a predicate

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to remove elements from

      • predicate: ((item) => boolean)

        Function that returns true for elements to remove

          • (item): boolean
          • Parameters

            • item: T

            Returns boolean

      Returns T[]

      New array with matching elements removed

  • shuffle:function
    • Returns a new array with elements in random order

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to shuffle

      Returns T[]

      New array with elements in random order

  • sortCompare:function
    • Compares two values for sorting

      Parameters

      • x: Comparable

        First value to compare

      • y: Comparable

        Second value to compare

      Returns number

      -1 if x < y, 0 if equal, 1 if x > y

  • sumArray:function
    • Returns the sum of all numbers in an array

      Parameters

      • arr: number[]

        Array of numbers

      Returns number

      Sum of all numbers

  • takeFirst:function
    • Returns the first n elements of an array

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to get elements from

      • n: number = 1

        Number of elements to get (default: 1)

      Returns T[]

      Array containing the first n elements

  • takeLast:function
    • Returns the last n elements of an array

      Type Parameters

      • T

      Parameters

      • arr: T[]

        Array to get elements from

      • n: number = 1

        Number of elements to get (default: 1)

      Returns T[]

      Array containing the last n elements

  • union:function
    • Returns the union of multiple arrays

      Type Parameters

      • T

      Parameters

      • Rest ...arrays: T[][]

        Arrays to union

      Returns T[]

      Array containing unique elements from all arrays