translocator 
Convert line-column locations to ranges and vice-versa.
var assert = require('assert');
var text = [
'line0'
, 'line1'
].join('\n');
var translocator = require('../')(text);
var locations = { start: { line: 0, column: 2 }, end: { line: 1, column: 3 } };
var range = translocator.range(locations.start, locations.end)
console.dir({ range: range, subtext: text.slice(range[0], range[1]) });
assert.deepEqual(translocator.locations(range), locations);
// => { 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
translocator::index(loc) → {number}
Finds the index in the text that matches the given location.
Parameters:
| Name | Type | Description |
|---|---|---|
loc |
Object | location of format: |
Throws:
Returns:
the index of the given location in the text
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:
Returns:
location of format: { line: number, column: number }
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 |
Throws:
Returns:
locations of the format { start: { line: number, column, number }, end: { line: number, column: number } }
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:
|
to |
Object | end location of format: |
Throws:
Returns:
the range of the locations within the text of format [ start, end ]
generated with docme
License
MIT
