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

unist-util-flatmap

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unist-util-flatmap

Create a new Unist tree by mapping (to an array) and flattening

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
decreased by-31.92%
Maintainers
1
Weekly downloads
 
Created
Source

unist-util-flatmap

Create a new Unist tree by mapping (to an array) with the provided function and then flattening.

Helper for creating unist: Universal Syntax Tree.

Installation

npm install unist-util-flatmap

Usage

flatMap(AST, (node, index, parent) => /* array */): AST

flatMap function returns new AST object, but the argument function should return an array of ASTs.

const assert = require('assert')
const assign = require('object-assign')
const flatMap = require('unist-util-flatmap')

// Input
const tree = {
  type: 'root',
  children: [
    {
      type: 'node',
      children: [{type: 'leaf', value: '1'}]
    },
    {type: 'leaf', value: '2'}
  ]
}

// Transform:
const actual = flatMap(tree, node => {
  if (node.type === 'leaf') {
    return [
      assign({}, node, {value: 'FIRST'}),
      assign({}, node, {value: 'SECOND'})
    ]
  }
  // No change
  return [node]
})

// Expected output:
const expected = {
  type: 'root',
  children: [
    {
      type: 'node',
      children: [
        {type: 'leaf', value: 'FIRST'},
        {type: 'leaf', value: 'SECOND'}
      ]
    },
    {type: 'leaf', value: 'FIRST'},
    {type: 'leaf', value: 'SECOND'}
  ]
}

assert.deepEqual(actual, expected)

Tests

npm test

License

MIT

Keywords

FAQs

Package last updated on 21 Jul 2018

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