What is unist-util-is?
The unist-util-is npm package is a utility library for working with Unist nodes. Unist (Universal Syntax Tree) is part of the unified ecosystem, which provides a way to work with syntax trees for content such as Markdown, HTML, or plain text. unist-util-is specifically helps in testing and filtering nodes in these trees based on certain conditions.
What are unist-util-is's main functionalities?
Test nodes with a condition
This feature allows you to test if a node matches a specific type or condition. In the example, `is` is used to check if the root node is a 'leaf' (which it isn't), and then if the first child of the root is a 'leaf' (which it is).
const is = require('unist-util-is');
const u = require('unist-builder');
const tree = u('root', [
u('leaf', 'first leaf'),
u('node', [u('leaf', 'nested leaf')])
]);
const test = is(tree, 'leaf'); // false
const testLeaf = is(tree.children[0], 'leaf'); // true
Filter nodes by type
This feature demonstrates how to filter nodes by type using `is` in combination with `unist-util-select`. It selects all 'leaf' nodes from the tree and filters them to ensure they are of type 'leaf'.
const is = require('unist-util-is');
const u = require('unist-builder');
const select = require('unist-util-select');
const tree = u('root', [
u('leaf', 'first leaf'),
u('node', [u('leaf', 'nested leaf')])
]);
const leaves = select.selectAll('leaf', tree).filter(node => is(node, 'leaf'));
Other packages similar to unist-util-is
unist-util-visit
This package is similar to unist-util-is in that it is used to work with Unist nodes. However, unist-util-visit focuses on visiting nodes within a tree, optionally filtering nodes, and applying a function to each node. It differs from unist-util-is by providing traversal capabilities rather than just testing or filtering.
unist-util-select
unist-util-select is used to select nodes from a Unist tree using CSS-like selectors. It complements unist-util-is by providing a way to retrieve nodes based on complex queries, whereas unist-util-is is more focused on testing nodes against specified conditions.
unist-util-is
Unist utility to check if a node passes
a test. Useful when working with mdast
or retext.
Installation
npm:
npm install unist-util-is
unist-util-is is also available for bower,
component, and
duo, and as an AMD, CommonJS, and globals
module, uncompressed and
compressed.
Usage
var is = require('.');
var node = {
'type': 'strong'
};
var parent = {
'type': 'paragraph',
'children': [node]
};
function test(node, n) {
return n === 5
}
is(null, node);
is('strong', node);
is('emphasis', node);
is(node, node)
is(node, {type: 'strong'})
is(test, node);
is(test, node, 4, parent);
is(test, node, 5, parent);
API
is(test, node[, index, parent[, context]])
Utility to check if a node passes a test.
Parameters:
-
test
(Function
, string
, or
Node
, optional)
— When not given, return is return true
.
Passing a string
is equal to passing
function (node) {return node.type === test}
.
Passing a node
is equal to passing
function (node) {return node === test}
.
-
node
(Node
)
— Node to test;
-
index
(number
, optional) — Position of node
in parent
;
-
parent
(Node
, optional) — Parent of node
;
-
context
(*
, optional) — Parent of node
.
Returns: boolean
, whether test
passed.
function test(node[, index, parent])
Parameters:
node
(Node
) — Node to test;index
(number
?) — Position of node
in parent
.parent
(Node
?) — Parent of node
.
Context: The to is
given context.
Returns: boolean?
, whether this iteration passes.
License
MIT © Titus Wormer