unist-util-modify-children

unist utility to modify direct children of a parent

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
unist-util-modify-children
503.1.17 days ago7 years agoMinified + gzip package size for unist-util-modify-children in KB

Readme

unist-util-modify-children
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
unist 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! Use unist-util-visitunist-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.shesmsh:
import {modifyChildren} from 'https://esm.sh/unist-util-modify-children@3'

In browsers with esm.shesmsh:
<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 identifier modifyChildrenapi-modifychildren. There is no default export.

modifyChildren(modifier)

Wrap modifier to be called for each child in the nodes later given to modify.
Parameters
— callback called for each `child` in `parent` later given to `modify`
Returns
Modify children of parent (Modifyapi-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 types Modifierapi-modifier and Modifyapi-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

See contributing.mdcontributing in syntax-tree/.githubhealth for ways to get started. See support.mdsupport 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.

License

MITlicense © Titus Wormerauthor