vfile-location
Advanced tools
Comparing version 3.2.0 to 4.0.0
62
index.js
@@ -1,11 +0,22 @@ | ||
'use strict' | ||
/** | ||
* @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 | ||
*/ | ||
module.exports = factory | ||
function factory(file) { | ||
/** | ||
* 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.exec(value)) { | ||
while (search.test(value)) { | ||
indices.push(search.lastIndex) | ||
@@ -16,10 +27,13 @@ } | ||
return { | ||
toPoint: offsetToPoint, | ||
toPosition: offsetToPoint, | ||
toOffset: pointToOffset | ||
} | ||
return {toPoint, toOffset} | ||
// Get the line and column-based `point` for `offset` in the bound indices. | ||
function offsetToPoint(offset) { | ||
/** | ||
* 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 | ||
@@ -33,3 +47,3 @@ | ||
column: offset - (indices[index - 1] || 0) + 1, | ||
offset: offset | ||
offset | ||
} | ||
@@ -40,13 +54,25 @@ } | ||
return {} | ||
return {line: undefined, column: undefined, offset: undefined} | ||
} | ||
// Get the `offset` for a line and column-based `point` in the bound | ||
// indices. | ||
function pointToOffset(point) { | ||
/** | ||
* 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 (!isNaN(line) && !isNaN(column) && line - 1 in indices) { | ||
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 | ||
@@ -53,0 +79,0 @@ } |
{ | ||
"name": "vfile-location", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "vfile utility to convert between positional (line and column-based) and offset (range-based) locations", | ||
@@ -29,37 +29,34 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"types/index.d.ts" | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"types": "types/index.d.ts", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"@types/unist": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/unist": "^2.0.0", | ||
"browserify": "^17.0.0", | ||
"nyc": "^15.0.0", | ||
"@types/tape": "^4.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", | ||
"tinyify": "^3.0.0", | ||
"tsd": "^0.13.1", | ||
"vfile": "^4.0.0", | ||
"xo": "^0.34.0" | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"vfile": "^5.0.0", | ||
"xo": "^0.39.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"build-bundle": "browserify . -s vfileLocation > vfile-location.js", | ||
"build-mangle": "browserify . -s vfileLocation -p tinyify > vfile-location.min.js", | ||
"build": "npm run build-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test-types": "tsd", | ||
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types" | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -75,9 +72,6 @@ "tabWidth": 2, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"unicorn/prefer-number-properties": "off" | ||
}, | ||
"ignores": [ | ||
"vfile-location.js" | ||
] | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
}, | ||
@@ -88,3 +82,8 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -16,2 +16,5 @@ # vfile-location | ||
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. | ||
[npm][]: | ||
@@ -26,9 +29,9 @@ | ||
```js | ||
var vfile = require('vfile') | ||
var vfileLocation = require('vfile-location') | ||
import {VFile} from 'vfile' | ||
import {location} from 'vfile-location' | ||
var location = vfileLocation(vfile('foo\nbar\nbaz')) | ||
var place = location(new VFile('foo\nbar\nbaz')) | ||
var offset = location.toOffset({line: 3, column: 3}) // => 10 | ||
location.toPoint(offset) // => {line: 3, column: 3, offset: 10} | ||
var offset = place.toOffset({line: 3, column: 3}) // => 10 | ||
place.toPoint(offset) // => {line: 3, column: 3, offset: 10} | ||
``` | ||
@@ -38,4 +41,7 @@ | ||
### `location = vfileLocation(doc)` | ||
This package exports the following identifiers: `place`. | ||
There is no default export. | ||
### `place = location(doc)` | ||
Get transform functions for the given `doc` (`string`) or [`file`][vfile]. | ||
@@ -45,3 +51,3 @@ | ||
### `location.toOffset(point)` | ||
### `place.toOffset(point)` | ||
@@ -52,3 +58,3 @@ Get the `offset` (`number`) for a line and column-based [`point`][point] in the | ||
### `location.toPoint(offset)` | ||
### `place.toPoint(offset)` | ||
@@ -73,5 +79,5 @@ Get the line and column-based [`point`][point] for `offset` in the bound file. | ||
[build-badge]: https://img.shields.io/travis/vfile/vfile-location.svg | ||
[build-badge]: https://github.com/vfile/vfile-location/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/vfile/vfile-location | ||
[build]: https://github.com/vfile/vfile-location/actions | ||
@@ -116,6 +122,6 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile-location.svg | ||
[to-offset]: #locationtooffsetpoint | ||
[to-offset]: #placetooffsetpoint | ||
[to-point]: #locationtopointoffset | ||
[to-point]: #placetopointoffset | ||
[point]: https://github.com/syntax-tree/unist#point |
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
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
9174
94
121
Yes
1
+ Added@types/unist@^2.0.0
+ Added@types/unist@2.0.11(transitive)