Skip to content

Commit 7811233

Browse files
committed
Refactor code-style
1 parent 855818d commit 7811233

File tree

3 files changed

+203
-205
lines changed

3 files changed

+203
-205
lines changed

lib/index.js

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,105 @@
22
* @typedef {import('unist').Node} Node
33
* @typedef {import('unist').Parent} Parent
44
* @typedef {import('unist-util-is').Test} Test
5-
*
5+
*/
6+
7+
/**
68
* @typedef Options
79
* Configuration.
810
* @property {boolean | null | undefined} [cascade=true]
911
* Whether to drop parent nodes if they had children, but all their children
10-
* were filtered out.
12+
* were filtered out (default: `true`).
1113
*/
1214

1315
import {convert} from 'unist-util-is'
1416

15-
/** @type {Array<unknown>} */
16-
const empty = []
17-
1817
/**
1918
* Change the given `tree` by removing all nodes that pass `test`.
2019
*
2120
* The tree is walked in preorder (NLR), visiting the node itself, then its
2221
* head, etc.
2322
*
24-
* @param tree
23+
* @template {Node} Tree
24+
* Node kind.
25+
*
26+
* @overload
27+
* @param {Tree} node
28+
* @param {Test} [test]
29+
* @returns {Tree | undefined}
30+
*
31+
* @overload
32+
* @param {Tree} node
33+
* @param {Options | null | undefined} options
34+
* @param {Test} [test]
35+
* @returns {Tree | undefined}
36+
*
37+
* @param {Tree} tree
2538
* Tree to change.
26-
* @param options
39+
* @param {Options | Test} options
2740
* Configuration (optional).
28-
* @param test
41+
* @param {Test} [test]
2942
* `unist-util-is` compatible test.
30-
* @returns
43+
* @returns {Tree | undefined}
3144
* The given `tree` without nodes that pass `test`.
3245
*
33-
* `null` is returned if `tree` itself didn’t pass the test or is cascaded
34-
* away.
46+
* `undefined` is returned if `tree` itself didn’t pass the test or is
47+
* cascaded away.
3548
*/
3649
// To do: next major: don’t return `tree`.
37-
export const remove =
38-
/**
39-
* @type {(
40-
* (<Tree extends Node>(node: Tree, options: Options, test: Test) => Tree | null) &
41-
* (<Tree extends Node>(node: Tree, test: Test) => Tree | null)
42-
* )}
43-
*/
44-
(
45-
/**
46-
* @param {Node} tree
47-
* @param {Options | null | undefined} [options]
48-
* @param {Test | null | undefined} [test]
49-
* @returns {Node | null}
50-
*/
51-
function (tree, options, test) {
52-
const is = convert(test || options)
53-
const cascade =
54-
!options || options.cascade === undefined || options.cascade === null
55-
? true
56-
: options.cascade
50+
export function remove(tree, options, test) {
51+
const is = convert(test || options)
52+
let cascade = true
5753

58-
return preorder(tree)
54+
if (
55+
options &&
56+
typeof options === 'object' &&
57+
'cascade' in options &&
58+
typeof options.cascade === 'boolean'
59+
) {
60+
cascade = options.cascade
61+
}
5962

60-
/**
61-
* Check and remove nodes recursively in preorder.
62-
* For each composite node, modify its children array in-place.
63-
*
64-
* @param {Node} node
65-
* @param {number | null | undefined} [index]
66-
* @param {Parent | null | undefined} [parent]
67-
* @returns {Node | null}
68-
*/
69-
function preorder(node, index, parent) {
70-
/** @type {Array<Node>} */
71-
// @ts-expect-error looks like a parent.
72-
const children = node.children || empty
73-
let childIndex = -1
74-
let position = 0
63+
return preorder(tree)
7564

76-
if (is(node, index, parent)) {
77-
return null
78-
}
65+
/**
66+
* Check and remove nodes recursively in preorder.
67+
* For each composite node, modify its children array in-place.
68+
*
69+
* @template {Node} Kind
70+
* @param {Kind} node
71+
* @param {number | undefined} [index]
72+
* @param {Parent | undefined} [parent]
73+
* @returns {Kind | undefined}
74+
*/
75+
function preorder(node, index, parent) {
76+
if (is(node, index, parent)) {
77+
return undefined
78+
}
7979

80-
if (children.length > 0) {
81-
// Move all living children to the beginning of the children array.
82-
while (++childIndex < children.length) {
83-
// @ts-expect-error looks like a parent.
84-
if (preorder(children[childIndex], childIndex, node)) {
85-
children[position++] = children[childIndex]
86-
}
87-
}
80+
if ('children' in node && Array.isArray(node.children)) {
81+
const nodeAsParent = /** @type {Parent} */ (node)
82+
const children = nodeAsParent.children
83+
let oldChildIndex = -1
84+
let newChildIndex = 0
8885

89-
// Cascade delete.
90-
if (cascade && !position) {
91-
return null
86+
if (children.length > 0) {
87+
// Move all living children to the beginning of the children array.
88+
while (++oldChildIndex < children.length) {
89+
if (preorder(children[oldChildIndex], oldChildIndex, nodeAsParent)) {
90+
children[newChildIndex++] = children[oldChildIndex]
9291
}
92+
}
9393

94-
// Drop other nodes.
95-
children.length = position
94+
// Cascade delete.
95+
if (cascade && !newChildIndex) {
96+
return undefined
9697
}
9798

98-
return node
99+
// Drop other nodes.
100+
children.length = newChildIndex
99101
}
100102
}
101-
)
103+
104+
return node
105+
}
106+
}

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const tree = u('root', [
8383
// Remove all nodes of type `leaf`.
8484
remove(tree, 'leaf')
8585

86-
console.dir(tree, {depth: null})
86+
console.dir(tree, {depth: undefined})
8787
```
8888

8989
Yields:
@@ -128,7 +128,8 @@ head, etc.
128128

129129
A changed given `tree`, without nodes that pass `test`.
130130

131-
`null` is returned if `tree` itself didn’t pass the test or is cascaded away.
131+
`undefined` is returned if `tree` itself didn’t pass the test or is cascaded
132+
away.
132133

133134
### `Options`
134135

0 commit comments

Comments
 (0)