vfile-location
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -1,22 +0,2 @@ | ||
/** | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('vfile').VFile} VFile | ||
* | ||
* @typedef {Pick<Point, 'line'|'column'>} PositionalPoint | ||
* @typedef {Required<Point>} FullPoint | ||
* @typedef {NonNullable<Point['offset']>} Offset | ||
*/ | ||
/** | ||
* Get transform functions for the given `document`. | ||
* | ||
* @param {string|Uint8Array|VFile} file | ||
*/ | ||
export function location(file: string | Uint8Array | VFile): { | ||
toPoint: (offset: Offset) => FullPoint | ||
toOffset: (point: PositionalPoint) => Offset | ||
} | ||
export type Point = import('unist').Point | ||
export type VFile = import('vfile').VFile | ||
export type PositionalPoint = Pick<Point, 'line' | 'column'> | ||
export type FullPoint = Required<Point> | ||
export type Offset = NonNullable<Point['offset']> | ||
export {location} from './lib/index.js' | ||
export type Location = import('./lib/index.js').Location |
79
index.js
/** | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('vfile').VFile} VFile | ||
* | ||
* @typedef {Pick<Point, 'line'|'column'>} PositionalPoint | ||
* @typedef {Required<Point>} FullPoint | ||
* @typedef {NonNullable<Point['offset']>} Offset | ||
* @typedef {import('./lib/index.js').Location} Location | ||
*/ | ||
/** | ||
* Get transform functions for the given `document`. | ||
* | ||
* @param {string|Uint8Array|VFile} file | ||
*/ | ||
export function location(file) { | ||
var value = String(file) | ||
/** @type {Array.<number>} */ | ||
var indices = [] | ||
var search = /\r?\n|\r/g | ||
while (search.test(value)) { | ||
indices.push(search.lastIndex) | ||
} | ||
indices.push(value.length + 1) | ||
return {toPoint, toOffset} | ||
/** | ||
* Get the line and column-based `point` for `offset` in the bound indices. | ||
* Returns a point with `undefined` values when given invalid or out of bounds | ||
* input. | ||
* | ||
* @param {Offset} offset | ||
* @returns {FullPoint} | ||
*/ | ||
function toPoint(offset) { | ||
var index = -1 | ||
if (offset > -1 && offset < indices[indices.length - 1]) { | ||
while (++index < indices.length) { | ||
if (indices[index] > offset) { | ||
return { | ||
line: index + 1, | ||
column: offset - (indices[index - 1] || 0) + 1, | ||
offset | ||
} | ||
} | ||
} | ||
} | ||
return {line: undefined, column: undefined, offset: undefined} | ||
} | ||
/** | ||
* Get the `offset` for a line and column-based `point` in the bound indices. | ||
* Returns `-1` when given invalid or out of bounds input. | ||
* | ||
* @param {PositionalPoint} point | ||
* @returns {Offset} | ||
*/ | ||
function toOffset(point) { | ||
var line = point && point.line | ||
var column = point && point.column | ||
/** @type {number} */ | ||
var offset | ||
if ( | ||
typeof line === 'number' && | ||
typeof column === 'number' && | ||
!Number.isNaN(line) && | ||
!Number.isNaN(column) && | ||
line - 1 in indices | ||
) { | ||
offset = (indices[line - 2] || 0) + column - 1 || 0 | ||
} | ||
return offset > -1 && offset < indices[indices.length - 1] ? offset : -1 | ||
} | ||
} | ||
export {location} from './lib/index.js' |
{ | ||
"name": "vfile-location", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "vfile utility to convert between positional (line and column-based) and offset (range-based) locations", | ||
@@ -34,2 +34,3 @@ "license": "MIT", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
@@ -43,19 +44,17 @@ "index.js" | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -72,11 +71,7 @@ }, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
"remark-preset-wooorm" | ||
] | ||
@@ -83,0 +78,0 @@ }, |
113
readme.md
@@ -11,12 +11,36 @@ # vfile-location | ||
Convert between positional (line and column-based) and offsets (range-based) | ||
locations in a [virtual file][vfile]. | ||
[vfile][] utility to convert between positional (line and column-based) and | ||
offsets (range-based) locations. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`location(file)`](#locationfile) | ||
* [`Location`](#location) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This is a tiny but useful package to convert between arbitrary places in a | ||
file. | ||
## When should I use this? | ||
This utility is useful when ASTs nodes don’t cut it. | ||
For example, when you are making a lint rule that looks for dangerous | ||
characters in a file, which you accomplish by searching the raw file value, | ||
and still want to report it to users. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -26,2 +50,16 @@ npm install vfile-location | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {location} from 'https://esm.sh/vfile-location@4' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {location} from 'https://esm.sh/vfile-location@4?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -33,5 +71,5 @@ | ||
var place = location(new VFile('foo\nbar\nbaz')) | ||
const place = location(new VFile('foo\nbar\nbaz')) | ||
var offset = place.toOffset({line: 3, column: 3}) // => 10 | ||
const offset = place.toOffset({line: 3, column: 3}) // => 10 | ||
place.toPoint(offset) // => {line: 3, column: 3, offset: 10} | ||
@@ -42,21 +80,42 @@ ``` | ||
This package exports the following identifiers: `place`. | ||
This package exports the identifier [`location`][api-location]. | ||
There is no default export. | ||
### `place = location(doc)` | ||
### `location(file)` | ||
Get transform functions for the given `doc` (`string`) or [`file`][vfile]. | ||
Index the given document so you can translate between line/column and offset | ||
based positional info. | ||
Returns an object with [`toOffset`][to-offset] and [`toPoint`][to-point]. | ||
###### Parameters | ||
### `place.toOffset(point)` | ||
* `file` (`string`, `Buffer`, or [`VFile`][vfile]) | ||
— file to index | ||
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. | ||
###### Returns | ||
### `place.toPoint(offset)` | ||
Accessors for index ([`Location`][api-location-map]). | ||
Get the line and column-based [`point`][point] for `offset` in the bound file. | ||
### `Location` | ||
Accessors for index (TypeScript type). | ||
###### Fields | ||
* `toPoint` (`(offset?: number) => Point`) | ||
— get a line/column-based [`Point`][point] from `offset` | ||
* `toOffset` (`(point?: Point) => number`) | ||
— get an offset from a line/column-based [`point`][point] | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional type [`Location`][api-location-map]. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Contribute | ||
@@ -106,9 +165,15 @@ | ||
[contributing]: https://github.com/vfile/.github/blob/HEAD/contributing.md | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[support]: https://github.com/vfile/.github/blob/HEAD/support.md | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[contributing]: https://github.com/vfile/.github/blob/main/contributing.md | ||
[support]: https://github.com/vfile/.github/blob/main/support.md | ||
[health]: https://github.com/vfile/.github | ||
[coc]: https://github.com/vfile/.github/blob/HEAD/code-of-conduct.md | ||
[coc]: https://github.com/vfile/.github/blob/main/code-of-conduct.md | ||
@@ -121,6 +186,6 @@ [license]: license | ||
[to-offset]: #placetooffsetpoint | ||
[point]: https://github.com/syntax-tree/unist#point | ||
[to-point]: #placetopointoffset | ||
[api-location]: #locationfile | ||
[point]: https://github.com/syntax-tree/unist#point | ||
[api-location-map]: #location |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13505
8
7
219
186
1