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

utility to modify direct children of a parent

License

NotificationsYou must be signed in to change notification settings

syntax-tree/unist-util-modify-children

BuildCoverageDownloadsSizeSponsorsBackersChat

unist utility to change children of a parent.

Contents

What is this?

This is a tiny utility that you can use to create a reusable function thatmodifies children.

When should I use this?

Probably never!Useunist-util-visit.

Install

This package isESM only.In Node.js (version 16+), install withnpm:

npm install unist-util-modify-children

In Deno withesm.sh:

import{modifyChildren}from'https://esm.sh/unist-util-modify-children@4'

In browsers withesm.sh:

<scripttype="module">import{modifyChildren}from'https://esm.sh/unist-util-modify-children@4?bundle'</script>

Use

importufrom'unist-builder'import{modifyChildren}from'unist-util-modify-children'consttree=u('root',[u('leaf','1'),u('parent',[u('leaf','2')]),u('leaf','3')])constmodify=modifyChildren(function(node,index,parent){if(node.type==='parent'){parent.children.splice(index,1,{type:'subtree',children:parent.children})returnindex+1}})modify(tree)console.dir(tree,{depth:undefined})

Yields:

{type:'root',children:[{type:'leaf',value:'1'},{type:'subtree',children:[{type:'leaf',value:'2'}]},{type:'leaf',value:'3'}]}

API

This package exports the identifiermodifyChildren.There is no default export.

modifyChildren(modifier)

Wrapmodifier to be called for each child in the nodes later given tomodify.

Parameters
  • modifier (Modifier)— callback called for eachchild inparent later given tomodify
Returns

Modify children ofparent (Modify).

Modifier

Callback called for eachchild inparent later given tomodify(TypeScript type).

Parameters
  • child (Node)— child ofparent
  • index (number)— position ofchild inparent
  • parent (Node)— parent node
Returns

Position to move to next (optional) (number orundefined).

Modify

Modify children ofparent (TypeScript type).

Parameters
  • parent (Node)— parent node
Returns

Nothing (undefined).

Types

This package is fully typed withTypeScript.It exports the additional typesModifier andModify.

Compatibility

Projects maintained by the unified collective are compatible with maintainedversions of Node.js.

When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,unist-util-modify-children@^4, compatible with Node.js 16.

Related

Contribute

Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.

This project has acode of conduct.By interacting with this repository, organization, or community you agree toabide by its terms.

License

MIT ©Titus Wormer

About

utility to modify direct children of a parent

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp