@oazmi/kitchensink - v0.9.13
    Preparing search index...

    Function rangeArray

    • generate a numeric array with sequentially increasing value, within a specific range interval. similar to python's range function.

      however, unlike python's range, you must always supply the starting index and the ending index, even if the start index is supposed to be 0, you cannot substitute the first argument with the ending index. only the step argument is optional. moreover, the step argument must always be a positive number.

      Note

      there is also an iterator generator variant of this function that is also capable of indefinite sequences. check out rangeIterator for details.

      Parameters

      • start: number

        the initial number to begin the output range sequence from.

      • end: number

        the final exclusive number to end the output range sequence at. its value will not be in the output array.

      • step: number = 1

        a positive number, dictating how large each step from the start to the end should be. for safety, so that a user doesn't run into an infinite loop by providing a negative step value, we always take the absolute value of this parameter. defaults to 1.

      • decimal_precision: number = 6

        an integer that specifies the number of decimal places to which the output numbers should be rounded to, in order to nullify floating point arithmetic inaccuracy. for instance, in javascript 0.1 + 0.2 = 0.30000000000000004 instead of 0.3. now, you'd certainly not want to see this kind of number in our output, which is why we round it so that it becomes 0.3. defaults to 6 (6 decimal places; i.e. rounds to the closest micro-number (10**(-6))).

      Returns number[]

      a numeric array with sequentially increasing value from the start to the end interval, with steps of size step.

      import { assertEquals } from "jsr:@std/assert"

      // aliasing our functions for brevity
      const
      fn = rangeArray,
      eq = assertEquals

      eq(fn(0, 5), [0, 1, 2, 3, 4])
      eq(fn(-2, 3), [-2, -1, 0, 1, 2])
      eq(fn(2, 7), [2, 3, 4, 5, 6])
      eq(fn(2, 7.1), [2, 3, 4, 5, 6, 7])
      eq(fn(0, 1, 0.2), [0, 0.2, 0.4, 0.6, 0.8])
      eq(fn(0, 100, 20), [0, 20, 40, 60, 80])
      eq(fn(2, -3), [2, 1, 0, -1, -2])
      eq(fn(2, -7, 2), [2, 0, -2, -4, -6])
      eq(fn(2, -7, -2), [2, 0, -2, -4, -6]) // as a protective measure, only the `abs(step)` value is ever taken.
      eq(fn(2, 7, -1), [2, 3, 4, 5, 6]) // as a protective measure, only the `abs(step)` value is ever taken.