Skip to content

Commit c3ef223

Browse files
committed
Refactor code-style
1 parent 389264c commit c3ef223

File tree

12 files changed

+332
-374
lines changed

12 files changed

+332
-374
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
.DS_Store
2+
.nyc_output/
13
/node_modules/
4+
coverage/

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage/

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ language: node_js
22
node_js:
33
- lts/boron
44
- node
5+
after_script: bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 0 additions & 113 deletions
This file was deleted.

index.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,52 @@
1-
'use strict';
1+
'use strict'
22

3-
var flatmap = require('flatmap'),
4-
is = require('unist-util-is');
3+
var flatmap = require('flatmap')
4+
var is = require('unist-util-is')
55

6+
module.exports = filter
67

7-
module.exports = function (ast, opts, predicate) {
8-
if (arguments.length == 2) {
9-
predicate = opts;
10-
opts = {};
8+
function filter(tree, opts, test) {
9+
var cascade
10+
11+
if (!test) {
12+
test = opts
13+
opts = {}
1114
}
12-
opts.cascade = opts.cascade || opts.cascade === undefined;
1315

14-
return (function preorder (node, index, parent) {
15-
if (!is(predicate, node, index, parent)) {
16-
return null;
16+
cascade = opts.cascade
17+
cascade = cascade === null || cascade === undefined ? true : cascade
18+
19+
return preorder(tree, null, null)
20+
21+
function preorder(node, index, parent) {
22+
var next
23+
24+
if (!is(test, node, index, parent)) {
25+
return null
1726
}
1827

19-
var newNode = Object.keys(node).reduce(function (acc, key) {
20-
if (key != 'children') {
21-
acc[key] = node[key];
22-
}
23-
return acc;
24-
}, {});
28+
next = Object.keys(node).reduce(reduce, {})
2529

2630
if (node.children) {
27-
newNode.children = flatmap(node.children, function (child, index) {
28-
return preorder(child, index, node);
29-
});
31+
next.children = flatmap(node.children, map)
32+
33+
if (cascade && node.children.length !== 0 && next.children.length === 0) {
34+
return null
35+
}
36+
}
37+
38+
return next
3039

31-
if (opts.cascade && !newNode.children.length && node.children.length) {
32-
return null;
40+
function reduce(acc, key) {
41+
if (key !== 'children') {
42+
acc[key] = node[key]
3343
}
44+
45+
return acc
3446
}
3547

36-
return newNode;
37-
}(ast, null, null));
38-
};
48+
function map(child, index) {
49+
return preorder(child, index, node)
50+
}
51+
}
52+
}

package.json

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,50 @@
2929
"unist-util-is": "^2.0.0"
3030
},
3131
"devDependencies": {
32+
"nyc": "^12.0.1",
33+
"prettier": "^1.13.3",
34+
"remark-cli": "^5.0.0",
35+
"remark-preset-wooorm": "^4.0.0",
3236
"tape": "^4.4.0",
3337
"unist-builder": "^1.0.0",
34-
"unist-util-select": "^1.3.0"
38+
"unist-util-select": "^1.3.0",
39+
"xo": "^0.21.1"
3540
},
3641
"scripts": {
37-
"test": "tape test/*.js"
42+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
43+
"test-api": "node test",
44+
"test-coverage": "nyc --reporter lcov tape test.js",
45+
"test": "npm run format && npm run test-coverage"
46+
},
47+
"nyc": {
48+
"check-coverage": true,
49+
"lines": 100,
50+
"functions": 100,
51+
"branches": 100
52+
},
53+
"prettier": {
54+
"tabWidth": 2,
55+
"useTabs": false,
56+
"singleQuote": true,
57+
"bracketSpacing": false,
58+
"semi": false,
59+
"trailingComma": "none"
60+
},
61+
"xo": {
62+
"prettier": true,
63+
"esnext": false,
64+
"rules": {
65+
"guard-for-in": "off",
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
68+
},
69+
"ignores": [
70+
"unist-util-position.js"
71+
]
72+
},
73+
"remarkConfig": {
74+
"plugins": [
75+
"preset-wooorm"
76+
]
3877
}
3978
}

readme.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# unist-util-filter [![Build Status][build-badge]][build-page] [![Coverage Status][coverage-badge]][coverage-page]
2+
3+
Create a new [unist][] tree with all nodes that pass the given test.
4+
5+
## Install
6+
7+
```sh
8+
npm install unist-util-filter
9+
```
10+
11+
## Usage
12+
13+
```js
14+
var filter = require('unist-util-filter');
15+
16+
var tree = {
17+
type: 'root',
18+
children: [
19+
{type: 'leaf', value: '1'},
20+
{
21+
type: 'node',
22+
children: [
23+
{type: 'leaf', value: '2'},
24+
{type: 'node', children: [{type: 'leaf', value: '3'}]}
25+
]
26+
},
27+
{type: 'leaf', value: '4'}
28+
]
29+
}
30+
31+
console.log(filter(tree, node => node.type != 'leaf' || node.value % 2 == 0))
32+
```
33+
34+
Yields:
35+
36+
```js
37+
{
38+
type: 'root',
39+
children: [
40+
{type: 'node', children: [{type: 'leaf', value: '2'}]},
41+
{type: 'leaf', value: '4'}
42+
]
43+
}
44+
```
45+
46+
## API
47+
48+
### `filter(tree, [opts], test)`
49+
50+
Creates a copy of `tree` consisting of all nodes that pass `test`.
51+
The tree is filtered in [preorder][].
52+
53+
###### Parameters
54+
55+
* `tree` ([`Node?`][node])
56+
— Tree to filter
57+
* `opts.cascade` (`boolean`, default: `true`)
58+
— Whether to drop parent nodes if they had children, but all their
59+
children were filtered out
60+
* `test`
61+
— See [`unist-util-is`][is] for details
62+
63+
###### Returns
64+
65+
A new tree ([`Node?`][node]) with nodes for which `test` returned `true`.
66+
`null` is returned if `tree` itself didn’t pass the test, or is cascaded away.
67+
68+
## Contribute
69+
70+
See [`contributing.md` in `syntax-tree/unist`][contributing] for ways to get
71+
started.
72+
73+
This organisation has a [Code of Conduct][coc]. By interacting with this
74+
repository, organisation, or community you agree to abide by its terms.
75+
76+
## License
77+
78+
[MIT][] © Eugene Sharygin
79+
80+
[mit]: LICENSE
81+
82+
[unist]: https://github.com/syntax-tree/unist
83+
84+
[node]: https://github.com/syntax-tree/unist#node
85+
86+
[is]: https://github.com/syntax-tree/unist-util-is
87+
88+
[preorder]: https://en.wikipedia.org/wiki/Tree_traversal
89+
90+
[build-page]: https://travis-ci.org/syntax-tree/unist-util-filter
91+
92+
[build-badge]: https://travis-ci.org/syntax-tree/unist-util-filter.svg?branch=master
93+
94+
[coverage-page]: https://codecov.io/github/syntax-tree/unist-util-filter?branch=master
95+
96+
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-filter.svg?branch=master
97+
98+
[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md
99+
100+
[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md

0 commit comments

Comments
 (0)