unist utility to calculate the number of nodes in a tree
npm install unist-util-size[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
[unist][] utility to calculate the number of nodes in a tree.
* What is this?
* When should I use this?
* Install
* Use
* API
* [size(tree[, test])](#sizetree-test)
* Types
* Compatibility
* Related
* Contribute
* License
This is a tiny utility that you can use to count nodes (that pass a test) in a
tree.
This is super tiny but a sometimes common problem.
You can use [unist-util-visit][unist-util-visit] yourself as well.
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install unist-util-size
In Deno with [esm.sh][esmsh]:
`js`
import {size} from 'https://esm.sh/unist-util-size@4'
In browsers with [esm.sh][esmsh]:
`html`
`js
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
`
This package exports the identifier [size][api-size].
There is no default export.
Calculate the number of nodes in tree.
###### Parameters
* tree ([Node][node])test
— tree to traverse
* ([Test][test], optional)unist-util-is
— compatible test (such as a node type)
###### Returns
Number of exclusive descendants of tree that pass test (number).
This package is fully typed with [TypeScript][].
It exports no additional types.
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.
* unist-util-source
— get the source of a node or position
See [contributing.md][contributing] in [syntax-tree/.github][health] forsupport.md`][support] for ways to get help.
ways to get started.
See [
This project has a [code of conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/syntax-tree/unist-util-size/workflows/main/badge.svg
[build]: https://github.com/syntax-tree/unist-util-size/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-size.svg
[coverage]: https://codecov.io/github/syntax-tree/unist-util-size
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-size.svg
[downloads]: https://www.npmjs.com/package/unist-util-size
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-size
[size]: https://bundlejs.com/?q=unist-util-size
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/syntax-tree/unist/discussions
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[license]: license
[author]: https://wooorm.com
[health]: https://github.com/syntax-tree/.github
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
[unist]: https://github.com/syntax-tree/unist
[node]: https://github.com/syntax-tree/unist#node
[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit
[test]: https://github.com/syntax-tree/unist-util-is#test
[api-size]: #sizetree-test