unist-util-modify-children
!Buildbuild-badgebuild
!Coveragecoverage-badgecoverage
!Downloadsdownloads-badgedownloads
!Sizesize-badgesize
!Sponsorssponsors-badgecollective
!Backersbackers-badgecollective
!Chatchat-badgechatunist utility to change children of a parent.
Contents
* [`modifyChildren(modifier)`](#modifychildrenmodifier)
* [`Modifier`](#modifier)
* [`Modify`](#modify)
What is this?
This is a tiny utility that you can use to create a reusable function that modifies children.When should I use this?
Probably never! Useunist-util-visit
unist-util-visit.Install
This package is ESM onlyesm. In Node.js (version 14.14+ and 16.0+), install with npm:npm install unist-util-modify-children
In Deno with
esm.sh
esmsh:import {modifyChildren} from 'https://esm.sh/unist-util-modify-children@3'
In browsers with
esm.sh
esmsh:<script type="module">
import {modifyChildren} from 'https://esm.sh/unist-util-modify-children@3?bundle'
</script>
Use
import u from 'unist-builder'
import {modifyChildren} from 'unist-util-modify-children'
const tree = u('root', [
u('leaf', '1'),
u('parent', [u('leaf', '2')]),
u('leaf', '3')
])
const modify = modifyChildren(function (node, index, parent) {
if (node.type === 'parent') {
parent.children.splice(index, 1, {type: 'subtree', children: parent.children})
return index + 1
}
})
modify(tree)
console.dir(tree, {depth: null})
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
api-modifychildren.
There is no default export.modifyChildren(modifier)
Wrap modifier
to be called for each child in the nodes later given to
modify
.Parameters
modifier
(Modifier
api-modifier)
— callback called for each `child` in `parent` later given to `modify`
Returns
Modify children ofparent
(Modify
api-modify).Modifier
Callback called for each child
in parent
later given to modify
(TypeScript type).Parameters
— child of `parent`
index
(number
)
— position of `child` in `parent`
— parent node
Returns
Position to move to next (optional) (number
or void
).Modify
Modify children of parent
(TypeScript type).Parameters
— parent node
Returns
Nothing (void
).Types
This package is fully typed with TypeScript. It exports the additional typesModifier
api-modifier and
Modify
api-modify.Compatibility
Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.Related
— walk the tree
— walk the tree with a stack of parents
— create a new tree with all nodes that pass a test
— create a new tree with all nodes mapped by a given function
— create a new tree by mapping (to an array) with the given function
— find a node after another node
— find a node before another node
— find all nodes after another node
— find all nodes before another node
— find all nodes between two nodes
— remove nodes from a tree that pass a test
— select nodes with CSS-like selectors
Contribute
Seecontributing.md
contributing in syntax-tree/.github
health for
ways to get started.
See support.md
support for ways to get help.This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.