Skip to content

Commit df038c8

Browse files
committed
Use ESM
1 parent e9be909 commit df038c8

File tree

6 files changed

+43
-57
lines changed

6 files changed

+43
-57
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-
hast-util-shift-heading.js
7-
hast-util-shift-heading.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-
hast-util-shift-heading.js
3-
hast-util-shift-heading.min.js
4-
*.json
52
*.md

index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
'use strict'
1+
import {headingRank} from 'hast-util-heading-rank'
2+
import {visit} from 'unist-util-visit'
23

3-
var headingRank = require('hast-util-heading-rank')
4-
var visit = require('unist-util-visit')
5-
6-
module.exports = shiftHeading
7-
8-
function shiftHeading(tree, shift) {
4+
export function shiftHeading(tree, shift) {
95
if (
106
typeof shift !== 'number' ||
117
!shift ||
12-
!isFinite(shift) ||
8+
!Number.isFinite(shift) ||
139
Math.floor(shift) !== shift
1410
) {
1511
throw new Error('Expected a non-null finite integer, not `' + shift + '`')

package.json

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,30 @@
2727
"contributors": [
2828
"Titus Wormer <[email protected]> (https://wooorm.com)"
2929
],
30+
"sideEffects": false,
31+
"type": "module",
32+
"main": "index.js",
3033
"files": [
3134
"index.js"
3235
],
3336
"dependencies": {
34-
"hast-util-heading-rank": "^1.0.0",
35-
"unist-util-visit": "^2.0.0"
37+
"hast-util-heading-rank": "^2.0.0",
38+
"unist-util-visit": "^3.0.0"
3639
},
3740
"devDependencies": {
38-
"browserify": "^17.0.0",
39-
"hastscript": "^6.0.0",
40-
"nyc": "^15.0.0",
41+
"c8": "^7.0.0",
42+
"hastscript": "^7.0.0",
4143
"prettier": "^2.0.0",
4244
"remark-cli": "^9.0.0",
4345
"remark-preset-wooorm": "^8.0.0",
4446
"tape": "^5.0.0",
45-
"tinyify": "^3.0.0",
46-
"xo": "^0.38.0"
47+
"xo": "^0.39.0"
4748
},
4849
"scripts": {
4950
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
50-
"build-bundle": "browserify . -s hastUtilShiftHeading -o hast-util-shift-heading.js",
51-
"build-mangle": "browserify . -s hastUtilShiftHeading -o hast-util-shift-heading.min.js -p tinyify",
52-
"build": "npm run build-bundle && npm run build-mangle",
53-
"test-api": "node test",
54-
"test-coverage": "nyc --reporter lcov tape test.js",
55-
"test": "npm run format && npm run build && npm run test-coverage"
51+
"test-api": "node test.js",
52+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
53+
"test": "npm run format && npm run test-coverage"
5654
},
5755
"prettier": {
5856
"tabWidth": 2,
@@ -64,19 +62,10 @@
6462
},
6563
"xo": {
6664
"prettier": true,
67-
"esnext": false,
6865
"rules": {
69-
"unicorn/prefer-number-properties": "off"
70-
},
71-
"ignores": [
72-
"hast-util-shift-heading.js"
73-
]
74-
},
75-
"nyc": {
76-
"check-coverage": true,
77-
"lines": 100,
78-
"functions": 100,
79-
"branches": 100
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
68+
}
8069
},
8170
"remarkConfig": {
8271
"plugins": [

readme.md

Lines changed: 9 additions & 3 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,8 +24,8 @@ npm install hast-util-shift-heading
2124
## Use
2225

2326
```js
24-
var h = require('hastscript')
25-
var shift = require('hast-util-shift-heading')
27+
import {h} from 'hastscript'
28+
import {shiftHeading} from 'hast-util-shift-heading'
2629

2730
var tree = h('main', [
2831
h('h1', 'Alpha'),
@@ -34,7 +37,7 @@ var tree = h('main', [
3437
h('h6', 'Golf')
3538
])
3639

37-
shift(tree, -1)
40+
shiftHeading(tree, -1)
3841

3942
console.log(tree)
4043
```
@@ -78,6 +81,9 @@ Yields:
7881

7982
## API
8083

84+
This package exports the following identifiers: `shiftHeading`.
85+
There is no default export.
86+
8187
### `shiftHeading(tree, shift)`
8288

8389
Change the rank of all headings (`h1` to `h6`) in `tree`.

test.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,77 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var h = require('hastscript')
5-
var shift = require('.')
1+
import test from 'tape'
2+
import {h} from 'hastscript'
3+
import {shiftHeading} from './index.js'
64

75
test('shiftHeading', function (t) {
86
t.throws(
97
function () {
10-
shift(h())
8+
shiftHeading(h(''))
119
},
1210
/^Error: Expected a non-null finite integer, not `undefined`$/,
1311
'should throw when not given a number'
1412
)
1513

1614
t.throws(
1715
function () {
18-
shift(h(), NaN)
16+
shiftHeading(h(''), Number.NaN)
1917
},
2018
/^Error: Expected a non-null finite integer, not `NaN`$/,
2119
'should throw when given not a number'
2220
)
2321

2422
t.throws(
2523
function () {
26-
shift(h(), 0.1)
24+
shiftHeading(h(''), 0.1)
2725
},
2826
/^Error: Expected a non-null finite integer, not `0.1`$/,
2927
'should throw when not given an integer'
3028
)
3129

3230
t.throws(
3331
function () {
34-
shift(h(), Infinity)
32+
shiftHeading(h(''), Number.POSITIVE_INFINITY)
3533
},
3634
/^Error: Expected a non-null finite integer, not `Infinity`$/,
3735
'should throw when not given a finite number'
3836
)
3937

4038
t.throws(
4139
function () {
42-
shift(h(), 0)
40+
shiftHeading(h(''), 0)
4341
},
4442
/^Error: Expected a non-null finite integer, not `0`$/,
4543
'should throw when not given a non-null number'
4644
)
4745

4846
t.deepEqual(
49-
shift(h('h1', 'Alpha'), 1),
47+
shiftHeading(h('h1', 'Alpha'), 1),
5048
h('h2', 'Alpha'),
5149
'should shift nodes upwards'
5250
)
5351

5452
t.deepEqual(
55-
shift(h('h2', 'Bravo'), -1),
53+
shiftHeading(h('h2', 'Bravo'), -1),
5654
h('h1', 'Bravo'),
5755
'should shift nodes downwards'
5856
)
5957

6058
t.deepEqual(
61-
shift(h('h2', 'Charlie'), -2),
59+
shiftHeading(h('h2', 'Charlie'), -2),
6260
h('h1', 'Charlie'),
6361
'should not shift upwards past h1'
6462
)
6563

6664
t.deepEqual(
67-
shift(h('h5', 'Delta'), 2),
65+
shiftHeading(h('h5', 'Delta'), 2),
6866
h('h6', 'Delta'),
6967
'should not shift downwards past h6'
7068
)
7169

7270
t.deepEqual(
73-
shift(h('main', [h('h1', 'Echo'), h('p', 'Foxtrot'), h('h5', 'Golf')]), 2),
71+
shiftHeading(
72+
h('main', [h('h1', 'Echo'), h('p', 'Foxtrot'), h('h5', 'Golf')]),
73+
2
74+
),
7475
h('main', [h('h3', 'Echo'), h('p', 'Foxtrot'), h('h6', 'Golf')]),
7576
'should change a tree'
7677
)

0 commit comments

Comments
 (0)