Verified Commit 898d82bd authored by staltz's avatar staltz
Browse files

initial

parents
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
node_modules
npm-debug.log
*.iml
.idea/
.vscode/
shrinkwrap.yaml
package-lock.json
yarn.lock
Copyright (c) 2018 Andre 'Staltz' Medeiros
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# 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)
module.exports = flatMap
function flatMap(ast, fn) {
return transform(ast, 0, null)[0]
function transform(node, index, parent) {
if (node.children) {
var out = []
for (var i = 0, n = node.children.length; i < n; i++) {
var xs = transform(node.children[i], i, node)
if (xs) {
for (var j = 0, m = xs.length; j < m; j++) {
out.push(xs[j])
}
}
}
node.children = out
}
return fn(node, index, parent)
}
}
{
"name": "unist-util-flatmap",
"version": "1.0.0",
"description": "Create a new Unist tree by mapping (to an array) and flattening",
"license": "MIT",
"keywords": [
"unist",
"textlint",
"util"
],
"repository": "https://gitlab.com/staltz/unist-util-flatmap",
"author": "<contact@staltz.com>",
"main": "index.js",
"devDependencies": {
"mocha": "^5.0.0",
"object-assign": "4.1.1",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write '**/*.js'",
"test-api": "mocha",
"test": "npm run format && npm run test-api"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
}
}
const assert = require('assert')
const assign = require('object-assign')
const flatMap = require('.')
describe('should not traverse into children of filtered out nodes', function() {
it('should map specified node', function() {
const ast = {
type: 'root',
children: [
{
type: 'node',
children: [{type: 'leaf', value: '1'}]
},
{type: 'leaf', value: '2'}
]
}
const actual = flatMap(ast, function(node) {
if (node.type === 'leaf') {
return [
assign({}, node, {value: 'FIRST'}),
assign({}, node, {value: 'SECOND'})
]
}
// No change
return [node]
})
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)
})
context('when return null', function() {
it('should map as empty object', function() {
const ast = {
type: 'root',
children: [
{
type: 'node',
children: [{type: 'leaf', value: '1'}]
},
{type: 'leaf', value: '2'}
]
}
const actual = flatMap(ast, function(node) {
if (node.type === 'leaf') {
return null
}
// No change
return [node]
})
const expected = {
type: 'root',
children: [
{
type: 'node',
children: []
}
]
}
assert.deepEqual(actual, expected)
})
})
context('when pass empty object', function() {
it('should work map', function() {
const ast = {}
const actual = flatMap(ast, function() {
return [
{
value: 'test'
}
]
})
const expected = {
value: 'test'
}
assert.deepEqual(actual, expected)
})
})
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment