All Projects → syntax-tree → unist-util-map

syntax-tree / unist-util-map

Licence: MIT license
utility to create a new tree by mapping all nodes

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to unist-util-map

unist-builder
utility to create a new trees with a nice syntax
Stars: ✭ 52 (+73.33%)
Mutual labels:  syntax-tree, util, unist, unist-util
unist-util-inspect
utility to inspect nodes
Stars: ✭ 16 (-46.67%)
Mutual labels:  syntax-tree, util, unist, unist-util
unist-util-visit-parents
utility to recursively walk over unist nodes, with ancestral information
Stars: ✭ 25 (-16.67%)
Mutual labels:  syntax-tree, util, unist, unist-util
hast-util-to-html
utility to serialize hast to HTML
Stars: ✭ 47 (+56.67%)
Mutual labels:  syntax-tree, util, unist
hast-util-sanitize
utility to sanitize hast nodes
Stars: ✭ 34 (+13.33%)
Mutual labels:  syntax-tree, util, unist
unist-util-select
utility to select unist nodes with CSS-like selectors
Stars: ✭ 41 (+36.67%)
Mutual labels:  util, unist, unist-util
mdast-util-to-string
utility to get the plain text content of an mdast node
Stars: ✭ 27 (-10%)
Mutual labels:  syntax-tree, util, unist
hast-util-from-dom
utility to transform a DOM tree to hast
Stars: ✭ 20 (-33.33%)
Mutual labels:  syntax-tree, util, unist
nlcst-to-string
utility to transform an nlcst tree to a string
Stars: ✭ 16 (-46.67%)
Mutual labels:  syntax-tree, util, unist
sast
Parse CSS, Sass, SCSS, and Less into a unist syntax tree
Stars: ✭ 51 (+70%)
Mutual labels:  syntax-tree, unist
xast
Extensible Abstract Syntax Tree
Stars: ✭ 32 (+6.67%)
Mutual labels:  syntax-tree, unist
ntast
Notion Abstract Syntax Tree specification.
Stars: ✭ 101 (+236.67%)
Mutual labels:  syntax-tree, unist
Unist Util Visit
utility to visit nodes
Stars: ✭ 101 (+236.67%)
Mutual labels:  syntax-tree, util
hast-util-to-mdast
utility to transform hast (HTML) to mdast (markdown)
Stars: ✭ 26 (-13.33%)
Mutual labels:  unist, unist-util
mdast-util-to-hast
utility to transform mdast to hast
Stars: ✭ 53 (+76.67%)
Mutual labels:  syntax-tree, unist
Unified
☔️ interface for parsing, inspecting, transforming, and serializing content through syntax trees
Stars: ✭ 3,036 (+10020%)
Mutual labels:  syntax-tree, unist
MarkdownSyntax
☄️ A Type-safe Markdown parser in Swift.
Stars: ✭ 65 (+116.67%)
Mutual labels:  syntax-tree, unist
Galton
Lightweight Node.js isochrone map server
Stars: ✭ 249 (+730%)
Mutual labels:  map
has-value
Returns true if a value exists, false if empty. Works with deeply nested values using object paths.
Stars: ✭ 27 (-10%)
Mutual labels:  util
Go Staticmaps
A go (golang) library and command line tool to render static map images using OpenStreetMap tiles.
Stars: ✭ 246 (+720%)
Mutual labels:  map

unist-util-map

Build Coverage Downloads Size Sponsors Backers Chat

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

Contents

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

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with npm:

npm install unist-util-map

In Deno with esm.sh:

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

In browsers with esm.sh:

<script type="module">
  import {map} from "https://esm.sh/unist-util-map@3?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, (node) => {
  return node.type === 'leaf'
    ? Object.assign({}, node, {value: 'CHANGED'})
    : node
})

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

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.

API

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

map(tree, mapFunction)

Create a new tree (Node) by mapping all nodes with the given function (MapFunction).

Returns

New mapped tree (Node).

function mapFunction(node, index, parent)

Function called with a node (Node), its index (number?), and its parent (Node?) to produce a new node.

Returns

Node to be used in the new tree (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 a type MapFunction<Tree extends Node = Node> to properly type MapFunctions.

Compatibility

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.

Related

Contribute

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.

License

MIT © azu

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].