From 414d1f78e1ec306ef6af99f9574491ff128d37b3 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Thu, 29 Jun 2023 19:44:05 +0200 Subject: [PATCH] Change to yield `undefined` instead of `null` --- index.js | 5 ++--- readme.md | 2 +- test/select.js | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 7328dcf..80ef621 100644 --- a/index.js +++ b/index.js @@ -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. * @@ -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] } /** diff --git a/readme.md b/readme.md index b284c6e..36b8d4f 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/test/select.js b/test/select.js index adff76e..6b2d218 100644 --- a/test/select.js +++ b/test/select.js @@ -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 () { @@ -184,7 +184,7 @@ test('select.select()', async function (t) { u('b', 'Delta') ]) ), - null + undefined ) }) }) @@ -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 ) }) }) @@ -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 ) } ) @@ -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 ) } ) @@ -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 ) } ) @@ -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) }) }) @@ -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) }) }) @@ -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) }) })