@accuser/unist-util-flatmap
v1.0.2
Published
Flatmap for unist nodes (useful for transforming trees) without mutating them.
Downloads
6
Readme
unist-util-flatmap
Create a new unist tree by mapping over all nodes and flattening the result without mutating the original tree.
Installation
npm install @accuser/unist-util-flatmap
Usage
import flatmap from '@accuser/unist-util-flatmap';
const tree = {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{ type: 'text', value: 'Hello, World!' },
]
}
]
};
const result = flatmap(tree, (node) => {
if (node.type === 'text') {
return [
{ type: 'text', value: node.value.toUpperCase() }
];
}
return [node];
});
console.log(result);
Output:
{ type: 'root', children: [{ type: 'paragraph', children: [{ type: 'text', value: 'HELLO, WORLD!' }] }] }
Tests
npm test