Skip to content

Commit 84450e1

Browse files
committed
Refactor to improve bundle size
1 parent 1c99087 commit 84450e1

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

index.js

+21-42
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,40 @@ var convert = require('unist-util-is/convert')
44

55
module.exports = remove
66

7-
function remove(ast, options, test) {
8-
var is
9-
var cascade
7+
function remove(tree, options, test) {
8+
var is = convert(test || options)
9+
var cascade = options.cascade == null ? true : options.cascade
1010

11-
if (!test) {
12-
test = options
13-
options = {}
14-
}
15-
16-
cascade = options.cascade
17-
cascade = cascade === null || cascade === undefined ? true : cascade
18-
is = convert(test)
19-
20-
return preorder(ast, null, null)
11+
return preorder(tree, null, null)
2112

2213
// Check and remove nodes recursively in preorder.
2314
// For each composite node, modify its children array in-place.
24-
function preorder(node, nodeIndex, parent) {
25-
var children
26-
var length
27-
var index
28-
var position
29-
var child
15+
function preorder(node, index, parent) {
16+
var children = node.children
17+
var childIndex = -1
18+
var position = 0
3019

31-
if (is(node, nodeIndex, parent)) {
20+
if (is(node, index, parent)) {
3221
return null
3322
}
3423

35-
children = node.children
36-
37-
if (!children || children.length === 0) {
38-
return node
39-
}
40-
41-
// Move all living children to the beginning of the children array.
42-
position = 0
43-
length = children.length
44-
index = -1
45-
46-
while (++index < length) {
47-
child = preorder(children[index], index, node)
24+
if (children && children.length) {
25+
// Move all living children to the beginning of the children array.
26+
while (++childIndex < children.length) {
27+
if (preorder(children[childIndex], childIndex, node)) {
28+
children[position++] = children[childIndex]
29+
}
30+
}
4831

49-
if (child) {
50-
children[position++] = child
32+
// Cascade delete.
33+
if (cascade && !position) {
34+
return null
5135
}
52-
}
5336

54-
// Cascade delete.
55-
if (cascade && position === 0) {
56-
return null
37+
// Drop other nodes.
38+
children.length = position
5739
}
5840

59-
// Drop other nodes.
60-
children.length = position
61-
6241
return node
6342
}
6443
}

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,18 @@
6666
},
6767
"xo": {
6868
"prettier": true,
69-
"esnext": false
69+
"esnext": false,
70+
"rules": {
71+
"eqeqeq": [
72+
"error",
73+
"always",
74+
{
75+
"null": "ignore"
76+
}
77+
],
78+
"no-eq-null": "off",
79+
"unicorn/explicit-length-check": "off"
80+
}
7081
},
7182
"remarkConfig": {
7283
"plugins": [

0 commit comments

Comments
 (0)