Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

utility to check if a node passes a test

License

NotificationsYou must be signed in to change notification settings

syntax-tree/unist-util-is

BuildCoverageDownloadsSizeSponsorsBackersChat

unist utility to check if nodes pass a test.

Contents

What is this?

This package is a small utility that checks that a node is a certain node.

When should I use this?

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.

Install

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>

Use

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}

API

This package exports the identifiersconvert andis.There is no default export.

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

Check ifnode is aNode and whether it passes the given test.

Parameters
  • node (unknown, optional)— thing to check, typicallyNode
  • test (Test, optional)— a test for a specific element
  • index (number, optional)— the node’s position in its parent
  • parent (Node, optional)— the node’s parent
  • context (unknown, optional)— context object (this) to calltest with
Returns

Whethernode is aNode and passes a test (boolean).

Throws

When an incorrecttest,index, orparent is given.There is no error thrown whennode is not a node.

convert(test)

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.

Parameters
  • test (Test, optional)— a test for a specific node
Returns

A check (Check).

Check

Check that an arbitrary value is a node (TypeScript type).

Parameters
  • this (unknown, optional)— context object (this) to calltest with
  • node (unknown)— anything (typically a node)
  • index (number, optional)— the node’s position in its parent
  • parent (Node, optional)— the node’s parent
Returns

Whether this is a node and passes a test (boolean).

Test

Check for an arbitrary node (TypeScript type).

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:

  • whenstring, checks that the node has that tag name
  • whenfunction, seeTestFunction
  • whenobject, checks that all keys in test are in node, and that they have(strictly) equal values
  • whenArray, checks if one of the subtests pass

TestFunction

Check if a node passes a test (TypeScript type).

Parameters
  • node (Node)— a node
  • index (number orundefined)— the node’s position in its parent
  • parent (Node orundefined)— the node’s parent
Returns

Whether this node passes the test (boolean, optional).

Examples

Example ofconvert

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'}]

Types

This package is fully typed withTypeScript.It exports the additional typesCheck,Test,TestFunction.

Compatibility

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.

Related

Contribute

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.

License

MIT ©Titus Wormer


[8]ページ先頭

©2009-2025 Movatter.jp