Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
unist-util-map
Advanced tools
Create a new Unist tree with all nodes that mapped by the provided function
The unist-util-map package is a utility for transforming unist syntax trees by applying a function to each node, creating a new tree with the same structure but with transformed nodes.
Transforming Nodes
This feature allows you to apply a transformation function to each node in a unist tree. In the example, each text node's value is transformed to uppercase.
const map = require('unist-util-map');
const tree = {
type: 'root',
children: [
{ type: 'text', value: 'Hello' },
{ type: 'text', value: 'World' }
]
};
const newTree = map(tree, node => {
if (node.type === 'text') {
return { ...node, value: node.value.toUpperCase() };
}
return node;
});
console.log(newTree);
unist-util-visit is a utility for walking through unist syntax trees and performing operations on nodes. Unlike unist-util-map, which creates a new tree with transformed nodes, unist-util-visit is typically used for side effects or gathering information from nodes.
unist-util-filter is used to create a new tree by filtering nodes based on a predicate function. While unist-util-map transforms nodes, unist-util-filter removes nodes that do not match the predicate.
unist-util-modify-children allows for modifying the children of a node in a unist tree. It is more focused on direct modifications rather than creating a new tree structure like unist-util-map.
Create a new Unist tree with all nodes that mapped by the provided function.
Helper for creating unist: Universal Syntax Tree.
npm install unist-util-map
map(AST, function(node, index, parent){ /* return */ }): AST
map function return new AST object.
const map = require("unist-util-map");
const ObjectAssign = require("object-assign");
// input
const ast = {
"type": "root",
"children": [
{
"type": "node",
"children": [
{"type": "leaf", "value": "1"}
]
},
{"type": "leaf", "value": "2"}
]
};
// tranformed
const actual = map(ast, function (node) {
if (node.type === "leaf") {
return ObjectAssign({}, node, {
value: "CHANGED"
});
}
// no change
return node;
});
// output
const expected = {
"type": "root",
"children": [
{
"type": "node",
"children": [
{"type": "leaf", "value": "CHANGED"}
]
},
{"type": "leaf", "value": "CHANGED"}
]
};
assert.deepEqual(actual, expected);
npm test
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT
FAQs
unist utility to create a new tree by mapping all nodes
The npm package unist-util-map receives a total of 81,210 weekly downloads. As such, unist-util-map popularity was classified as popular.
We found that unist-util-map demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.