translocator
Convert line-column locations to ranges and vice-versa.
var assert = ; var text = 'line0' 'line1'; var translocator = text; var locations = start: line: 0 column: 2 end: line: 1 column: 3 ;var range = translocator consoledir range: range subtext: text ; assert; // => { range: [ 2, 9 ], subtext: 'ne0\nlin' }
Installation
npm install translocator
API
-
translocator(text) → {Translocator}
-
Creates a translocator for the given text.
Parameters:
Name Type Description text
string Returns:
translocator
- Type
- Translocator
-
translocator::index(loc) → {number}
-
Finds the index in the text that matches the given location.
Parameters:
Name Type Description loc
Object location of format:
{ line: number, column: number }
Throws:
-
when the given location is outside of the text
- Type
- Error
Returns:
the index of the given location in the text
- Type
- number
-
-
translocator::location(index) → {Object}
-
Finds the location of the character in the text at the given index.
Parameters:
Name Type Description index
number the index of the character in the text to locate
Throws:
-
when the given index is outside of the text
- Type
- Error
Returns:
location of format:
{ line: number, column: number }
- Type
- Object
-
-
translocator::locations(range) → {Object}
-
Finds the start and end locations for the given range
Parameters:
Name Type Description range
Array.<number> start and end indexes of the format
[ start, end ]
Throws:
-
when the given index is outside of the text
- Type
- Error
Returns:
locations of the format
{ start: { line: number, column, number }, end: { line: number, column: number } }
- Type
- Object
-
-
translocator::range(locs, to) → {Arrary.<number>}
-
Finds start and end index for the given locations.
Parameters:
Name Type Description locs
Object start and end locationx of format:
{ start: { line: number, column, number }, end: { line: number, column: number } }
to
Object end location of format:
{ line: number, column: number }
Throws:
-
when the given location is outside of the text
- Type
- Error
Returns:
the range of the locations within the text of format
[ start, end ]
- Type
- Arrary.<number>
-
generated with docme
License
MIT