Big update!Introducing GitHub Bot Commands. Learn more
Socket
Log inBook a demo

unist-util-is

Package Overview
Dependencies
0
Maintainers
2
Versions
16
Issues
File Explorer

Advanced tools

unist-util-is

unist utility to check if a node passes a test

    5.1.1latest

Version published
Maintainers
2
Weekly downloads
8,769,583
decreased by-11.77%

Weekly downloads

Changelog

Source

  • 27b28da Fix types to allow null

Readme

Source

unist-util-is

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to check if a node passes a test.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

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

API

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

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

Parameters
  • 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 values
  • index (number, optional) — Index of node in parent
  • parent (Node, optional) — Parent of node
  • context (*, optional) — Context object to invoke test 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
  • node (Node) — Node to check
  • index (number?) — Index of node in parent
  • parent (Node?) — Parent of node
Context

* — The to is given context.

Returns

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

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, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer

Keywords

FAQs

What is unist-util-is?

unist utility to check if a node passes a test

Is unist-util-is popular?

The npm package unist-util-is receives a total of 7,058,067 weekly downloads. As such, unist-util-is popularity was classified as popular.

Is unist-util-is well maintained?

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.

Last updated on 21 Jul 2021

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket
Socket

Product

Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc