unist utility to create a new tree by mapping all nodes

Downloads in past


3003.1.22 months ago7 years agoMinified + gzip package size for unist-util-map in KB


!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unist utility to create a new tree by mapping all nodes with a given function.


*   [`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, use unist-util-visitunist-util-visit (or unist-util-visit-parentsunist-util-visit-parents) instead.
To remove certain nodes, you can also walk the tree with unist-util-visit, or use unist-util-filterunist-util-filter (clones the tree instead of mutating) or unist-util-removeunist-util-remove (mutates). To create trees, use unist-builderunist-builder.


This package is ESM onlyesm. In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with npm:
npm install unist-util-map

In Deno with esm.shesmsh:
import {map} from "https://esm.sh/unist-util-map@3"

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


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('leaf', 'leaf 3')

const next = map(tree, (node) => {
  return node.type === 'leaf'
    ? Object.assign({}, node, {value: 'CHANGED'})
    : node

console.dir(next, {depth: null})

  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. There is no default export.

map(tree, mapFunction)

Create a new tree (Nodenode) by mapping all nodes with the given function (MapFunctionmap-function).
New mapped tree (Nodenode).

function mapFunction(node, index, parent)

Function called with a node (Nodenode), its index (number?), and its parent (Node?) to produce a new node.
Node to be used in the new tree (Nodenode). 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 a type MapFunction<Tree extends Node = Node> to properly type MapFunctions.


Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.


— 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


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


MITlicense © azuauthor