Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

unist-util-modify-children

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unist-util-modify-children

Unist utility to modify direct children of a parent

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

unist-util-modify-children Build Status Coverage Status

Unist (mdast, retext) utility to modify direct children of a parent. As in, wrap fn so it accepts a parent and invoke fn for each of its children. When fn returns a number, goes to that child next.

Installation

npm:

npm install unist-util-modify-children

unist-util-modify-children is also available for bower, component, and duo, and as an AMD, CommonJS, and globals module, uncompressed and compressed.

Usage

var modifyChildren = require('unist-util-modify-children');

var modifier = modifyChildren(function (child, index, parent) {
    console.log(child, index);

    if (child.value === 'bravo') {
        parent.children.splice(index + 1, 0, { 'type': 'bar', 'value': 'delta' });
        return index + 1;
    }
});

var parent = {
    'type': 'foo',
    'children': [
        { 'type': 'bar', 'value': 'alpha' },
        { 'type': 'bar', 'value': 'bravo' },
        { 'type': 'bar', 'value': 'charlie' }
    ]
};

modifier(parent);
/*
 * { type: 'bar', value: 'alpha' } 0
 * { type: 'bar', value: 'bravo' } 1
 * { type: 'bar', value: 'delta' } 2
 * { type: 'bar', value: 'charlie' } 3
 */

console.log(parent);
/*
 * { type: 'foo',
 *   children:
 *    [ { type: 'bar', value: 'alpha' },
 *      { type: 'bar', value: 'bravo' },
 *      { type: 'bar', value: 'delta' },
 *      { type: 'bar', value: 'charlie' } ] }
 */

API

modifyChildren(fn)

Parameters

  • fn (Function) — Function to wrap.

Return

Function — Wrapped fn.

function fn(child, index, parent)

Modifier for children of parent.

Parameters

  • child (Node) — Current iteration;

  • index (number) — Position of child in parent;

  • parent (Node) — Parent node of child.

Returns

number (optional) — Next position to iterate.

function plugin(parent)

Function invoking fn for each child of parent.

Parameters

  • parent (Node) — Node with children.

Throws

  • Error — When not given a parent node.

License

MIT © Titus Wormer

Keywords

FAQs

Package last updated on 23 Aug 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc