# unist-util-flatmap

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

Latest version **1.0.0** (published 2018-07-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install unist-util-flatmap
pnpm add unist-util-flatmap
yarn add unist-util-flatmap
bun add unist-util-flatmap
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-07-21 |
| First published | 2018-07-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | staltz |
| Keywords | unist, textlint, util |

## Links

- npm: https://www.npmjs.com/package/unist-util-flatmap
- Repository: https://gitlab.com/staltz/unist-util-flatmap
- Homepage: https://gitlab.com/staltz/unist-util-flatmap#README
- Issues: https://gitlab.com/staltz/unist-util-flatmap/issues
- npm.io page: https://npm.io/package/unist-util-flatmap

## Alternatives

- [eslint-plugin-sonarjs](https://npm.io/package/eslint-plugin-sonarjs.md) — 2.9M weekly downloads
- [eslint-config-expo](https://npm.io/package/eslint-config-expo.md) — 1.5M weekly downloads
- [@matter/protocol](https://npm.io/package/@matter/protocol.md) — 63.5K weekly downloads
- [@eventcatalog/linter](https://npm.io/package/@eventcatalog/linter.md) — 24.8K weekly downloads
- [@inrupt/eslint-config-base](https://npm.io/package/@inrupt/eslint-config-base.md) — 4.5K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-07-21

## README

# 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](https://github.com/syntax-tree/unist).

## Installation

```sh
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.

```js
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

```sh
npm test
```

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/unist-util-flatmap · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
