unist-util-size

unist utility to calculate the number of nodes in a tree

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
unist-util-size
104.0.010 months ago7 years agoMinified + gzip package size for unist-util-size in KB

Readme

unist-util-size
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unist utility to calculate the number of nodes in a tree.

Contents

*   [`size(tree[, test])`](#sizetree-test)

What is this?

This is a tiny utility that you can use to count nodes (that pass a test) in a tree.

When should I use this?

This is super tiny but a sometimes common problem. You can use unist-util-visitunist-util-visit yourself as well.

Install

This package is ESM onlyesm. In Node.js (version 16+), install with npm:
npm install unist-util-size

In Deno with esm.shesmsh:
import {size} from 'https://esm.sh/unist-util-size@4'

In browsers with esm.shesmsh:
<script type="module">
  import {size} from 'https://esm.sh/unist-util-size@4?bundle'
</script>

Use

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

const 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 identifier sizeapi-size. There is no default export.

size(tree[, test])

Calculate the number of nodes in tree.
Parameters
— tree to traverse
— `unist-util-is` compatible test (such as a node type)
Returns
Number of exclusive descendants of tree that pass test (number).

Types

This package is fully typed with TypeScript. It exports no additional types.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, unist-util-size@^4, compatible with Node.js 16.

Related

— get the source of a node or position

Contribute

See contributing.mdcontributing in syntax-tree/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor