Skip to content

Commit fce892a

Browse files
committed
Use ESM
1 parent 5722318 commit fce892a

File tree

6 files changed

+82
-74
lines changed

6 files changed

+82
-74
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
unist-util-position.js
7-
unist-util-position.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
unist-util-position.js
3-
unist-util-position.min.js
4-
*.json
52
*.md

index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
'use strict'
1+
export var pointStart = point('start')
2+
export var pointEnd = point('end')
23

3-
module.exports = position
4-
5-
position.start = factory('start')
6-
position.end = factory('end')
7-
8-
function position(node) {
9-
return {start: position.start(node), end: position.end(node)}
4+
export function position(node) {
5+
return {start: pointStart(node), end: pointEnd(node)}
106
}
117

12-
function factory(type) {
8+
function point(type) {
139
point.displayName = type
1410

1511
return point
@@ -20,7 +16,7 @@ function factory(type) {
2016
return {
2117
line: point.line || null,
2218
column: point.column || null,
23-
offset: isNaN(point.offset) ? null : point.offset
19+
offset: point.offset > -1 ? point.offset : null
2420
}
2521
}
2622
}

package.json

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,25 @@
2222
"contributors": [
2323
"Titus Wormer <[email protected]> (https://wooorm.com)"
2424
],
25+
"sideEffects": false,
26+
"type": "module",
27+
"main": "index.js",
2528
"files": [
2629
"index.js"
2730
],
2831
"devDependencies": {
29-
"browserify": "^17.0.0",
30-
"nyc": "^15.0.0",
32+
"c8": "^7.0.0",
3133
"prettier": "^2.0.0",
3234
"remark-cli": "^9.0.0",
3335
"remark-preset-wooorm": "^8.0.0",
3436
"tape": "^5.0.0",
35-
"tinyify": "^3.0.0",
3637
"xo": "^0.38.0"
3738
},
3839
"scripts": {
3940
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
40-
"build-bundle": "browserify . -s unistUtilPosition -o unist-util-position.js",
41-
"build-mangle": "browserify . -s unistUtilPosition -o unist-util-position.min.js -p tinyify",
42-
"build": "npm run build-bundle && npm run build-mangle",
43-
"test-api": "node test",
44-
"test-coverage": "nyc --reporter lcov tape test.js",
45-
"test": "npm run format && npm run build && npm run test-coverage"
46-
},
47-
"nyc": {
48-
"check-coverage": true,
49-
"lines": 100,
50-
"functions": 100,
51-
"branches": 100
41+
"test-api": "node test.js",
42+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
43+
"test": "npm run format && npm run test-coverage"
5244
},
5345
"prettier": {
5446
"tabWidth": 2,
@@ -60,13 +52,11 @@
6052
},
6153
"xo": {
6254
"prettier": true,
63-
"esnext": false,
6455
"rules": {
65-
"unicorn/prefer-number-properties": "off"
66-
},
67-
"ignores": [
68-
"unist-util-position.js"
69-
]
56+
"import/no-mutable-exports": "off",
57+
"no-var": "off",
58+
"prefer-arrow-callback": "off"
59+
}
7060
},
7161
"remarkConfig": {
7262
"plugins": [

readme.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

15+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17+
1518
[npm][]:
1619

1720
```sh
@@ -21,18 +24,18 @@ npm install unist-util-position
2124
## Use
2225

2326
```js
24-
var remark = require('remark')
25-
var position = require('unist-util-position')
27+
import remark from 'remark'
28+
import {position, pointStart, pointEnd} from 'unist-util-position'
2629

2730
var tree = remark().parse('# foo\n\n* bar\n')
2831

2932
console.log(position(tree))
30-
console.log(position.start(tree))
31-
console.log(position.end(tree))
33+
console.log(pointStart(tree))
34+
console.log(pointEnd(tree))
3235

3336
console.log(position())
34-
console.log(position.start())
35-
console.log(position.end())
37+
console.log(pointStart())
38+
console.log(pointEnd())
3639
```
3740

3841
Yields:
@@ -48,14 +51,18 @@ Yields:
4851

4952
## API
5053

54+
This package exports the following identifiers: `position`, `pointStart`, and
55+
`pointEnd`.
56+
There is no default export.
57+
5158
### `position(node?)`
5259

5360
Get the positional info of `node` ([`Node?`][node]).
5461
Returns [`Position`][position].
5562

56-
### `position.start(node?)`
63+
### `pointStart(node?)`
5764

58-
### `position.end(node?)`
65+
### `pointEnd(node?)`
5966

6067
Get the start or end points in the positional info of `node` ([`Node?`][node]).
6168
Returns [`Point`][point].

test.js

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var position = require('.')
1+
import test from 'tape'
2+
import {position, pointStart, pointEnd} from './index.js'
53

64
var properties = {
75
position: {
@@ -19,8 +17,6 @@ var noPosition = {}
1917
var generated = {line: null, column: null, offset: null}
2018

2119
test('unist-util-position', function (t) {
22-
var sides = ['start', 'end']
23-
2420
t.test('position', function (t) {
2521
t.same(
2622
position(properties),
@@ -55,35 +51,60 @@ test('unist-util-position', function (t) {
5551
t.end()
5652
})
5753

58-
// eslint-disable-next-line unicorn/no-array-for-each
59-
sides.forEach(function (side) {
60-
t.test('position.' + side, function (t) {
61-
var fn = position[side]
54+
t.test('pointStart', function (t) {
55+
t.same(
56+
pointStart(properties),
57+
properties.position.start,
58+
'should get a side'
59+
)
60+
61+
t.same(
62+
pointStart(noFields),
63+
generated,
64+
'should return an empty point without fields'
65+
)
66+
67+
t.same(
68+
pointStart(noPoints),
69+
generated,
70+
'should return an empty point without points'
71+
)
72+
73+
t.same(
74+
pointStart(noPosition),
75+
generated,
76+
'should return an empty point without position'
77+
)
78+
79+
t.same(pointStart(), generated, 'should return an empty point without node')
80+
81+
t.end()
82+
})
6283

63-
t.same(fn(properties), properties.position[side], 'should get a side')
84+
t.test('pointEnd', function (t) {
85+
t.same(pointEnd(properties), properties.position.end, 'should get a side')
6486

65-
t.same(
66-
fn(noFields),
67-
generated,
68-
'should return an empty point without fields'
69-
)
87+
t.same(
88+
pointEnd(noFields),
89+
generated,
90+
'should return an empty point without fields'
91+
)
7092

71-
t.same(
72-
fn(noPoints),
73-
generated,
74-
'should return an empty point without points'
75-
)
93+
t.same(
94+
pointEnd(noPoints),
95+
generated,
96+
'should return an empty point without points'
97+
)
7698

77-
t.same(
78-
fn(noPosition),
79-
generated,
80-
'should return an empty point without position'
81-
)
99+
t.same(
100+
pointEnd(noPosition),
101+
generated,
102+
'should return an empty point without position'
103+
)
82104

83-
t.same(fn(), generated, 'should return an empty point without node')
105+
t.same(pointEnd(), generated, 'should return an empty point without node')
84106

85-
t.end()
86-
})
107+
t.end()
87108
})
88109

89110
t.end()

0 commit comments

Comments
 (0)