Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 5, 2023
1 parent 67e8a41 commit dba9b8c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
36 changes: 27 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
* Offset.
*
* @callback ToPoint
* Get a line/column-based `point` from `offset`.
* Get the line/column based `Point` for `offset` in the bound indices.
*
* Returns `undefined` when given out of bounds input.
*
* Also implemented in Rust in [`wooorm/markdown-rs`][markdown-rs].
*
* [markdown-rs]: https://github.com/wooorm/markdown-rs/blob/main/src/util/location.rs
* @param {number | null | undefined} [offset]
* Something that should be an `offset.
* @returns {UnistPoint | undefined}
* Point, if valid and in-bounds input.
* Point, if `offset` is valid and in-bounds input.
*
* @callback ToOffset
* Get an offset from a line/column-based `point`.
* 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}
Expand All @@ -32,14 +38,20 @@
* @typedef Location
* Accessors for index.
* @property {ToPoint} toPoint
* Get a line/column-based `point` from `offset`.
* Get the line/column based `Point` for `offset` in the bound indices.
* @property {ToOffset} toOffset
* Get an offset from a line/column-based `point`.
* Get the `offset` from a line/column based `Point` in the bound indices.
*/

const search = /\r?\n|\r/g

/**
* Index the given document so you can translate between line/column and offset
* based positional info.
* Create an index of the given document to translate between line/column and
* offset based positional info.
*
* Also implemented in Rust in [`wooorm/markdown-rs`][markdown-rs].
*
* [markdown-rs]: https://github.com/wooorm/markdown-rs/blob/main/src/util/location.rs
*
* @param {VFile | Value} file
* File to index.
Expand All @@ -48,9 +60,15 @@
*/
export function location(file) {
const value = String(file)
/** @type {Array<number>} */
/**
* List, where each index is a line number (0-based), and each value is the
* byte index *after* where the line ends.
*
* @type {Array<number>}
*/
const indices = []
const search = /\r?\n|\r/g

search.lastIndex = 0

while (search.test(value)) {
indices.push(search.lastIndex)
Expand Down
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ There is no default export.

### `location(file)`

Index the given document so you can translate between line/column and offset
based positional info.
Create an index of the given document to translate between line/column and
offset based positional info.

###### Parameters

Expand All @@ -98,9 +98,11 @@ Accessors for index (TypeScript type).
###### Fields

* `toPoint` (`(offset: number) => Point | undefined`)
— get a line/column-based [`Point`][point] from `offset`
— get the line/column based [`Point`][point] for `offset` in the bound
indices
* `toOffset` (`(point: Point) => number`)
— get an offset from a line/column-based [`point`][point]
— get the `offset` from a line/column based [`Point`][point] in the bound
indices

## Types

Expand Down
5 changes: 2 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import assert from 'node:assert/strict'
import test from 'node:test'
import {VFile} from 'vfile'
import {location} from './index.js'
import * as mod from './index.js'

test('location', function () {
test('location', async function () {
assert.deepEqual(
Object.keys(mod).sort(),
Object.keys(await import('./index.js')).sort(),
['location'],
'should expose the public api'
)
Expand Down

0 comments on commit dba9b8c

Please sign in to comment.