unist-util-is
unist utility to check if a node passes a test
Last updated 3 years ago by wooorm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install unist-util-is 
SYNC missed versions from official npm registry.

unist-util-is

Build Coverage Downloads Size Sponsors Backers Chat

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 what nodes are.

A similar package, hast-util-is-element, works on hast elements.

For more advanced tests, unist-util-select can be used to match against CSS selectors.

Install

This package is ESM only. In Node.js (version 14.14+ and 16.0+), install with npm:

npm install unist-util-is

In Deno with esm.sh:

import {is} from 'https://esm.sh/unist-util-is@5'

In browsers with esm.sh:

<script type="module">
  import {is} from 'https://esm.sh/unist-util-is@5?bundle'
</script>

Use

import {is} from 'unist-util-is'

const node = {type: 'strong'}
const parent = {type: 'paragraph', children: [node]}

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

function test(node, n) {
  return n === 5
}

API

This package exports the identifiers convert and is. There is no default export.

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

Check if node is a Node and whether it passes the given test.

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

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

Throws

When an incorrect test, index, or parent is given. There is no error thrown when node 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 a utility where something else passes a compatible test.

The created function is a bit faster because it expects valid input only: a node, index, and parent.

Parameters
Returns

An assertion (AssertAnything or AssertPredicate).

AssertAnything

Check that an arbitrary value is a node, unaware of TypeScript inferral (TypeScript type).

Parameters
  • 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).

AssertPredicate

Check that an arbitrary value is a specific node, aware of TypeScript (TypeScript type).

Type parameters
  • Kind (Node) — node type
Parameters
  • 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 (node is Kind).

Test

Check for an arbitrary node, unaware of TypeScript inferral (TypeScript type).

Type
type Test =
  | null
  | undefined
  | string
  | Record<string, unknown>
  | TestFunctionAnything
  | Array<string | Record<string, unknown> | TestFunctionAnything>

Checks that the given thing is a node, and then:

  • when string, checks that the node has that tag name
  • when function, see TestFunctionAnything
  • 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

TestFunctionAnything

Check if a node passes a test, unaware of TypeScript inferral (TypeScript type).

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

Whether this node passes the test (boolean).

PredicateTest

Check for a node that can be inferred by TypeScript (TypeScript type).

Type
type PredicateTest<Kind extends Node> =
  | Kind['type']
  | Partial<Kind>
  | TestFunctionPredicate<Kind>
  | Array<Kind['type'] | Partial<Kind> | TestFunctionPredicate<Kind>>

See TestFunctionPredicate.

TestFunctionPredicate

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

Type parameters
  • Kind (Node) — node type
Parameters
  • node (Node) — a node
  • index (number, optional) — the node’s position in its parent
  • parent (Node, optional) — the node’s parent
Returns

Whether this node passes the test (node is Kind).

Examples

Example of convert

import {u} from 'unist-builder'
import {convert} from 'unist-util-is'

const test = convert('leaf')

const tree = u('tree', [
  u('node', [u('leaf', '1')]),
  u('leaf', '2'),
  u('node', [u('leaf', '3'), u('leaf', '4')]),
  u('leaf', '5')
])

const leafs = tree.children.filter((child, index) => test(child, index, tree))

console.log(leafs)

Yields:

[{type: 'leaf', value: '2'}, {type: 'leaf', value: '5'}]

Types

This package is fully typed with TypeScript. It exports the additional types AssertAnything, AssertPredicate, Test, TestFunctionAnything, TestFunctionPredicate, and PredicateTest.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

Related

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

Current Tags

  • 5.2.1                                ...           latest (2 years ago)

18 Versions

  • 5.2.1                                ...           2 years ago
  • 5.2.0                                ...           2 years ago
  • 5.1.1                                ...           3 years ago
  • 5.1.0                                ...           4 years ago
  • 5.0.0                                ...           4 years ago
  • 4.1.0                                ...           4 years ago
  • 4.0.4                                ...           4 years ago
  • 4.0.3                                ...           4 years ago
  • 4.0.2                                ...           5 years ago
  • 4.0.1                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 3.0.0                                ...           6 years ago
  • 2.1.3                                ...           6 years ago
  • 2.1.2                                ...           7 years ago
  • 2.1.1                                ...           7 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 10
Last Day 0
Last Week 10
Last Month 0
Dependencies (0)
None
Dev Dependencies (16)

Copyright 2013 - present © cnpmjs.org | Home |