Range

A Range represents a contiguous, linear region of an element, specified by a start and end index. Most often it is used to indicate sections of a text stream.

The Range class is not subclassable.

Class Methods

constructor(start, end)

Creates a new Range with a provided start and end index. Raises an Error if the end index precedes the start index.

Properties

start

The start index of the range, as a Number.

This property is readonly.

end

The end index of the range, as a Number.

This property is readonly.

length

The length of the range, as a Number. This is equivalent to subtracting start from end.

This property is readonly.

empty

A Boolean indicating whether the range is empty (its start and end indices are the same).

This property is readonly.

Methods

isEqual(range)

Returns true if the receiver is equal to another provided range, false otherwise.

compare(range)

Returns a Number indicating how a provided range compares to the receiver in sort order. The return value will be -1 if the receiver’s start index precedes the other’s, or if the same, if its length is shorter. The return value will be 1 if the opposite is true. The return value will be 0 if the ranges are equal.

containsRange(range)

Returns true if the receiver fully contains another provided range, false otherwise.

containsIndex(index)

Returns true if the receiver contains a provided index (Number), false otherwise.

union(range)

Returns a new Range representing a union of the receiver and a provided range.

intersection(range)

Returns a new Range representing an intersection of the receiver and a provided range. If the two ranges to not intersect, the returned range will have zero start and end indices.

intersectsRange(range)

Returns true if the receiver intersects a provided range (shares at least one index), false otherwise.