unist-util-map
!Buildbuild-badgebuild
!Coveragecoverage-badgecoverage
!Downloadsdownloads-badgedownloads
!Sizesize-badgesize
!Sponsorssponsors-badgecollective
!Backersbackers-badgecollective
!Chatchat-badgechatunist utility to create a new tree by mapping all nodes with a given function.
Contents
* [`map(tree, mapFunction)`](#maptree-mapfunction)
What is this?
This is a small utility that helps you make new trees.When should I use this?
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, useunist-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.Install
This package is ESM onlyesm. In Node.js (version 16+), install with npm:npm install unist-util-map
In Deno with
esm.sh
esmsh:import {map} from 'https://esm.sh/unist-util-map@4'
In browsers with
esm.sh
esmsh:<script type="module">
import {map} from 'https://esm.sh/unist-util-map@4?bundle'
</script>
Use
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:
{
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 andtree
is not mutated.
API
This package exports the identifiermap
api-map.
There is no default export.map(tree, mapFunction)
Create a new tree by mapping all nodes with the given function.Parameters
— tree to map
mapFunction
(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 to map
index
(number
orundefined
)
— index of `node` in `parent` (if any)
— parent of `node`
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.
Types
This package is fully typed with TypeScript. It exports the additional typeMapFunction
api-mapfunction.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-map@^4
,
compatible with Node.js 16.Related
— create a new tree with all nodes that pass the given function
— create a new tree by expanding a node into many
— remove nodes from trees
— select nodes with CSS-like selectors
— walk trees
— create trees
Contribute
Seecontributing.md
contributing in syntax-tree/.github
health for
ways to get started.
See support.md
support for ways to get help.This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.