Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8
utility to check if a node passes a test
License
syntax-tree/unist-util-is
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
unist utility to check if nodes pass a test.
This package is a small utility that checks that a node is a certain node.
Use this small utility if you find yourself repeating code for checking whatnodes are.
A similar package,hast-util-is-element, works on hastelements.
For more advanced tests,unist-util-select can be usedto match against CSS selectors.
This package isESM only.In Node.js (version 16+), install withnpm:
npm install unist-util-is
In Deno withesm.sh:
import{is}from'https://esm.sh/unist-util-is@6'
In browsers withesm.sh:
<scripttype="module">import{is}from'https://esm.sh/unist-util-is@6?bundle'</script>
import{is}from'unist-util-is'constnode={type:'strong'}constparent={type:'paragraph',children:[node]}is()// => falseis({children:[]})// => falseis(node)// => trueis(node,'strong')// => trueis(node,'emphasis')// => falseis(node,node)// => trueis(parent,{type:'paragraph'})// => trueis(parent,{type:'strong'})// => falseis(node,test)// => falseis(node,test,4,parent)// => falseis(node,test,5,parent)// => truefunctiontest(node,n){returnn===5}
This package exports the identifiersconvert andis.There is no default export.
Check ifnode is aNode and whether it passes the given test.
node(unknown, optional)— thing to check, typicallyNodetest(Test, optional)— a test for a specific elementindex(number, optional)— the node’s position in its parentparent(Node, optional)— the node’s parentcontext(unknown, optional)— context object (this) to calltestwith
Whethernode is aNode and passes a test (boolean).
When an incorrecttest,index, orparent is given.There is no error thrown whennode is not a node.
Generate a check from a test.
Useful if you’re going to test many nodes, for example when creating autility where something else passes a compatible test.
The created function is a bit faster because it expects valid input only:anode,index, andparent.
test(Test, optional)— a test for a specific node
A check (Check).
Check that an arbitrary value is a node (TypeScript type).
this(unknown, optional)— context object (this) to calltestwithnode(unknown)— anything (typically a node)index(number, optional)— the node’s position in its parentparent(Node, optional)— the node’s parent
Whether this is a node and passes a test (boolean).
Check for an arbitrary node (TypeScript type).
typeTest=|Array<Record<string,unknown>|TestFunction|string>|Record<string,unknown>|TestFunction|string|null|undefined
Checks that the given thing is a node, and then:
- when
string, checks that the node has that tag name - when
function, seeTestFunction - when
object, checks that all keys in test are in node, and that they have(strictly) equal values - when
Array, checks if one of the subtests pass
Check if a node passes a test (TypeScript type).
node(Node)— a nodeindex(numberorundefined)— the node’s position in its parentparent(Nodeorundefined)— the node’s parent
Whether this node passes the test (boolean, optional).
import{u}from'unist-builder'import{convert}from'unist-util-is'consttest=convert('leaf')consttree=u('tree',[u('node',[u('leaf','1')]),u('leaf','2'),u('node',[u('leaf','3'),u('leaf','4')]),u('leaf','5')])constleafs=tree.children.filter(function(child,index){returntest(child,index,tree)})console.log(leafs)
Yields:
[{type:'leaf',value:'2'},{type:'leaf',value:'5'}]
This package is fully typed withTypeScript.It exports the additional typesCheck,Test,TestFunction.
Projects maintained by the unified collective are compatible with maintainedversions of Node.js.
When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,unist-util-is@^6,compatible with Node.js 16.
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 tree
Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.
This project has acode of conduct.By interacting with this repository, organization, or community you agree toabide by its terms.
About
utility to check if a node passes a test
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.