range.js

Provides utility functions for working with text selection ranges.

Exports

Range (class)

constructor(length, location)
Up to two arguments. The first may be an existing range, an NSValue, an NSRange, an array with two indices (length and location), or a number representing the starting location of the range. The second should only be included if the first is a number, and is a number representing the length of the range.

Returns a Range object.

var range = new Range(10, 3);
var range = new Range([10, 3]);
// etc.
location
Int; the starting location of this range
length
Int; the length of the range
limit
Int; the final index of the range (location + length)
rangeValue()
Returns the NSRange for the range.
value()
Returns the NSValue for the range.
string()
Returns the string the range represents in the current text context.
equals(secondRange)
Returns bool; whether the secondRange (or object that can be converted into a range, like an array) has an identical length and location
contains(secondRange)
Returns bool; whether the secondRange (or range-like object) is contained within the range
inside(secondRange)
Returns bool; whether the range is contained within the secondRange
startLine()
Returns Range; the range representing the line that wraps this range.
startLineNumber()
Returns int; the line number for the range that wraps this range’s starting location
endLine()
Returns Range; the range representing the line that wraps the end of this range.
endLineNumber()
Returns int; the line number for the range that wraps this range’s ending location
lineRange()
Returns Range; the range of all lines that contain the range
select()
Void; shortcut method to select the range in the current text context.
log()
Shortcut method to log the range to the console

Range.from(fromRange)

Returns Range; converts fromRange into a Range (if necessary) and returns it.

Extensions

If you require syntax_zone or text_recipe Range objects will receive additional methods for dealing with syntax zones. See syntax_zone.js or text_recipe.js for more info.