Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 10, 2021
1 parent 8327e1b commit d59168d
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 87 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
nlcst-test.js
nlcst-test.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
coverage/
nlcst-test.js
nlcst-test.min.js
*.json
*.md
101 changes: 63 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,73 @@
'use strict'

var assert = require('assert')
var zwitch = require('zwitch')
var mapz = require('mapz')
var unist = require('unist-util-assert')

var nlcst = zwitch('type')

exports = unist.wrap(nlcst)
module.exports = exports

exports.parent = unist.wrap(parent)
exports.text = unist.text
exports.void = unist.void
exports.wrap = unist.wrap
exports.all = mapz(exports, {key: 'children', indices: false})

// Core interface.
nlcst.unknown = unknown
nlcst.invalid = unknown

// Per-type handling.
nlcst.handlers = {
RootNode: unist.wrap(RootNode),
ParagraphNode: exports.parent,
SentenceNode: exports.parent,
WordNode: exports.parent,
TextNode: exports.text,
SymbolNode: exports.text,
PunctuationNode: exports.text,
WhiteSpaceNode: exports.text,
SourceNode: exports.text
import nodeAssert from 'assert'
import {zwitch} from 'zwitch'
import {mapz} from 'mapz'
import {
assert as unistAssert,
parent as unistParent,
literal,
_void,
wrap
} from 'unist-util-assert'

/**
* Assert that `node` is a valid nlcst node.
* If `node` is a parent, all children will be asserted too.
*
* @param {unknown} [node]
* @param {Parent} [parent]
* @returns {asserts node is Node}
*/
export function assert(node, parent) {
return wrap(nlcst)(node, parent)
}

/**
* Assert that `node` is a valid nlcst parent.
*
* @param {unknown} [node]
* @param {Parent} [parent]
* @returns {asserts node is Parent}
*/
export function parent(node, parent) {
return wrap(assertParent)(node, parent)
}

export {literal, _void, wrap}

var all = mapz(assert, {key: 'children'})

var nlcst = zwitch('type', {
// Core interface.
unknown,
invalid: unknown,
// Per-type handling.
handlers: {
RootNode: wrap(RootNode),
ParagraphNode: parent,
SentenceNode: parent,
WordNode: parent,
TextNode: literal,
SymbolNode: literal,
PunctuationNode: literal,
WhiteSpaceNode: literal,
SourceNode: literal
}
})

function unknown(node, ancestor) {
unist(node, ancestor)
unistAssert(node, ancestor)
}

function parent(node) {
unist.parent(node)
exports.all(node)
function assertParent(node) {
unistParent(node)
all(node)
}

function RootNode(node, ancestor) {
parent(node)
assert.strictEqual(ancestor, undefined, '`RootNode` should not have a parent')
nodeAssert.strictEqual(
ancestor,
undefined,
'`RootNode` should not have a parent'
)
}
38 changes: 15 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,30 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"mapz": "^1.0.0",
"unist-util-assert": "^2.0.0",
"zwitch": "^1.0.0"
"mapz": "^2.0.0",
"unist-util-assert": "^3.0.0",
"zwitch": "^2.0.0"
},
"devDependencies": {
"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",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s nlcstTest -o nlcst-test.js",
"build-mangle": "browserify . -s nlcstTest -o nlcst-test.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test",
"test": "npm run format && npm run build && npm run test-coverage"
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -60,16 +58,10 @@
},
"xo": {
"prettier": true,
"esnext": false,
"ignores": [
"nlcst-test.js"
]
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

## 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 @@ -21,7 +24,7 @@ npm install nlcst-test
## Use

```js
var assert = require('nlcst-test')
import {assert} from 'nlcst-test'

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

## API

This package exports the following identifiers: `assert`, `parent`, `literal`,
`_void`, and `wrap`.
There is no default export.

### `assert(tree)`

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

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

## Related

Expand Down
6 changes: 2 additions & 4 deletions test/children.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var test = require('tape')
var assert = require('..')
import test from 'tape'
import {assert} from '../index.js'

test('children', function (t) {
t.throws(
Expand Down
8 changes: 3 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

/* eslint-disable import/no-unassigned-import */
require('./node')
require('./children')
require('./root')
import './node.js'
import './children.js'
import './root.js'
/* eslint-enable import/no-unassigned-import */
6 changes: 2 additions & 4 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var test = require('tape')
var assert = require('..')
import test from 'tape'
import {assert} from '../index.js'

test('node', function (t) {
t.throws(
Expand Down
6 changes: 2 additions & 4 deletions test/root.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

var test = require('tape')
var assert = require('..')
import test from 'tape'
import {assert} from '../index.js'

test('assert(RootNode)', function (t) {
t.throws(
Expand Down

0 comments on commit d59168d

Please sign in to comment.