@rokt33r/unist-util-is
v3.0.1-1
Published
Utility to check if a node passes a test
Downloads
4
Maintainers
Readme
unist-util-is
unist utility to check if a node passes a test.
Install
npm:
npm install unist-util-is
Usage
var is = require('unist-util-is')
var node = {type: 'strong'}
var 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
API
is(node[, test[, index, parent[, context]]])
Parameters
node
(Node
) — Node to check.test
(Function
,string
,Object
, orArray.<Test>
, optional) — When not given, checks ifnode
is aNode
. Whenstring
, works like passingnode => node.type === test
. Whenarray
, checks if any one of the subtests pass. Whenobject
, checks that all keys intest
are innode
, and that they have strictly equal valuesindex
(number
, optional) — Index ofnode
inparent
parent
(Node
, optional) — Parent ofnode
context
(*
, optional) — Context object to invoketest
with
Returns
boolean
— Whether test
passed and node
is a Node
(object with
type
set to a non-empty string
).
function test(node[, index, parent])
Parameters
Context
*
— The to is
given context
.
Returns
boolean?
— Whether node
matches.
is.convert(test)
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.
Can also be accessed with require('unist-util-is/convert')
.
For example:
var u = require('unist-builder')
var convert = require('unist-util-is/convert')
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))
console.log(leafs)
Yields:
[({type: 'leaf', value: '2'}, {type: 'leaf', value: '5'})]
Related
unist-util-find-after
— Find a node after another nodeunist-util-find-before
— Find a node before another nodeunist-util-find-all-after
— Find all nodes after another nodeunist-util-find-all-before
— Find all nodes before another nodeunist-util-find-all-between
— Find all nodes between two nodesunist-util-find
— Find nodes matching a predicateunist-util-filter
— Create a new tree with nodes that pass a checkunist-util-remove
— Remove nodes from tree
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, organisation, or community you agree to abide by its terms.