A range between 2 values.

Constructors

  • Create a new range from 2 values.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    Returns Range

    Throws

    If lower is greater than upper

    Example

    const range = new Range(100, 200);
    

Properties

lower: Comparable

The lower bound of this range

upper: Comparable

The upper bound of this range

Methods

  • An iterable that contains this.lower and this.upper.

    Time complexity: O(1)

    Space complexity: O(1)

    Returns IterableIterator<Comparable>

    Example

    const [lower, upper] = range;
    
  • Check whether a given range has the same lower and upper bounds as this one.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • range: Range

      Range to compare

    Returns boolean

    Whether the other range has the same lower and upper bounds as this one

    Example

    const a = new Range(0, 100);
    const b = new Range(0, 100);

    a.equals(a); // true
    a.equals(b); // true

    Example

    const a = new Range(0, 100);
    const b = new Range(150, 150);

    a.equals(b); // true
  • Returns true if value is within this.lower and this.upper, false otherwise. The >= and <= operators are used to compare value.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    Returns boolean

    Example

    const range = new Range(100, 200);

    console.log(range.has(150)); // true
    console.log(range.has(300)); // false
  • Check if this range and a given range have any intersection.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • range: Range

      Range to compare

    Returns boolean

    Whether the ranges intersect

    Example

    const a = new Range(100, 200);
    const b = new Range(150, 250);

    a.intersects(b); // true

    Example

    const a = new Range(100, 200);
    const b = new Range(300, 400);

    a.intersects(b); // false
  • Check whether this range contains the given range

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • range: Range

      Range to compare

    Returns boolean

    Whether this range contains the given range

    Example

    const a = new Range(0, 100);
    const b = new Range(25, 75);

    a.isSubRange(b); // true
    b.isSubRange(a); // false
  • Check whether a given range contains this range.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • range: Range

      Range to compare

    Returns boolean

    Whether this range is contained within the other one

    Example

    const a = new Range(0, 100);
    const b = new Range(25, 75);

    a.isSuperrange(b); // false
    b.isSuperrange(a); // true
  • Create a new range from the first 2 values of an iterable.

    Time complexity: O(1)

    Space complexity: O(1)

    Parameters

    • iterable: Iterable<Comparable>

      The iterable to create a range from

    Returns Range

    A new range

    Example

    const range = Range.from([100, 200]);
    

Generated using TypeDoc