A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/syntax-tree/unist-util-map below:

syntax-tree/unist-util-map: utility to create a new tree by mapping all nodes

unist utility to create a new tree by mapping all nodes with a given function.

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 (or unist-util-visit-parents) instead.

To remove certain nodes, you can also walk the tree with unist-util-visit, or use unist-util-filter (clones the tree instead of mutating) or unist-util-remove (mutates). To create trees, use unist-builder.

This package is ESM only. In Node.js (version 16+), install with npm:

npm install unist-util-map

In Deno with esm.sh:

import {map} from 'https://esm.sh/unist-util-map@4'

In browsers with esm.sh:

<script type="module">
  import {map} from 'https://esm.sh/unist-util-map@4?bundle'
</script>
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 and tree is not mutated.

This package exports the identifier map. There is no default export.

Create a new tree by mapping all nodes with the given function.

New mapped tree (Node).

Function called with a node, its index, and its parent to produce a new node (TypeScript type).

New mapped 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.

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.

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

MIT © azu


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4