Skip to content

Commit 2ae0a55

Browse files
committed
Refactor code-style
1 parent b898080 commit 2ae0a55

File tree

4 files changed

+216
-256
lines changed

4 files changed

+216
-256
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

index.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,62 @@
1-
'use strict';
1+
'use strict'
22

3-
var is = require('unist-util-is');
3+
var is = require('unist-util-is')
44

5+
module.exports = remove
56

6-
module.exports = function (ast, opts, predicate) {
7-
if (arguments.length == 2) {
8-
predicate = opts;
9-
opts = {};
7+
function remove(ast, opts, test) {
8+
var cascade
9+
10+
if (!test) {
11+
test = opts
12+
opts = {}
1013
}
1114

12-
opts.cascade = opts.cascade || opts.cascade === undefined;
15+
cascade = opts.cascade
16+
cascade = cascade === null || cascade === undefined ? true : cascade
17+
18+
return preorder(ast, null, null)
1319

1420
// Check and remove nodes recursively in preorder.
1521
// For each composite node, modify its children array in-place.
16-
return (function preorder (node, nodeIndex, parent) {
17-
if (is(predicate, node, nodeIndex, parent)) {
18-
return null;
22+
function preorder(node, nodeIndex, parent) {
23+
var children
24+
var length
25+
var index
26+
var position
27+
var child
28+
29+
if (is(test, node, nodeIndex, parent)) {
30+
return null
1931
}
20-
if (!node.children || !node.children.length) {
21-
return node;
32+
33+
children = node.children
34+
35+
if (!children || children.length === 0) {
36+
return node
2237
}
2338

2439
// Move all living children to the beginning of the children array.
40+
position = 0
41+
length = children.length
42+
index = -1
2543

26-
var length = 0;
27-
28-
for (var index = 0; index < node.children.length; ++index) {
29-
var child = preorder(node.children[index], index, node);
44+
while (++index < length) {
45+
child = preorder(children[index], index, node)
3046

3147
if (child) {
32-
node.children[length++] = child;
48+
children[position++] = child
3349
}
3450
}
3551

36-
if (!length && opts.cascade) {
37-
// Cascade delete.
38-
return null;
52+
// Cascade delete.
53+
if (cascade && position === 0) {
54+
return null
3955
}
4056

41-
node.children.length = length;
42-
return node;
43-
}(ast, null, null));
44-
};
57+
// Drop other nodes.
58+
children.length = position
59+
60+
return node
61+
}
62+
}

package.json

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,34 @@
3030
"unist-util-is": "^2.0.0"
3131
},
3232
"devDependencies": {
33+
"prettier": "^1.13.3",
3334
"remark-cli": "^5.0.0",
3435
"remark-preset-wooorm": "^4.0.0",
3536
"tape": "^4.4.0",
36-
"unist-builder": "^1.0.0"
37+
"unist-builder": "^1.0.0",
38+
"xo": "^0.21.1"
3739
},
3840
"scripts": {
39-
"format": "remark . -qfo",
40-
"test": "tape test/*.js",
41-
"cov": "nyc --reporter lcov tape test/*.js"
41+
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
42+
"test": "node test",
43+
"cov": "nyc --reporter lcov tape test.js"
44+
},
45+
"prettier": {
46+
"tabWidth": 2,
47+
"useTabs": false,
48+
"singleQuote": true,
49+
"bracketSpacing": false,
50+
"semi": false,
51+
"trailingComma": "none"
52+
},
53+
"xo": {
54+
"prettier": true,
55+
"esnext": false,
56+
"rules": {
57+
"guard-for-in": "off",
58+
"no-var": "off",
59+
"prefer-arrow-callback": "off"
60+
}
4261
},
4362
"remarkConfig": {
4463
"plugins": [

0 commit comments

Comments
 (0)