From db5ea4657fe9ce0d241f8cc0a7e08e254e6b7739 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 7 Aug 2023 15:50:51 +0200 Subject: [PATCH] Change to yield `undefined` --- lib/index.js | 10 ++++------ readme.md | 3 ++- test/select.js | 26 +++++++++++++------------- test/svg.js | 2 +- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1cd11af..45a4b47 100644 --- a/lib/index.js +++ b/lib/index.js @@ -87,17 +87,15 @@ export function matches(selector, node, space) { * Tree to search (optional). * @param {Space | null | undefined} [space='html'] * Name of namespace (default: `'html'`). - * @returns {Element | null} - * First element in `tree` that matches `selector` or `null` if nothing is - * found. - * This could be `tree` itself. + * @returns {Element | undefined} + * First element in `tree` that matches `selector` or `undefined` if nothing + * is found; this could be `tree` itself. */ export function select(selector, tree, space) { const state = createState(selector, tree, space) state.one = true walk(state, tree || undefined) - // To do in major: return `undefined` instead. - return state.results[0] || null + return state.results[0] } /** diff --git a/readme.md b/readme.md index ceef9fd..d58b178 100644 --- a/readme.md +++ b/readme.md @@ -160,7 +160,8 @@ Searches the tree in *[preorder][]*. ###### Returns -First element in `tree` that matches `selector` or `null` if nothing is found. +First element in `tree` that matches `selector` or `undefined` if nothing is +found. This could be `tree` itself. ###### Example diff --git a/test/select.js b/test/select.js index 6f7655b..a4628be 100644 --- a/test/select.js +++ b/test/select.js @@ -74,13 +74,13 @@ test('select.select()', async function (t) { ) await t.test('should not select if not given a node', async function () { - assert.equal(select('*'), null) + assert.equal(select('*'), undefined) }) await t.test( 'should not select if not given an element', async function () { - assert.equal(select('*', {type: 'text', value: 'a'}), null) + assert.equal(select('*', {type: 'text', value: 'a'}), undefined) } ) @@ -219,7 +219,7 @@ test('select.select()', async function (t) { h('p', 'Delta') ]) ), - null + undefined ) }) }) @@ -290,7 +290,7 @@ test('select.select()', async function (t) { 'h1 ~ p', u('root', [h('p', 'Alpha'), h('h1', 'Bravo'), h('h2', 'Charlie')]) ), - null + undefined ) }) }) @@ -321,7 +321,7 @@ test('select.select()', async function (t) { 'h1:first-child', u('root', [h('p', 'Alpha'), h('h1', 'Bravo'), h('p', 'Charlie')]) ), - null + undefined ) } ) @@ -352,7 +352,7 @@ test('select.select()', async function (t) { 'h1:last-child', u('root', [h('p', 'Alpha'), h('h1', 'Bravo'), h('p', 'Charlie')]) ), - null + undefined ) } ) @@ -383,7 +383,7 @@ test('select.select()', async function (t) { 'h1:only-child', u('root', [h('p', 'Alpha'), h('h1', 'Bravo'), h('p', 'Charlie')]) ), - null + undefined ) } ) @@ -739,7 +739,7 @@ test('select.select()', async function (t) { ) await t.test('should return nothing without matches', async function () { - assert.equal(select('dt:first-of-type', h('dl', [])), null) + assert.equal(select('dt:first-of-type', h('dl', [])), undefined) }) }) @@ -765,7 +765,7 @@ test('select.select()', async function (t) { ) await t.test('should return nothing without matches', async function () { - assert.equal(select('dt:last-of-type', h('dl', [])), null) + assert.equal(select('dt:last-of-type', h('dl', [])), undefined) }) }) @@ -800,13 +800,13 @@ test('select.select()', async function (t) { h('dd', 'Foxtrot') ]) ), - null + undefined ) } ) await t.test('should return nothing without matches', async function () { - assert.equal(select('dt:only-of-type', h('dl', [])), null) + assert.equal(select('dt:only-of-type', h('dl', [])), undefined) }) }) }) @@ -885,7 +885,7 @@ test('select.select()', async function (t) { ]) ]) ), - null + undefined ) } ) @@ -900,7 +900,7 @@ test('select.select()', async function (t) { 'p:read-only', u('root', [h('div', {contentEditable: 'true'}, [h('p', 'A')])]) ), - null + undefined ) } ) diff --git a/test/svg.js b/test/svg.js index 18fe786..ec10113 100644 --- a/test/svg.js +++ b/test/svg.js @@ -46,7 +46,7 @@ test('svg', async function (t) { await t.test('should match svg (#3)', async function () { assert.deepEqual( select('[writing-mode]', s('text', {writingMode: 'lr-tb'}, '!')), - null + undefined ) })