Skip to content

Commit

Permalink
Fix naming to use toPoint
Browse files Browse the repository at this point in the history
Closes GH-5.
Closes GH-6.

Reviewed-by: Christian Murphy <[email protected]>
  • Loading branch information
wooorm authored Aug 21, 2020
1 parent ca9982d commit ef0ea9a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 45 deletions.
30 changes: 16 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ module.exports = factory

function factory(file) {
var contents = indices(String(file))
var toPoint = offsetToPointFactory(contents)

return {
toPosition: offsetToPositionFactory(contents),
toOffset: positionToOffsetFactory(contents)
toPoint: toPoint,
toPosition: toPoint,
toOffset: pointToOffsetFactory(contents)
}
}

// Factory to get the line and column-based `position` for `offset` in the bound
// Factory to get the line and column-based `point` for `offset` in the bound
// indices.
function offsetToPositionFactory(indices) {
return offsetToPosition
function offsetToPointFactory(indices) {
return offsetToPoint

// Get the line and column-based `position` for `offset` in the bound indices.
function offsetToPosition(offset) {
// Get the line and column-based `point` for `offset` in the bound indices.
function offsetToPoint(offset) {
var index = -1
var length = indices.length

Expand All @@ -39,16 +41,16 @@ function offsetToPositionFactory(indices) {
}
}

// Factory to get the `offset` for a line and column-based `position` in the
// Factory to get the `offset` for a line and column-based `point` in the
// bound indices.
function positionToOffsetFactory(indices) {
return positionToOffset
function pointToOffsetFactory(indices) {
return pointToOffset

// Get the `offset` for a line and column-based `position` in the bound
// Get the `offset` for a line and column-based `point` in the bound
// indices.
function positionToOffset(position) {
var line = position && position.line
var column = position && position.column
function pointToOffset(point) {
var line = point && point.line
var column = point && point.column

if (!isNaN(line) && !isNaN(column) && line - 1 in indices) {
return (indices[line - 2] || 0) + column - 1 || 0
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"virtual",
"file",
"location",
"point",
"position",
"offset"
],
Expand Down Expand Up @@ -40,9 +41,9 @@
"remark-cli": "^8.0.0",
"remark-preset-wooorm": "^7.0.0",
"tape": "^5.0.0",
"tinyify": "^2.0.0",
"tinyify": "^3.0.0",
"vfile": "^4.0.0",
"xo": "^0.32.0"
"xo": "^0.33.0"
},
"scripts": {
"format": "remark . -qfo && prettier . --write && xo --fix",
Expand Down
23 changes: 11 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

Convert between positions (line and column-based) and offsets (range-based)
Convert between positional (line and column-based) and offsets (range-based)
locations in a [virtual file][vfile].

## Install
Expand All @@ -28,7 +28,7 @@ var vfileLocation = require('vfile-location')
var location = vfileLocation(vfile('foo\nbar\nbaz'))

var offset = location.toOffset({line: 3, column: 3}) // => 10
location.toPosition(offset) // => {line: 3, column: 3, offset: 10}
location.toPoint(offset) // => {line: 3, column: 3, offset: 10}
```

## API
Expand All @@ -37,18 +37,17 @@ location.toPosition(offset) // => {line: 3, column: 3, offset: 10}

Get transform functions for the given `doc` (`string`) or [`file`][vfile].

Returns an object with [`toOffset`][to-offset] and [`toPosition`][to-position].
Returns an object with [`toOffset`][to-offset] and [`toPoint`][to-point].

### `location.toOffset(position)`
### `location.toOffset(point)`

Get the `offset` (`number`) for a line and column-based [`position`][position]
in the bound file.
Get the `offset` (`number`) for a line and column-based [`point`][point] in the
bound file.
Returns `-1` when given invalid or out of bounds input.

### `location.toPosition(offset)`
### `location.toPoint(offset)`

Get the line and column-based [`position`][position] for `offset` in the bound
file.
Get the line and column-based [`point`][point] for `offset` in the bound file.

## Contribute

Expand Down Expand Up @@ -108,8 +107,8 @@ abide by its terms.

[vfile]: https://github.com/vfile/vfile

[to-offset]: #locationtooffsetposition
[to-offset]: #locationtooffsetpoint

[to-position]: #locationtopositionoffset
[to-point]: #locationtopointoffset

[position]: https://github.com/syntax-tree/unist#position
[point]: https://github.com/syntax-tree/unist#point
20 changes: 10 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test('location()', function (t) {
t.equals(
typeof location.toOffset,
'function',
'should expose `toPosition` for `doc`'
'should expose `toPoint` for `doc`'
)

location = vfileLocation(vfile())
Expand All @@ -30,10 +30,10 @@ test('location()', function (t) {
t.equals(
typeof location.toOffset,
'function',
'should expose `toPosition` for `file`'
'should expose `toPoint` for `file`'
)

t.test('location.toOffset(position)', function (st) {
t.test('location.toOffset(point)', function (st) {
var location = vfileLocation('foo\nbar\nbaz')

st.equals(location.toOffset({}), -1, 'should return `-1` for invalid input')
Expand Down Expand Up @@ -65,31 +65,31 @@ test('location()', function (t) {
st.end()
})

t.test('location.toPosition(offset)', function (st) {
t.test('location.toPoint(offset)', function (st) {
var location = vfileLocation('foo\nbar\nbaz')

st.deepEquals(
location.toPosition(-1),
location.toPoint(-1),
{},
'should return an empty object for invalid input'
)

st.deepEquals(
location.toPosition(12),
location.toPoint(12),
{},
'should return an empty object for out of bounds input'
)

st.deepEquals(
location.toPosition(0),
location.toPoint(0),
{line: 1, column: 1, offset: 0},
'should return a position (#1)'
'should return a point (#1)'
)

st.deepEquals(
location.toPosition(11),
location.toPoint(11),
{line: 3, column: 4, offset: 11},
'should return a position (#2)'
'should return a point (#2)'
)

st.end()
Expand Down
14 changes: 9 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import {Point} from 'unist'
import {VFile} from 'vfile'

declare namespace vfileLocation {
type Position = Pick<Point, 'line' | 'column'>
type PositionWithOffset = Required<Point>
type PositionalPoint = Pick<Point, 'line' | 'column'>
type FullPoint = Required<Point>
type Offset = NonNullable<Point['offset']>
/** @deprecated */
type Position = PositionalPoint

interface Location {
/**
* Get the offset for a line and column based position in the bound file.
* Returns `-1` when given invalid or out of bounds input.
*/
toOffset: (position: Position) => Offset
toOffset: (point: PositionalPoint) => Offset

/**
* Get the line and column-based position for offset in the bound file.
* Get the line and column-based point for offset in the bound file.
*/
toPosition: (offset: Offset) => PositionWithOffset
toPoint: (offset: Offset) => FullPoint
/** @deprecated */
toPosition: (offset: Offset) => FullPoint
}
}

Expand Down
4 changes: 2 additions & 2 deletions types/vfile-location-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ location.toOffset(12)
// $ExpectError
location.toOffset({line: 1, column: 1, offset: 1})

location.toPosition(1)
location.toPoint(1)
// $ExpectError
location.toPosition({line: 1, column: 1})
location.toPoint({line: 1, column: 1})

0 comments on commit ef0ea9a

Please sign in to comment.