Skip to content

Commit 65bb974

Browse files
committed
Change to yield undefined, not null
1 parent ed30722 commit 65bb974

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

index.test-d.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ expectType<Root>(filter(root, 'root'))
1818
expectType<Root>(filter(root, {}, 'root'))
1919
expectError(filter(root, {notAnOption: true}, 'root'))
2020
expectType<Root>(filter(root, {cascade: false}, 'root'))
21-
expectType<null>(filter(root, 'heading'))
22-
expectType<null>(filter(root, {cascade: false}, 'notAHeading'))
21+
expectType<undefined>(filter(root, 'heading'))
22+
expectType<undefined>(filter(root, {cascade: false}, 'notAHeading'))
2323

2424
// Vague types.
2525
expectType<Heading | Paragraph>(filter(headingOrParagraph))
26-
expectType<Paragraph | null>(filter(headingOrParagraph, 'paragraph'))
27-
expectType<null>(filter(headingOrParagraph, 'notAHeading'))
28-
expectType<Heading | null>(
26+
expectType<Paragraph | undefined>(filter(headingOrParagraph, 'paragraph'))
27+
expectType<undefined>(filter(headingOrParagraph, 'notAHeading'))
28+
expectType<Heading | undefined>(
2929
filter(headingOrParagraph, {cascade: false}, 'heading')
3030
)
3131

32-
expectType<Heading | Paragraph | null>(
32+
expectType<Heading | Paragraph | undefined>(
3333
filter(headingOrParagraph, {cascade: false}, function () {
3434
return Math.random() > 0.5
3535
})
3636
)
3737

38-
expectType<Heading | null>(
38+
expectType<Heading | undefined>(
3939
filter(
4040
headingOrParagraph,
4141
{cascade: false},
@@ -47,14 +47,14 @@ expectType<Heading | null>(
4747
// These don’t work well.
4848
// Use strict nodes types.
4949
expectType<Node>(filter(justSomeNode))
50-
expectType<null>(filter(justSomeNode, '???'))
51-
expectType<null>(filter(justSomeNode, {cascade: false}, '???'))
52-
expectType<Node | null>(
50+
expectType<undefined>(filter(justSomeNode, '???'))
51+
expectType<undefined>(filter(justSomeNode, {cascade: false}, '???'))
52+
expectType<Node | undefined>(
5353
filter(justSomeNode, {cascade: false}, function () {
5454
return Math.random() > 0.5
5555
})
5656
)
57-
expectType<null>(
57+
expectType<undefined>(
5858
filter(
5959
justSomeNode,
6060
{cascade: false},

lib/complex-types.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ type MatchesOne<Value, Check> =
1111
? Check extends (value: any) => value is infer Inferred
1212
? Value extends Inferred
1313
? Value
14-
: null
14+
: undefined
1515
: // This test function isn’t a type predicate.
16-
Value | null
16+
Value | undefined
1717
: // String (type) test.
1818
Value['type'] extends Check
1919
? Value extends {type: Check}
2020
? Value
21-
: Value | null
21+
: Value | undefined
2222
: // Partial test.
2323
Value extends Check
2424
? Value
25-
: null
26-
: null
25+
: undefined
26+
: undefined
2727

2828
export type Matches<Value, Check> =
2929
// Is this a list?

lib/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ const own = {}.hasOwnProperty
4747
* Configuration (optional).
4848
* @param {Test} [test]
4949
* `unist-util-is` compatible test.
50-
* @returns {Node | null}
50+
* @returns {Node | undefined}
5151
* New filtered tree.
5252
*
53-
* `null` is returned if `tree` itself didn’t pass the test, or is cascaded
54-
* away.
53+
* `undefined` is returned if `tree` itself didn’t pass the test, or is
54+
* cascaded away.
5555
*/
5656
export function filter(tree, options, test) {
5757
const is = convert(test || options)
@@ -71,14 +71,14 @@ export function filter(tree, options, test) {
7171
* Index of `node` in `parent`.
7272
* @param {Parent | undefined} [parentNode]
7373
* Parent node.
74-
* @returns {Node | null}
74+
* @returns {Node | undefined}
7575
* Shallow copy of `node`.
7676
*/
7777
function preorder(node, index, parentNode) {
7878
/** @type {Array<Node>} */
7979
const children = []
8080

81-
if (!is(node, index, parentNode)) return null
81+
if (!is(node, index, parentNode)) return undefined
8282

8383
if (parent(node)) {
8484
let childIndex = -1
@@ -91,8 +91,9 @@ export function filter(tree, options, test) {
9191
}
9292
}
9393

94-
if (cascade && node.children.length > 0 && children.length === 0)
95-
return null
94+
if (cascade && node.children.length > 0 && children.length === 0) {
95+
return undefined
96+
}
9697
}
9798

9899
// Create a shallow clone, using the new children.

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ head, etc.
115115

116116
###### Returns
117117

118-
New filtered tree ([`Node`][node] or `null`).
118+
New filtered tree ([`Node`][node] or `undefined`).
119119

120-
`null` is returned if `tree` itself didn’t pass the test, or is cascaded away.
120+
`undefined` is returned if `tree` itself didn’t pass the test, or is cascaded
121+
away.
121122

122123
### `Options`
123124

test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ test('filter', async function (t) {
3434
)
3535

3636
await t.test(
37-
'should return `null` if root node is filtered out',
37+
'should return `undefined` if root node is filtered out',
3838
function () {
3939
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
4040

41-
assert.deepEqual(filter(tree, predicate), null)
41+
assert.deepEqual(filter(tree, predicate), undefined)
4242

4343
function predicate() {
4444
return false
@@ -50,7 +50,7 @@ test('filter', async function (t) {
5050
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
5151

5252
assert.deepEqual(filter(tree, notOne), u('root', [u('leaf', '2')]))
53-
assert.deepEqual(filter(tree, notLeaf), null)
53+
assert.deepEqual(filter(tree, notLeaf), undefined)
5454

5555
/**
5656
* @param {Node} node
@@ -106,20 +106,20 @@ test('filter', async function (t) {
106106
await t.test('should support type and node tests', function () {
107107
const tree = u('node', [u('node', [u('leaf', '1')]), u('leaf', '2')])
108108

109-
assert.deepEqual(filter(tree, 'node'), null)
109+
assert.deepEqual(filter(tree, 'node'), undefined)
110110
assert.deepEqual(
111111
filter(tree, {cascade: false}, 'node'),
112112
u('node', [u('node', [])])
113113
)
114-
assert.deepEqual(filter(tree, {cascade: false}, 'leaf'), null)
114+
assert.deepEqual(filter(tree, {cascade: false}, 'leaf'), undefined)
115115
})
116116

117117
await t.test('opts.cascade', function () {
118118
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
119119

120120
assert.deepEqual(
121121
filter(tree, {cascade: true}, predicate),
122-
null,
122+
undefined,
123123
'opts.cascade = true'
124124
)
125125

0 commit comments

Comments
 (0)