Skip to content

syntax-tree/unist-util-is

Repository files navigation

unist-util-is Build Status Coverage Status

Unist utility to check if a node passes a test. Useful when working with mdast or retext.

Installation

npm:

npm install unist-util-is

unist-util-is is also available for bower, component, and duo, and as an AMD, CommonJS, and globals module, uncompressed and compressed.

Usage

var is = require('.');
var node = {
    'type': 'strong'
};
var parent = {
    'type': 'paragraph',
    'children': [node]
};

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

is(null, node); // true
is('strong', node); // true
is('emphasis', node); // false

is(node, node) // true
is(node, {type: 'strong'}) // false

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

API

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

Utility to check if a node passes a test.

Parameters:

  • test (Function, string, or Node, optional) — When not given, return is return true.

    Passing a string is equal to passing function (node) {return node.type === test}.

    Passing a node is equal to passing function (node) {return node === test}.

  • node (Node) — Node to test;

  • index (number, optional) — Position of node in parent;

  • parent (Node, optional) — Parent of node;

  • context (*, optional) — Parent of node.

Returns: boolean, whether test passed.

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 this iteration passes.

License

MIT © Titus Wormer

About

utility to check if a node passes a test

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 6