Skip to content

Commit

Permalink
Change to yield undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 7, 2023
1 parent 87996c8 commit db5ea46
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
10 changes: 4 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

/**
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions test/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)

Expand Down Expand Up @@ -219,7 +219,7 @@ test('select.select()', async function (t) {
h('p', 'Delta')
])
),
null
undefined
)
})
})
Expand Down Expand Up @@ -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
)
})
})
Expand Down Expand Up @@ -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
)
}
)
Expand Down Expand Up @@ -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
)
}
)
Expand Down Expand Up @@ -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
)
}
)
Expand Down Expand Up @@ -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)
})
})

Expand All @@ -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)
})
})

Expand Down Expand Up @@ -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)
})
})
})
Expand Down Expand Up @@ -885,7 +885,7 @@ test('select.select()', async function (t) {
])
])
),
null
undefined
)
}
)
Expand All @@ -900,7 +900,7 @@ test('select.select()', async function (t) {
'p:read-only',
u('root', [h('div', {contentEditable: 'true'}, [h('p', 'A')])])
),
null
undefined
)
}
)
Expand Down
2 changes: 1 addition & 1 deletion test/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
})

Expand Down

0 comments on commit db5ea46

Please sign in to comment.