unist utility to create a new tree by mapping all nodes
npm install unist-util-map[![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 create a new tree by mapping all nodes with a given
function.
* What is this?
* When should I use this?
* Install
* Use
* API
* map(tree, mapFunction)
* Types
* Compatibility
* Related
* Contribute
* License
This is a small utility that helps you make new trees.
You can use this utility to create a new tree by mapping all nodes with a given
function.
Creating new trees like this can lead to performance problems, as it creates
new objects for every node.
When dealing with potentially large trees, and relatively few changes, use
[unist-util-visit][unist-util-visit] (or
[unist-util-visit-parents][unist-util-visit-parents]) instead.
To remove certain nodes, you can also walk the tree with unist-util-visit, or
use [unist-util-filter][unist-util-filter] (clones the tree instead of
mutating) or [unist-util-remove][unist-util-remove] (mutates).
To create trees, use [unist-builder][unist-builder].
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install unist-util-map
In Deno with [esm.sh][esmsh]:
`js`
import {map} from 'https://esm.sh/unist-util-map@4'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {u} from 'unist-builder'
import {map} from 'unist-util-map'
const tree = u('tree', [
u('leaf', 'leaf 1'),
u('node', [u('leaf', 'leaf 2')]),
u('void'),
u('leaf', 'leaf 3')
])
const next = map(tree, function (node) {
return node.type === 'leaf'
? Object.assign({}, node, {value: 'CHANGED'})
: node
})
console.dir(next, {depth: undefined})
`
Yields:
`js`
{
type: 'tree',
children: [
{type: 'leaf', value: 'CHANGED'},
{type: 'node', children: [{type: 'leaf', value: 'CHANGED'}]},
{type: 'void'},
{type: 'leaf', value: 'CHANGED'}
]
}
> 👉 Note: next is a changed clone and tree is not mutated.
This package exports the identifier [map][api-map].
There is no default export.
Create a new tree by mapping all nodes with the given function.
###### Parameters
* tree ([Node][node])mapFunction
— tree to map
* ([MapFunction][api-mapfunction])
— function called with a node, its index, and its parent to produce a new
node
###### Returns
New mapped tree ([Node][node]).
#### MapFunction
Function called with a node, its index, and its parent to produce a new
node (TypeScript type).
###### Parameters
* node ([Node][node])index
— node to map
* (number or undefined)node
— index of in parent (if any)parent
* ([Node][node] or undefined)node
— parent of
###### Returns
New mapped node ([Node][node]).
The children on the returned node are not used.
If the original node has children, those are mapped instead.
This package is fully typed with [TypeScript][].
It exports the additional type [MapFunction][api-mapfunction].
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-map@^4,
compatible with Node.js 16.
* unist-util-filter
— create a new tree with all nodes that pass the given function
* unist-util-flatmap
— create a new tree by expanding a node into many
* unist-util-remove
— remove nodes from trees
* unist-util-select
— select nodes with CSS-like selectors
* unist-util-visit
— walk trees
* unist-builder
— create trees
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, organization, or community you agree to
abide by its terms.
[MIT][license] © [azu][author]
[build-badge]: https://github.com/syntax-tree/unist-util-map/workflows/main/badge.svg
[build]: https://github.com/syntax-tree/unist-util-map/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-map.svg
[coverage]: https://codecov.io/github/syntax-tree/unist-util-map
[downloads-badge]: https://img.shields.io/npm/dm/unist-util-map.svg
[downloads]: https://www.npmjs.com/package/unist-util-map
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-map
[size]: https://bundlejs.com/?q=unist-util-map
[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://efcl.info
[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
[unist-util-visit-parents]: https://github.com/syntax-tree/unist-util-visit-parents
[unist-util-filter]: https://github.com/syntax-tree/unist-util-filter
[unist-util-remove]: https://github.com/syntax-tree/unist-util-remove
[unist-builder]: https://github.com/syntax-tree/unist-builder
[api-map]: #maptree-mapfunction
[api-mapfunction]: #mapfunction