Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

License

NotificationsYou must be signed in to change notification settings

GenerousLabs/unist-util-reduce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat

unist utility to recursively reduce a tree

Install

npm:

npm install unist-util-reduce

Usage

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.

API

reduce(tree, transform)

  • tree - A node of typeParent
  • transform - A function with the signature:
    • (node: Node, path: number[], root: Paretn) => Node | Node[]
      • node - TheNode in the tree to be transformed
      • path - The path to reach this node in each level of the tree
      • root - TheParent root node
      • Returns - What you return is passed to.concat() on thechildrenarray of the node's parent.

Related

License

MIT ©Callum Macdonald / GeneroUS Labs

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp