Skip to content

syntax-tree/unist-util-assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

unist-util-assert Build Status Coverage Status

Assert Unist nodes.

Installation

npm:

npm install unist-util-assert

Usage

var assert = require('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: [] }`

assert.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] }`

assert.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

assert(node)

Assert that node is a valid Unist node. If node has children, all children will be asserted as well.

assert.parent(node)

Assert that node is a valid Unist Parent.

assert.text(node)

Assert that node is a valid Unist Text.

assert.void(node)

Assert that node is a valid Unist node, but neither Text nor Parent.

Extensions

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

assert.wrap(fn)

Wraps a function (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).

Related

Contribute

See contribute.md in syntax-tree/unist for ways to get started.

This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer