Skip to content

Commit 3454b75

Browse files
committed
Refactor code-style
1 parent ec79ffa commit 3454b75

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {convert} from 'unist-util-is'
1616
* @property {boolean} [cascade=true] Whether to drop parent nodes if they had children, but all their children were filtered out.
1717
*/
1818

19-
var own = {}.hasOwnProperty
19+
const own = {}.hasOwnProperty
2020

2121
export const filter =
2222
/**
@@ -38,8 +38,8 @@ export const filter =
3838
* @returns {Node|null}
3939
*/
4040
function (tree, options, test) {
41-
var is = convert(test || options)
42-
var cascade =
41+
const is = convert(test || options)
42+
const cascade =
4343
options.cascade === undefined || options.cascade === null
4444
? true
4545
: options.cascade
@@ -54,15 +54,13 @@ export const filter =
5454
*/
5555
function preorder(node, index, parent) {
5656
/** @type {Array.<Node>} */
57-
var children = []
57+
const children = []
5858
/** @type {number} */
59-
var childIndex
59+
let childIndex
6060
/** @type {Node} */
61-
var result
62-
/** @type {typeof node} */
63-
var next
61+
let result
6462
/** @type {string} */
65-
var key
63+
let key
6664

6765
if (!is(node, index, parent)) return null
6866

@@ -81,12 +79,14 @@ export const filter =
8179
}
8280

8381
// @ts-ignore Looks like a parent.
84-
if (cascade && node.children.length && !children.length) return null
82+
if (cascade && node.children.length > 0 && children.length === 0)
83+
return null
8584
}
8685

8786
// Create a shallow clone, using the new children.
87+
/** @type {typeof node} */
8888
// @ts-ignore all the fields will be copied over.
89-
next = {}
89+
const next = {}
9090

9191
for (key in node) {
9292
/* istanbul ignore else - Prototype injection. */

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@
6868
"trailingComma": "none"
6969
},
7070
"xo": {
71-
"prettier": true,
72-
"rules": {
73-
"no-var": "off",
74-
"prefer-arrow-callback": "off",
75-
"unicorn/explicit-length-check": "off"
76-
}
71+
"prettier": true
7772
},
7873
"remarkConfig": {
7974
"plugins": [

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ npm install unist-util-filter
2828
import {u} from 'unist-builder'
2929
import {filter} from 'unist-util-filter'
3030

31-
var tree = u('root', [
31+
const tree = u('root', [
3232
u('leaf', '1'),
3333
u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]),
3434
u('leaf', '4')
3535
])
3636

37-
var newTree = filter(tree, node => node.type !== 'leaf' || node.value % 2 === 0)
37+
const newTree = filter(tree, node => node.type !== 'leaf' || node.value % 2 === 0)
3838

3939
console.dir(newTree, {depth: null})
4040
```

test.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import test from 'tape'
77
import {u} from 'unist-builder'
88
import {filter} from './index.js'
99

10-
test('should not traverse into children of filtered out nodes', function (t) {
11-
var tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
12-
var types = {}
10+
test('should not traverse into children of filtered out nodes', (t) => {
11+
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
12+
const types = {}
1313

1414
t.deepEqual(filter(tree, predicate), u('root', [u('leaf', '2')]))
1515
t.deepEqual(types, {root: 1, node: 1, leaf: 1})
@@ -25,8 +25,8 @@ test('should not traverse into children of filtered out nodes', function (t) {
2525
}
2626
})
2727

28-
test('should return `null` if root node is filtered out', function (t) {
29-
var tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
28+
test('should return `null` if root node is filtered out', (t) => {
29+
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
3030

3131
t.deepEqual(filter(tree, predicate), null)
3232

@@ -37,8 +37,8 @@ test('should return `null` if root node is filtered out', function (t) {
3737
}
3838
})
3939

40-
test('should cascade-remove parent nodes', function (t) {
41-
var tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
40+
test('should cascade-remove parent nodes', (t) => {
41+
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
4242

4343
t.deepEqual(filter(tree, notOne), u('root', [u('leaf', '2')]))
4444
t.deepEqual(filter(tree, notLeaf), null)
@@ -61,18 +61,18 @@ test('should cascade-remove parent nodes', function (t) {
6161
}
6262
})
6363

64-
test('should not cascade-remove nodes that were empty initially', function (t) {
65-
var tree = u('node', [u('node', []), u('node', [u('leaf')])])
64+
test('should not cascade-remove nodes that were empty initially', (t) => {
65+
const tree = u('node', [u('node', []), u('node', [u('leaf')])])
6666

6767
t.deepEqual(filter(tree, 'node'), u('node', [u('node', [])]))
6868

6969
t.end()
7070
})
7171

72-
test('should call iterator with `index` and `parent` args', function (t) {
73-
var tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
72+
test('should call iterator with `index` and `parent` args', (t) => {
73+
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
7474
/** @type {Array.<[Node, number|undefined, Parent|undefined]>} */
75-
var callLog = []
75+
const callLog = []
7676

7777
t.deepEqual(filter(tree, predicate), tree)
7878

@@ -92,8 +92,8 @@ test('should call iterator with `index` and `parent` args', function (t) {
9292
}
9393
})
9494

95-
test('should support type and node tests', function (t) {
96-
var tree = u('node', [u('node', [u('leaf', '1')]), u('leaf', '2')])
95+
test('should support type and node tests', (t) => {
96+
const tree = u('node', [u('node', [u('leaf', '1')]), u('leaf', '2')])
9797

9898
t.deepEqual(filter(tree, 'node'), null)
9999
t.deepEqual(
@@ -105,8 +105,8 @@ test('should support type and node tests', function (t) {
105105
t.end()
106106
})
107107

108-
test('opts.cascade', function (t) {
109-
var tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
108+
test('opts.cascade', (t) => {
109+
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
110110

111111
t.deepEqual(
112112
filter(tree, {cascade: true}, predicate),
@@ -130,8 +130,8 @@ test('opts.cascade', function (t) {
130130
}
131131
})
132132

133-
test('example from README', function (t) {
134-
var tree = u('root', [
133+
test('example from README', (t) => {
134+
const tree = u('root', [
135135
u('leaf', '1'),
136136
u('node', [u('leaf', '2'), u('node', [u('leaf', '3')])]),
137137
u('leaf', '4')

0 commit comments

Comments
 (0)