Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thlorenz/translocator
Convert line-column locations to ranges and vice-versa.
https://github.com/thlorenz/translocator
Last synced: 26 days ago
JSON representation
Convert line-column locations to ranges and vice-versa.
- Host: GitHub
- URL: https://github.com/thlorenz/translocator
- Owner: thlorenz
- License: mit
- Created: 2014-01-31T16:09:27.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-01-31T22:00:16.000Z (almost 11 years ago)
- Last Synced: 2024-05-08T17:30:27.925Z (8 months ago)
- Homepage:
- Size: 156 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# translocator [![build status](https://secure.travis-ci.org/thlorenz/translocator.png)](http://travis-ci.org/thlorenz/translocator)
[![testling badge](https://ci.testling.com/thlorenz/translocator.png)](https://ci.testling.com/thlorenz/translocator)
Convert line-column locations to ranges and vice-versa.
```js
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
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
ErrorReturns:
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
ErrorReturns:
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
ErrorReturns:
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
ErrorReturns:
the range of the locations within the text of format
[ start, end ]
Type
Arrary.<number>*generated with [docme](https://github.com/thlorenz/docme)*
## License
MIT