Skip to content

Commit

Permalink
Change to yield undefined instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 29, 2023
1 parent a54914e commit 414d1f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function matches(selector, node) {
* CSS selector, such as (`heading`, `link, linkReference`).
* @param {Node | NodeLike | null | undefined} [tree]
* Tree to search.
* @returns {Node | null}
* @returns {Node | undefined}
* First node in `tree` that matches `selector` or `null` if nothing is
* found.
*
Expand All @@ -54,8 +54,7 @@ export function select(selector, tree) {
const state = createState(selector, tree)
state.one = true
walk(state, tree || undefined)
// To do next major: return `undefined`.
return state.results[0] || null
return state.results[0]
}

/**
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Searches the tree in *[preorder][]*.

###### Returns

First node in `tree` that matches `selector` or `null` if nothing is found.
First node in `tree` that matches `selector` or `undefined` if nothing is found.

This could be `tree` itself.

Expand Down
20 changes: 10 additions & 10 deletions test/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('select.select()', async function (t) {
)

await t.test('should yield nothing if not given a node', async function () {
assert.equal(select('*'), null)
assert.equal(select('*'), undefined)
})

await t.test('should yield the node if given a node', async function () {
Expand Down Expand Up @@ -184,7 +184,7 @@ test('select.select()', async function (t) {
u('b', 'Delta')
])
),
null
undefined
)
})
})
Expand Down Expand Up @@ -227,7 +227,7 @@ test('select.select()', async function (t) {
'c ~ b',
u('a', [u('b', 'Alpha'), u('c', 'Bravo'), u('d', 'Charlie')])
),
null
undefined
)
})
})
Expand Down Expand Up @@ -258,7 +258,7 @@ test('select.select()', async function (t) {
'c:first-child',
u('a', [u('b', 'Alpha'), u('c', 'Bravo'), u('d', 'Charlie')])
),
null
undefined
)
}
)
Expand Down Expand Up @@ -289,7 +289,7 @@ test('select.select()', async function (t) {
'c:last-child',
u('a', [u('b', 'Alpha'), u('c', 'Bravo'), u('d', 'Charlie')])
),
null
undefined
)
}
)
Expand Down Expand Up @@ -320,7 +320,7 @@ test('select.select()', async function (t) {
'c:only-child',
u('a', [u('b', 'Alpha'), u('c', 'Bravo'), u('d', 'Charlie')])
),
null
undefined
)
}
)
Expand Down Expand Up @@ -676,7 +676,7 @@ test('select.select()', async function (t) {
)

await t.test('should return nothing without matches', async function () {
assert.equal(select('b:first-of-type', u('a', [])), null)
assert.equal(select('b:first-of-type', u('a', [])), undefined)
})
})

Expand All @@ -702,7 +702,7 @@ test('select.select()', async function (t) {
)

await t.test('should return nothing without matches', async function () {
assert.equal(select('b:last-of-type', u('a', [])), null)
assert.equal(select('b:last-of-type', u('a', [])), undefined)
})
})

Expand Down Expand Up @@ -737,13 +737,13 @@ test('select.select()', async function (t) {
u('c', 'Foxtrot')
])
),
null
undefined
)
}
)

await t.test('should return nothing without matches', async function () {
assert.equal(select('b:only-of-type', u('a', [])), null)
assert.equal(select('b:only-of-type', u('a', [])), undefined)
})
})

Expand Down

0 comments on commit 414d1f7

Please sign in to comment.