Type alias Range

Range: JSONEncodable & {
    add: ((value) => Range);
    at: ((index) => number);
    average: (() => number);
    clamp: ((value) => number);
    contains: ((value) => boolean);
    count: number;
    divide: ((value) => Range);
    end: number;
    enumerate: (() => IterableIterator<number>);
    equals: ((range) => boolean);
    id: string;
    indexOf: ((value) => number);
    intersection: ((range) => Range | undefined);
    intersects: ((range) => boolean);
    itemType: "range";
    length: number;
    mapValue: ((n, to) => number);
    multiply: ((value) => Range);
    start: number;
    step: number;
    subtract: ((value) => Range);
    sum: (() => number);
    toArray: (() => number[]);
    toJSON: (() => {
        end: number;
        start: number;
        step: number;
    });
    toString: (() => string);
    transform: ((value_transformer, step_transformer?) => Range);
    union: ((range) => Range);
    wrap: ((value) => number);
}

A range of numbers represented by an object with a start, end, and step, as well as methods for performing operations on the range.

Type declaration

  • add: ((value) => Range)
      • (value): Range
      • Adds a value to the start and end of the range, returning a new range with an adjusted start and end while keeping the same step.

        Parameters

        • value: number

          The value to add.

        Returns Range

        The added range.

  • at: ((index) => number)
      • (index): number
      • Gets the value at the specified index in the range.

        Parameters

        • index: number

          The index of the value to get.

        Returns number

        The value at the specified index.

  • average: (() => number)
      • (): number
      • Gets the average of the values in the range.

        Returns number

  • clamp: ((value) => number)
      • (value): number
      • Clamps a value to the range. If the value is less than the start of the range, the start of the range is returned. If the value is greater than the end of the range, the end of the range is returned. Otherwise, the value is returned.

        Parameters

        • value: number

          The value to clamp.

        Returns number

        The clamped value.

  • contains: ((value) => boolean)
      • (value): boolean
      • Checks whether the range contains a value or another range.

        Parameters

        • value: number | Range

          The value or range to check for containment.

        Returns boolean

        true if the range contains the value or range and false otherwise.

  • count: number

    The number of values in the range.

  • divide: ((value) => Range)
      • (value): Range
      • Divides the range by a value, returning a new range with an adjusted start, end, and step.

        Parameters

        • value: number

          The value to divide by.

        Returns Range

        The divided range.

  • end: number

    The end value of the range.

  • enumerate: (() => IterableIterator<number>)
      • (): IterableIterator<number>
      • Gets an iterator over the values in the range.

        Returns IterableIterator<number>

        An iterator over the values in the range.

  • equals: ((range) => boolean)
      • (range): boolean
      • Checks whether the range is equal to another range.

        Parameters

        • range: Range

          The range to compare to.

        Returns boolean

        true if the ranges are equal and false otherwise.

  • id: string

    The unique ID of the range.

  • indexOf: ((value) => number)
      • (value): number
      • Gets the index of a value in the range.

        Parameters

        • value: number

          The value to get the index of.

        Returns number

        The index of the value or -1 if the value is not in the range.

  • intersection: ((range) => Range | undefined)
      • (range): Range | undefined
      • Gets the intersection of the range with another range.

        Parameters

        • range: Range

          The range to intersect with.

        Returns Range | undefined

        The intersection of the ranges or undefined if the ranges do not intersect.

  • intersects: ((range) => boolean)
      • (range): boolean
      • Checks whether the range intersects with another range.

        Parameters

        • range: Range

          The range to check for intersection with.

        Returns boolean

        true if the ranges intersect and false otherwise.

  • itemType: "range"

    An identifier for the object type.

  • length: number

    The difference between the start and end values.

  • mapValue: ((n, to) => number)
      • (n, to): number
      • Maps a value from the range to a value in another range.

        Parameters

        • n: number

          The value to map.

        • to: Range

          The range to map to.

        Returns number

        The mapped value.

        Example

        const range1 = range(5, 10);
        const range2 = range(0, 100);
        expect(range1.mapValue(6, range2)).toBe(20);
  • multiply: ((value) => Range)
      • (value): Range
      • Multiplies the range by a value, returning a new range with an adjusted start, end, and step.

        Parameters

        • value: number

          The value to multiply by.

        Returns Range

        The multiplied range.

  • start: number

    The start value of the range.

  • step: number

    The step between values in the range. Defaults to 1.

  • subtract: ((value) => Range)
      • (value): Range
      • Subtracts a value from the start and end of the range, returning a new range with an adjusted start and end while keeping the same step.

        Parameters

        • value: number

          The value to subtract.

        Returns Range

        The subtracted range.

  • sum: (() => number)
      • (): number
      • Gets the sum of all values in the range.

        Returns number

  • toArray: (() => number[])
      • (): number[]
      • Gets the range's values as an array.

        Returns number[]

        The range as an array.

  • toJSON: (() => {
        end: number;
        start: number;
        step: number;
    })
      • (): {
            end: number;
            start: number;
            step: number;
        }
      • Gets the range as a JSON-encodable object.

        Returns {
            end: number;
            start: number;
            step: number;
        }

        The range as a JSON object.

        • end: number
        • start: number
        • step: number
  • toString: (() => string)
      • (): string
      • Gets the range as a string in the format [start, end, step].

        Returns string

        The range as a string.

  • transform: ((value_transformer, step_transformer?) => Range)
      • (value_transformer, step_transformer?): Range
      • Applies a transform function to the range.

        Parameters

        • value_transformer: ((value) => number)

          The function to apply to the start and end values.

            • (value): number
            • Parameters

              • value: number

              Returns number

        • Optional step_transformer: ((step) => number)

          The function to apply to the step. Optional. If not provided, the step will not be transformed.

            • (step): number
            • Parameters

              • step: number

              Returns number

        Returns Range

        The transformed range.

        Example

        const range1 = range(0, 10);
        expect(range1.transform((value) => value * 2, (step) => step * 2).equals(range(0, 20, 2))).toBe(true);
  • union: ((range) => Range)
      • (range): Range
      • Gets the union of the range with another range. The union is the smallest range that contains both ranges. The step size of the ranges must be the same.

        Parameters

        • range: Range

          The range to union with.

        Returns Range

        The union of the ranges.

        Throws

        An error if the ranges have different step sizes.

  • wrap: ((value) => number)
      • (value): number
      • Wraps a value to the range. The result is the value modulo the length of the range.

        Parameters

        • value: number

          The value to wrap.

        Returns number

        The wrapped value.

Generated using TypeDoc