@jonahsnider/util

    Class Range

    A range between 2 values.

    Index

    Constructors

    • Create a new range from 2 values.

      Time complexity: O(1)

      Space complexity: O(1)

      Parameters

      Returns Range

      If lower is greater than upper

      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>

      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

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

      a.equals(a); // true
      a.equals(b); // true
      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

      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

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

      a.intersects(b); // true
      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

      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

      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

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