diff --git a/index.js b/index.js index 2d7c21c..b7b74dd 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 diff --git a/package.json b/package.json index 3ff8e69..a951dc9 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "virtual", "file", "location", + "point", "position", "offset" ], @@ -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", diff --git a/readme.md b/readme.md index e1b50f6..ea7cd42 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test.js b/test.js index 84dc98d..219f206 100644 --- a/test.js +++ b/test.js @@ -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()) @@ -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') @@ -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() diff --git a/types/index.d.ts b/types/index.d.ts index 08e423b..ef5c6d0 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -4,21 +4,25 @@ import {Point} from 'unist' import {VFile} from 'vfile' declare namespace vfileLocation { - type Position = Pick - type PositionWithOffset = Required + type PositionalPoint = Pick + type FullPoint = Required type Offset = NonNullable + /** @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 } } diff --git a/types/vfile-location-tests.ts b/types/vfile-location-tests.ts index 674df38..435ee21 100644 --- a/types/vfile-location-tests.ts +++ b/types/vfile-location-tests.ts @@ -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})