Skip to content

Commit 62ee5c6

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types
1 parent 9ef08e0 commit 62ee5c6

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

lib/index.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @typedef Options
77
* Configuration (optional).
8-
* @property {boolean} [cascade=true]
8+
* @property {boolean | null | undefined} [cascade=true]
99
* Whether to drop parent nodes if they had children, but all their children
1010
* were filtered out.
1111
*/
@@ -27,59 +27,61 @@ const own = {}.hasOwnProperty
2727
* `unist-util-is`-compatible test (such as a type).
2828
* @returns
2929
* New filtered tree.
30+
*
3031
* `null` is returned if `tree` itself didn’t pass the test, or is cascaded
3132
* away.
3233
*/
3334
export const filter =
3435
/**
3536
* @type {(
36-
* (<Tree extends Node, Check extends Test>(node: Tree, options: Options, test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
37+
* (<Tree extends Node, Check extends Test>(node: Tree, options: Options | null | undefined, test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
3738
* (<Tree extends Node, Check extends Test>(node: Tree, test: Check) => import('../complex-types.js').Matches<Tree, Check>) &
38-
* (<Tree extends Node>(node: Tree, options?: Options) => Tree)
39+
* (<Tree extends Node>(node: Tree, options?: Options | null | undefined) => Tree)
3940
* )}
4041
*/
4142
(
4243
/**
4344
* @param {Node} tree
44-
* @param {Options} options
45-
* @param {Test} test
46-
* @returns {Node|null}
45+
* @param {Options | Test | null | undefined} [options]
46+
* @param {Test | null | undefined} [test]
47+
* @returns {Node | null}
4748
*/
4849
function (tree, options, test) {
4950
const is = convert(test || options)
51+
/** @type {boolean | null | undefined} */
52+
const cascadeRaw =
53+
options && typeof options === 'object' && 'cascade' in options
54+
? /** @type {boolean | null | undefined} */ (options.cascade)
55+
: undefined
5056
const cascade =
51-
options.cascade === undefined || options.cascade === null
52-
? true
53-
: options.cascade
57+
cascadeRaw === undefined || cascadeRaw === null ? true : cascadeRaw
5458

5559
return preorder(tree)
5660

5761
/**
5862
* @param {Node} node
59-
* @param {number|undefined} [index]
60-
* @param {Parent|undefined} [parent]
61-
* @returns {Node|null}
63+
* Current node.
64+
* @param {number | undefined} [index]
65+
* Index of `node` in `parent`.
66+
* @param {Parent | undefined} [parent]
67+
* Parent node.
68+
* @returns {Node | null}
69+
* Shallow copy of `node`.
6270
*/
6371
function preorder(node, index, parent) {
6472
/** @type {Array<Node>} */
6573
const children = []
66-
/** @type {number} */
67-
let childIndex
68-
/** @type {Node} */
69-
let result
70-
/** @type {string} */
71-
let key
7274

7375
if (!is(node, index, parent)) return null
7476

7577
// @ts-expect-error: Looks like a parent.
7678
if (node.children) {
77-
childIndex = -1
79+
let childIndex = -1
7880

7981
// @ts-expect-error Looks like a parent.
8082
while (++childIndex < node.children.length) {
8183
// @ts-expect-error Looks like a parent.
82-
result = preorder(node.children[childIndex], childIndex, node)
84+
const result = preorder(node.children[childIndex], childIndex, node)
8385

8486
if (result) {
8587
children.push(result)
@@ -95,6 +97,8 @@ export const filter =
9597
/** @type {typeof node} */
9698
// @ts-expect-error all the fields will be copied over.
9799
const next = {}
100+
/** @type {string} */
101+
let key
98102

99103
for (key in node) {
100104
if (own.call(node, key)) {

0 commit comments

Comments
 (0)