Range ‹ Chocolat API

Range(loc, len)

Creates a new Range.

  • loc Number – the starting location (or index) of the range.
  • len Number – the length of the range.

Note

If given one argument, then the location is 0 and the length is the given argument. i.e. new Range(42) is the same as new Range(0, 42);.

.isValid(documentLength)

Is the range valid?

  • documentLength Number – (optional) check that the indexes of this range are in [0, documentLength).

Returns Boolean – whether the range is valid.

  • To be valid, both the length and location must be >= 0.

  • length, location and length + location must all be "small" (less than 2^30).

  • If documentLength is specified, location + length must not exceed it.

.isEmpty()

Is the range's length 0?

Returns Boolean – whether the range is empty.

.max()

Returns the end of the range (i.e. location + length)

Returns Number – the end of the range.

.min()

Returns the range's location

Returns Number – the location of the range.

.lastIndex()

Returns the last index in the range (length + location - 1), or location if the range is empty.

Returns Number – the last index in the range.

.hasIndex(idx)

Test whether the range has a given index. If the range is empty (length is 0), then returns false.

  • idx Number – the index.

Returns Boolean – true if the range contains the given index.

.containsIndex(idx)

Test whether the range has a given index. If the range is empty (length is 0), then it treats it as a range of length == 1.

  • idx Number – the index.

Returns Boolean – true if the range contains the given index.

.overlapsWith(rng)

Checks if two ranges's indexes overlap. If either range is empty, returns false.

  • rng Range – a range.

Returns Boolean – whether the given range is overlaps with this range.

.intersectsWith(rng)

Checks if two ranges's indexes overlap. Considers empty ranges as if they were of length 1.

  • rng Range – a range.

Returns Boolean – whether the given range intersects with this range.

.containsRange(rng)

Checks if all indexes of a given range are contained in this range.

  • rng Range – a range.

Returns Boolean – whether the given range is contained within this range.

.equals(rng)

Checks if both ranges are equal (treats any two invalid ranges as equal).

  • rng Range – a range.

Returns Boolean – whether the given range is equal to this range.

.toString()

Returns a string in the form of {loc, len}. For debugging only, do not attempt to parse this string.

Returns String – a string representation of the range.

Range.compare(a, b)

Compare first by location, then by length. Treats any two invalid ranges as equal.

  • a Range – a range.
  • b Range – another range.

Returns Number-1 if a < b, 1 if a > b or 0 if a and b are equal.