Function ensureLength

Returns a copy of data with specified length. If the input array is too long, it is truncated.

If the input array is too short, it will be expanded based on the expand strategy:

  • 'undefined': fill with undefined
  • 'repeat': repeat array elements, starting from position 0
  • 'first': repeat with first element from data
  • 'last': repeat with last element from data
import { Arrays } from 'https://unpkg.com/ixfx/dist/data.js';

Arrays.ensureLength([1,2,3], 2); // [1,2]
Arrays.ensureLength([1,2,3], 5, `undefined`); // [1,2,3,undefined,undefined]
Arrays.ensureLength([1,2,3], 5, `repeat`); // [1,2,3,1,2]
Arrays.ensureLength([1,2,3], 5, `first`); // [1,2,3,1,1]
Arrays.ensureLength([1,2,3], 5, `last`); // [1,2,3,3,3]
  • Type Parameters

    • V

      Type of array

    Parameters

    • data: readonly V[] | V[]

      Input array to expand

    • length: number

      Desired length

    • expand:
          | "undefined"
          | "repeat"
          | "last"
          | "first" = ...

      Expand strategy

    Returns V[]