Socket
Socket
Sign inDemoInstall

unist-util-is

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    unist-util-is

Utility to check if a node passes a test


Version published
Weekly downloads
14M
decreased by-0.53%
Maintainers
1
Install size
10.1 kB
Created
Weekly downloads
 

Package description

What is unist-util-is?

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.

What are unist-util-is's main functionalities?

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'));

Other packages similar to unist-util-is

Readme

Source

unist-util-is Build Status Coverage Status

Unist utility to check if a node passes a test.

Installation

npm:

npm install unist-util-is

Usage

var is = require('unist-util-is')

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

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

is() // => false
is(null, {children: []}) // => false
is(null, node) // => true
is('strong', node) // => true
is('emphasis', node) // => false

is(node, node) // => true
is({type: 'paragraph'}, parent) // => true
is({type: 'strong'}, parent) // => false

is(test, node) // => false
is(test, node, 4, parent) // => false
is(test, node, 5, parent) // => true

API

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

Parameters
  • test (Function, string, Object, or Array.<Test>, optional) — When not given, checks if node is a Node. When string, works like passing function (node) {return node.type === test}. When array, checks any one of the subtests pass. When object, checks that all keys in test are in node, and that they have (strictly) equal values
  • node (Node) — Node to check. false is returned
  • index (number, optional) — Position 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 non-empty string).

function test(node[, index, parent])
Parameters
  • node (Node) — Node to test
  • index (number?) — Position of node in parent
  • parent (Node?) — Parent of node
Context

* — The to is given context.

Returns

boolean? — Whether node matches.

Contribute

See contributing.md in syntax-tree/unist for ways to get started.

This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer

Keywords

FAQs

Last updated on 30 Apr 2018

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc