unist-util-size

unist utility to calculate the number of nodes in a tree

Usage no npm install needed!

<script type="module">
  import unistUtilSize from 'https://cdn.skypack.dev/unist-util-size';
</script>

README

unist-util-size

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to calculate the number of nodes in a tree.

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-size

Use

import {h} from 'hastscript'
import {size} from 'unist-util-size'

var tree = h('div', [
  h('p', [
    'Some ',
    h('strong', 'importance'),
    ' and ',
    h('em', 'emphasis'),
    '.'
  ]),
  h('pre', h('code', 'bar()'))
])

console.log(size(tree)) // => 11
console.log(size(tree, 'element')) // => 5

API

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

size(tree[, test])

Calculate the number of nodes in tree.

Parameters
  • tree (Node) — Tree to traverse
  • test (Test, optional) — is-compatible test (such as a node type)
Returns

number — Number of exclusive descendants passing test in tree.

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

License

MIT © Titus Wormer