unist utility to check if a node passes a test

Downloads in past


2705.1.1a year ago7 years agoMinified + gzip package size for unist-util-is in KB


!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unistunist utility to check if a node passes a test.


This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.
npm install unist-util-is
## Use

import {is} from 'unist-util-is'

const node = {type: 'strong'}
const parent = {type: 'paragraph', children: [node]}

function test(node, n) {
  return n === 5

is() // => false
is({children: []}) // => false
is(node) // => true
is(node, 'strong') // => true
is(node, 'emphasis') // => false

is(node, node) // => true
is(parent, {type: 'paragraph'}) // => true
is(parent, {type: 'strong'}) // => false

is(node, test) // => false
is(node, test, 4, parent) // => false
is(node, test, 5, parent) // => true


This package exports the following identifiers: is, convert. There is no default export.

is(node[, test[, index, parent[, context]]])

—  When nullish, checks if `node` is a [`Node`][node].
When `string`, works like passing `node => node.type === test`.
When `array`, checks if any one of the subtests pass.
When `object`, checks that all keys in `test` are in `node`,
and that they have strictly equal values
  • index (number, optional) — Index of node in parent
  • parent (Nodenode, optional) — Parent of node
  • context (*, optional) — Context object to invoke test with
boolean — Whether test passed and node is a Nodenode (object with type set to a non-empty string).

function test(node[, index, parent])

* — The to is given context.
boolean? — Whether node matches.


Create a test function from test, that can later be called with a node, index, and parent. Useful if you’re going to test many nodes, for example when creating a utility where something else passes an is-compatible test.
The created function is slightly faster because it expects valid input only. Therefore, passing invalid input, yields unexpected results.
For example:
import u from 'unist-builder'
import {convert} from 'unist-util-is'

var test = convert('leaf')

var tree = u('tree', [
  u('node', [u('leaf', '1')]),
  u('leaf', '2'),
  u('node', [u('leaf', '3'), u('leaf', '4')]),
  u('leaf', '5')

var leafs = tree.children.filter((child, index) => test(child, index, tree))


[{type: 'leaf', value: '2'}, {type: 'leaf', value: '5'}]


— Find a node after another node
— Find a node before another node
— Find all nodes after another node
— Find all nodes before another node
— Find all nodes between two nodes
— Create a new tree with nodes that pass a check
— Remove nodes from tree


See contributing.md in syntax-tree/.githubcontributing for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.


MITlicense © Titus Wormerauthor