Skip to content

Commit d59168d

Browse files
committed
Use ESM
1 parent 8327e1b commit d59168d

File tree

9 files changed

+96
-87
lines changed

9 files changed

+96
-87
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-
nlcst-test.js
7-
nlcst-test.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-
nlcst-test.js
3-
nlcst-test.min.js
4-
*.json
52
*.md

index.js

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,73 @@
1-
'use strict'
2-
3-
var assert = require('assert')
4-
var zwitch = require('zwitch')
5-
var mapz = require('mapz')
6-
var unist = require('unist-util-assert')
7-
8-
var nlcst = zwitch('type')
9-
10-
exports = unist.wrap(nlcst)
11-
module.exports = exports
12-
13-
exports.parent = unist.wrap(parent)
14-
exports.text = unist.text
15-
exports.void = unist.void
16-
exports.wrap = unist.wrap
17-
exports.all = mapz(exports, {key: 'children', indices: false})
18-
19-
// Core interface.
20-
nlcst.unknown = unknown
21-
nlcst.invalid = unknown
22-
23-
// Per-type handling.
24-
nlcst.handlers = {
25-
RootNode: unist.wrap(RootNode),
26-
ParagraphNode: exports.parent,
27-
SentenceNode: exports.parent,
28-
WordNode: exports.parent,
29-
TextNode: exports.text,
30-
SymbolNode: exports.text,
31-
PunctuationNode: exports.text,
32-
WhiteSpaceNode: exports.text,
33-
SourceNode: exports.text
1+
import nodeAssert from 'assert'
2+
import {zwitch} from 'zwitch'
3+
import {mapz} from 'mapz'
4+
import {
5+
assert as unistAssert,
6+
parent as unistParent,
7+
literal,
8+
_void,
9+
wrap
10+
} from 'unist-util-assert'
11+
12+
/**
13+
* Assert that `node` is a valid nlcst node.
14+
* If `node` is a parent, all children will be asserted too.
15+
*
16+
* @param {unknown} [node]
17+
* @param {Parent} [parent]
18+
* @returns {asserts node is Node}
19+
*/
20+
export function assert(node, parent) {
21+
return wrap(nlcst)(node, parent)
22+
}
23+
24+
/**
25+
* Assert that `node` is a valid nlcst parent.
26+
*
27+
* @param {unknown} [node]
28+
* @param {Parent} [parent]
29+
* @returns {asserts node is Parent}
30+
*/
31+
export function parent(node, parent) {
32+
return wrap(assertParent)(node, parent)
3433
}
3534

35+
export {literal, _void, wrap}
36+
37+
var all = mapz(assert, {key: 'children'})
38+
39+
var nlcst = zwitch('type', {
40+
// Core interface.
41+
unknown,
42+
invalid: unknown,
43+
// Per-type handling.
44+
handlers: {
45+
RootNode: wrap(RootNode),
46+
ParagraphNode: parent,
47+
SentenceNode: parent,
48+
WordNode: parent,
49+
TextNode: literal,
50+
SymbolNode: literal,
51+
PunctuationNode: literal,
52+
WhiteSpaceNode: literal,
53+
SourceNode: literal
54+
}
55+
})
56+
3657
function unknown(node, ancestor) {
37-
unist(node, ancestor)
58+
unistAssert(node, ancestor)
3859
}
3960

40-
function parent(node) {
41-
unist.parent(node)
42-
exports.all(node)
61+
function assertParent(node) {
62+
unistParent(node)
63+
all(node)
4364
}
4465

4566
function RootNode(node, ancestor) {
4667
parent(node)
47-
assert.strictEqual(ancestor, undefined, '`RootNode` should not have a parent')
68+
nodeAssert.strictEqual(
69+
ancestor,
70+
undefined,
71+
'`RootNode` should not have a parent'
72+
)
4873
}

package.json

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,30 @@
2323
"contributors": [
2424
"Titus Wormer <[email protected]> (https://wooorm.com)"
2525
],
26+
"sideEffects": false,
27+
"type": "module",
28+
"main": "index.js",
2629
"files": [
2730
"index.js"
2831
],
2932
"dependencies": {
30-
"mapz": "^1.0.0",
31-
"unist-util-assert": "^2.0.0",
32-
"zwitch": "^1.0.0"
33+
"mapz": "^2.0.0",
34+
"unist-util-assert": "^3.0.0",
35+
"zwitch": "^2.0.0"
3336
},
3437
"devDependencies": {
35-
"browserify": "^17.0.0",
36-
"nyc": "^15.0.0",
38+
"c8": "^7.0.0",
3739
"prettier": "^2.0.0",
3840
"remark-cli": "^9.0.0",
3941
"remark-preset-wooorm": "^8.0.0",
4042
"tape": "^5.0.0",
41-
"tinyify": "^3.0.0",
42-
"xo": "^0.38.0"
43+
"xo": "^0.39.0"
4344
},
4445
"scripts": {
4546
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
46-
"build-bundle": "browserify . -s nlcstTest -o nlcst-test.js",
47-
"build-mangle": "browserify . -s nlcstTest -o nlcst-test.min.js -p tinyify",
48-
"build": "npm run build-bundle && npm run build-mangle",
49-
"test-api": "node test",
50-
"test-coverage": "nyc --reporter lcov tape test",
51-
"test": "npm run format && npm run build && npm run test-coverage"
47+
"test-api": "node test/index.js",
48+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
49+
"test": "npm run format && npm run test-coverage"
5250
},
5351
"prettier": {
5452
"tabWidth": 2,
@@ -60,16 +58,10 @@
6058
},
6159
"xo": {
6260
"prettier": true,
63-
"esnext": false,
64-
"ignores": [
65-
"nlcst-test.js"
66-
]
67-
},
68-
"nyc": {
69-
"check-coverage": true,
70-
"lines": 100,
71-
"functions": 100,
72-
"branches": 100
61+
"rules": {
62+
"no-var": "off",
63+
"prefer-arrow-callback": "off"
64+
}
7365
},
7466
"remarkConfig": {
7567
"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,7 +24,7 @@ npm install nlcst-test
2124
## Use
2225

2326
```js
24-
var assert = require('nlcst-test')
27+
import {assert} from 'nlcst-test'
2528

2629
assert({type: 'RootNode', children: []})
2730
assert({type: 'SourceNode', value: 'fn()'})
@@ -37,13 +40,16 @@ assert({type: 'WordNode', value: 'foo'})
3740

3841
## API
3942

43+
This package exports the following identifiers: `assert`, `parent`, `literal`,
44+
`_void`, and `wrap`.
45+
There is no default export.
46+
4047
### `assert(tree)`
4148

4249
Assert that [`tree`][tree] is a valid [nlcst][] [node][].
4350
If `tree` is a [parent][], all [child][]ren will be asserted as well.
4451

45-
The `assert.parent`, `assert.text`, `assert.void`, and `assert.wrap`
46-
methods from [`unist-util-assert`][unist-util-assert] are also included.
52+
The other methods come from [`unist-util-assert`][unist-util-assert].
4753

4854
## Related
4955

test/children.js

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

64
test('children', function (t) {
75
t.throws(

test/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
31
/* eslint-disable import/no-unassigned-import */
4-
require('./node')
5-
require('./children')
6-
require('./root')
2+
import './node.js'
3+
import './children.js'
4+
import './root.js'
75
/* eslint-enable import/no-unassigned-import */

test/node.js

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

64
test('node', function (t) {
75
t.throws(

test/root.js

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

64
test('assert(RootNode)', function (t) {
75
t.throws(

0 commit comments

Comments
 (0)