Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
unist-util-is
Advanced tools
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.
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'));
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 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 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 import
ed instead of require
d.
npm:
npm install unist-util-is
import {is} from '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
This package exports the following identifiers: is
, convert
.
There is no default export.
is(node[, test[, index, parent[, context]]])
node
(Node
) — Node to check.test
(Function
, string
, Object
, or Array.<Test>
, optional)
— When nullish, checks if node
is a 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 valuesindex
(number
, optional) — Index of node
in parent
parent
(Node
, optional) — Parent of node
context
(*
, optional) — Context object to invoke test
withboolean
— Whether test
passed and node
is a Node
(object with
type
set to a non-empty string
).
function test(node[, index, parent])
*
— The to is
given context
.
boolean?
— Whether node
matches.
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.
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))
console.log(leafs)
Yields:
[{type: 'leaf', value: '2'}, {type: 'leaf', value: '5'}]
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-filter
— Create a new tree with nodes that pass a checkunist-util-remove
— Remove nodes from treeSee 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.
FAQs
unist utility to check if a node passes a test
The npm package unist-util-is receives a total of 9,872,436 weekly downloads. As such, unist-util-is popularity was classified as popular.
We found that unist-util-is demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.