Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 5, 2021
1 parent da33b37 commit 9dc5adc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 99 deletions.
3 changes: 0 additions & 3 deletions .gitignore
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
vfile-location.js
vfile-location.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
@@ -1,5 +1,2 @@
coverage/
vfile-location.js
vfile-location.min.js
*.json
*.md
16 changes: 9 additions & 7 deletions index.js
@@ -1,8 +1,4 @@
'use strict'

module.exports = factory

function factory(file) {
export function location(file) {
var value = String(file)
var indices = []
var search = /\r?\n|\r/g
Expand All @@ -29,7 +25,7 @@ function factory(file) {
return {
line: index + 1,
column: offset - (indices[index - 1] || 0) + 1,
offset: offset
offset
}
}
}
Expand All @@ -45,7 +41,13 @@ function factory(file) {
var column = point && point.column
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
}

Expand Down
42 changes: 15 additions & 27 deletions package.json
Expand Up @@ -26,39 +26,30 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"Christian Murphy <christian.murphy.42@gmail.com>"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "types/index.d.ts",
"files": [
"index.js",
"types/index.d.ts"
"types/index.d.ts",
"index.js"
],
"types": "types/index.d.ts",
"devDependencies": {
"@types/unist": "^2.0.0",
"browserify": "^17.0.0",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"tsd": "^0.14.0",
"vfile": "^4.0.0",
"xo": "^0.38.0"
"vfile": "^5.0.0",
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s vfileLocation -o vfile-location.js",
"build-mangle": "browserify . -s vfileLocation -o vfile-location.min.js -p tinyify",
"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"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"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 format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -70,13 +61,10 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/prefer-number-properties": "off"
},
"ignores": [
"vfile-location.js"
]
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
26 changes: 16 additions & 10 deletions readme.md
Expand Up @@ -13,6 +13,9 @@ locations in a [virtual file][vfile].

## 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.

[npm][]:

```sh
Expand All @@ -22,30 +25,33 @@ npm install vfile-location
## Use

```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}
```

## API

### `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].

Returns an object with [`toOffset`][to-offset] and [`toPoint`][to-point].

### `location.toOffset(point)`
### `place.toOffset(point)`

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.toPoint(offset)`
### `place.toPoint(offset)`

Get the line and column-based [`point`][point] for `offset` in the bound file.

Expand Down Expand Up @@ -107,8 +113,8 @@ abide by its terms.

[vfile]: https://github.com/vfile/vfile

[to-offset]: #locationtooffsetpoint
[to-offset]: #placetooffsetpoint

[to-point]: #locationtopointoffset
[to-point]: #placetopointoffset

[point]: https://github.com/syntax-tree/unist#point
91 changes: 42 additions & 49 deletions test.js
@@ -1,93 +1,91 @@
'use strict'

var test = require('tape')
var vfile = require('vfile')
var vfileLocation = require('.')
import test from 'tape'
import {VFile} from 'vfile'
import {location} from './index.js'

test('location()', function (t) {
var location = vfileLocation('')
var place = location('')

t.equals(
typeof location.toOffset,
typeof place.toOffset,
'function',
'should expose `toOffset` for `doc`'
)

t.equals(
typeof location.toOffset,
typeof place.toOffset,
'function',
'should expose `toPoint` for `doc`'
)

location = vfileLocation(vfile())
place = location(new VFile())

t.equals(
typeof location.toOffset,
typeof place.toOffset,
'function',
'should expose `toOffset` for `file`'
)

t.equals(
typeof location.toOffset,
typeof place.toOffset,
'function',
'should expose `toPoint` for `file`'
)

t.test('location.toOffset(point)', function (t) {
var location = vfileLocation('foo\nbar\nbaz')
t.test('place.toOffset(point)', function (t) {
var place = location('foo\nbar\nbaz')

t.equals(location.toOffset({}), -1, 'should return `-1` for invalid input')
t.equals(place.toOffset({}), -1, 'should return `-1` for invalid input')

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

t.equals(
location.toOffset({line: 2, column: 2}),
place.toOffset({line: 2, column: 2}),
5,
'should return an offset (#1)'
)

t.equals(
location.toOffset({line: 1, column: 1}),
place.toOffset({line: 1, column: 1}),
0,
'should return an offset (#2)'
)

t.equals(
location.toOffset({line: 3, column: 4}),
place.toOffset({line: 3, column: 4}),
11,
'should return an offset (#3)'
)

t.end()
})

t.test('location.toPoint(offset)', function (t) {
var location = vfileLocation('foo\nbar\nbaz')
t.test('place.toPoint(offset)', function (t) {
var place = location('foo\nbar\nbaz')

t.deepEquals(
location.toPoint(-1),
place.toPoint(-1),
{},
'should return an empty object for invalid input'
)

t.deepEquals(
location.toPoint(12),
place.toPoint(12),
{},
'should return an empty object for out of bounds input'
)

t.deepEquals(
location.toPoint(0),
place.toPoint(0),
{line: 1, column: 1, offset: 0},
'should return a point (#1)'
)

t.deepEquals(
location.toPoint(11),
place.toPoint(11),
{line: 3, column: 4, offset: 11},
'should return a point (#2)'
)
Expand All @@ -96,46 +94,46 @@ test('location()', function (t) {
})

t.test('other tests', function (t) {
var location = vfileLocation('foo')
var place = location('foo')

t.deepEquals(
[location.toPoint(3), location.toPoint(4), location.toPoint(5)],
[place.toPoint(3), place.toPoint(4), place.toPoint(5)],
[{line: 1, column: 4, offset: 3}, {}, {}],
'should return points for offsets around an EOF w/o EOLs'
)

t.deepEquals(
[
location.toOffset({line: 1, column: 4}),
location.toOffset({line: 2, column: 1}),
location.toOffset({line: 2, column: 2})
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, -1, -1],
'should return offsets for points around an EOF w/o EOLs'
)

location = vfileLocation('foo\n')
place = location('foo\n')

t.deepEquals(
[location.toPoint(3), location.toPoint(4), location.toPoint(5)],
[place.toPoint(3), place.toPoint(4), place.toPoint(5)],
[{line: 1, column: 4, offset: 3}, {line: 2, column: 1, offset: 4}, {}],
'should return points for offsets around an EOF EOL'
)

t.deepEquals(
[
location.toOffset({line: 1, column: 4}),
location.toOffset({line: 2, column: 1}),
location.toOffset({line: 2, column: 2})
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, 4, -1],
'should return offsets for points around an EOF EOL'
)

location = vfileLocation('foo\rbar')
place = location('foo\rbar')

t.deepEquals(
[location.toPoint(3), location.toPoint(4), location.toPoint(5)],
[place.toPoint(3), place.toPoint(4), place.toPoint(5)],
[
{line: 1, column: 4, offset: 3},
{line: 2, column: 1, offset: 4},
Expand All @@ -146,23 +144,18 @@ test('location()', function (t) {

t.deepEquals(
[
location.toOffset({line: 1, column: 4}),
location.toOffset({line: 2, column: 1}),
location.toOffset({line: 2, column: 2})
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, 4, 5],
'should return offsets for points around carriage returns'
)

location = vfileLocation('foo\r\nbar')
place = location('foo\r\nbar')

t.deepEquals(
[
location.toPoint(3),
location.toPoint(4),
location.toPoint(5),
location.toPoint(6)
],
[place.toPoint(3), place.toPoint(4), place.toPoint(5), place.toPoint(6)],
[
{line: 1, column: 4, offset: 3},
{line: 1, column: 5, offset: 4},
Expand All @@ -174,9 +167,9 @@ test('location()', function (t) {

t.deepEquals(
[
location.toOffset({line: 1, column: 4}),
location.toOffset({line: 2, column: 1}),
location.toOffset({line: 2, column: 2})
place.toOffset({line: 1, column: 4}),
place.toOffset({line: 2, column: 1}),
place.toOffset({line: 2, column: 2})
],
[3, 5, 6],
'should return offsets for points around carriage returns + line feeds'
Expand Down

0 comments on commit 9dc5adc

Please sign in to comment.