unified

Project: syntax-tree/unist-util-assert

Package: unist-util-assert@3.0.1

  1. Dependencies: 1·Dependents: 4
  2. unist utility to assert nodes
  1. util 128
  2. utility 124
  3. unist 120
  4. unist-util 25
  5. check 11
  6. test 6
  7. assert 4

unist-util-assert

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to assert trees.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install unist-util-assert

Use

import {assert, parent, _void} from 'unist-util-assert'

assert({type: 'root', children: []})
assert({type: 'break'})
assert({type: 'element', properties: {}, children: []})
// All OK.

assert({children: []})
// AssertionError: node should have a type: `{ children: [] }`

parent({type: 'break'})
// AssertionError: parent should have `children`: `{ type: 'break' }`

assert({type: 'element', properties: function() {}})
// AssertionError: non-specced property `properties` should be JSON: `{ type: 'element', properties: [Function] }`

_void({type: 'text', value: 'Alpha'})
// AssertionError: void should not have `value`: `{ type: 'text', value: 'Alpha' }`

assert({type: 'paragraph', children: ['foo']})
// AssertionError: node should be an object: `'foo'` in `{ type: 'paragraph', children: [ 'foo' ] }`

API

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

assert(node[, parent])

Assert that node is a valid unist node. If node is a parent, all children will be asserted too.

parent(node[, parent])

Assert that node is a valid unist parent. All children will be asserted too.

literal(node[, parent])

Assert that node is a valid unist literal.

_void(node[, parent])

Assert that node is a valid unist node, but neither parent nor literal.

Extensions

This module can be used as a base to test subsets of unist (for an example, see mdast-util-assert). All functions that are exposed from such a module, and functions used internally to test children, should be wrapped in wrap, which adds an inspectable string of the respective node, and its parent when available, to the exposed error message.

wrap(fn)

Wraps fn (which is passed a node, and an optional parent node), so that any errors thrown inside it will contain information regarding the node (and the parent, when given).

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer