- Notifications
You must be signed in to change notification settings - Fork1
License
NotificationsYou must be signed in to change notification settings
GenerousLabs/unist-util-reduce
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
unist utility to recursively reduce a tree
npm:
npm install unist-util-reduce
constu=require("unist-builder");constreduce=require("unist-util-reduce");constassert=require("assert");consttree=u("tree",[u("leaf","1"),u("node",[u("leaf","2")]),u("void"),u("leaf","3")]);constnewTree=reduce(tree,function(node){if(node.value==="2"){constduplicateNode={ ...node,value:"two"};return[duplicateNode,node];}returnnode;});constexpected=u("tree",[u("leaf","1"),u("node",[u("leaf","two"),u("leaf","2")]),u("void"),u("leaf","3")]);assert.deepEqual(newTree,expected);
NOTE:leaf withvalue2 is visited before it's parentnode. By thetime yourtransform function is invoked, it'schildren will already havebeen passed throughtransform.
tree- A node of typeParenttransform- A function with the signature:(node: Node, path: number[], root: Paretn) => Node | Node[]node- TheNodein the tree to be transformedpath- The path to reach this node in each level of the treeroot- TheParentroot node- Returns - What you return is passed to
.concat()on thechildrenarray of the node's parent.
unist-util-filter— Create a new tree with all nodes that pass a testunist-util-map— Create a new tree with all nodes mapped by a given functionunist-util-remove— Remove nodes from a tree that pass a testunist-util-select— Select nodes with CSS-like selectors
About
No description, website, or topics provided.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.