Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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


Source:



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 }


Source:



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


Source:



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 ]


Source:



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 }


Source:



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](https://github.com/thlorenz/docme)*

## License

MIT