Skip to content

Commit

Permalink
Change to return undefined for out-of-bound offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 5, 2023
1 parent d19d357 commit 038c164
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
6 changes: 2 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
* Get the `offset` from a line/column based `Point` in the bound indices.
* @param {PointLike | null | undefined} [point]
* Something that should be a `point.
* @returns {number}
* Offset or `-1` for invalid or out of bounds input.
* @returns {number | undefined}
* Offset (`number`) or `undefined` for invalid or out of bounds input.
*
* @typedef Location
* Accessors for index.
Expand Down Expand Up @@ -117,7 +117,5 @@ export function location(file) {
return offset
}
}

return -1
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Accessors for index (TypeScript type).
* `toPoint` (`(offset: number) => Point | undefined`)
— get the line/column based [`Point`][point] for `offset` in the bound
indices
* `toOffset` (`(point: Point) => number`)
* `toOffset` (`(point: Point) => number | undefined`)
— get the `offset` from a line/column based [`Point`][point] in the bound
indices

Expand Down
24 changes: 18 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ test('toOffset(point)', function () {

assert.equal(
place.toOffset({line: undefined, column: undefined}),
-1,
'should return `-1` for invalid input'
undefined,
'should return `undefined` for invalid input'
)

assert.equal(
place.toOffset({line: 4, column: 2}),
-1,
'should return `-1` for out of bounds input'
undefined,
'should return `undefined` for out of bounds input'
)

assert.equal(
Expand All @@ -71,6 +71,12 @@ test('toOffset(point)', function () {
11,
'should return an offset (#3)'
)

assert.equal(
location('').toOffset({line: 1, column: 1}),
0,
'should support empty document'
)
})

test('toPoint(offset)', function () {
Expand Down Expand Up @@ -99,6 +105,12 @@ test('toPoint(offset)', function () {
{line: 3, column: 4, offset: 11},
'should return a point (#2)'
)

assert.deepEqual(
location('').toPoint(0),
{line: 1, column: 1, offset: 0},
'should support empty document'
)
})

test('other tests', function () {
Expand All @@ -116,7 +128,7 @@ test('other tests', function () {
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, -1, -1],
[3, undefined, undefined],
'should return offsets for points around an EOF w/o EOLs'
)

Expand All @@ -138,7 +150,7 @@ test('other tests', function () {
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, 4, -1],
[3, 4, undefined],
'should return offsets for points around an EOF EOL'
)

Expand Down

0 comments on commit 038c164

Please sign in to comment.