Skip to content

Commit

Permalink
Change to remove support for non-nodes, non-names
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 1, 2023
1 parent 8aabe35 commit 9020a64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 41 deletions.
28 changes: 9 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,21 @@
const own = {}.hasOwnProperty

/**
* Check if `node`is an element and has a `field` property.
* Check if `node` is an element and has a `name` property.
*
* @param {unknown} node
* Thing to check (typically `Element`).
* @param {unknown} field
* Field name to check (typically `string`).
* @param {Nodes} node
* Node to check (typically `Element`).
* @param {string} name
* Property name to check.
* @returns {boolean}
* Whether `node` is an element that has a `field` property.
* Whether `node` is an element that has a `name` property.
*/
export function hasProperty(node, field) {
export function hasProperty(node, name) {
const value =
typeof field === 'string' &&
isNode(node) &&
node.type === 'element' &&
node.properties &&
own.call(node.properties, field) &&
node.properties[field]
own.call(node.properties, name) &&
node.properties[name]

return value !== null && value !== undefined && value !== false
}

/**
* @param {unknown} value
* @returns {value is Nodes}
*/
function isNode(value) {
return Boolean(value && typeof value === 'object' && 'type' in value)
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ Check if `node`is an element and has a `field` property.

###### Parameters

* `node` (`unknown`) — thing to check (typically [`Element`][element])
* `name` (`unknown`) - field name to check (typically `string`)
* `node` (`Node`) — node to check (typically [`Element`][element])
* `name` (`string`) - property name to check

###### Returns

Whether `node` is an element that has a `field` property (`boolean`).
Whether `node` is an element that has a `name` property (`boolean`).

## Types

Expand Down
29 changes: 10 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@ test('hasProperty', async function (t) {
])
})

await t.test('should return `false` without node', async function () {
assert.equal(hasProperty(null, 'alpha'), false)
})

await t.test('should return `false` without `element`', async function () {
assert.equal(hasProperty({type: 'text', value: 'alpha'}, 'bravo'), false)
})

await t.test('should return `false` without properties', async function () {
assert.equal(hasProperty({type: 'element'}, 'charlie'), false)
})

await t.test(
'should return `false` for prototypal properties',
async function () {
assert.equal(
hasProperty({type: 'element', properties: {}}, 'toString'),
hasProperty(
{type: 'element', tagName: 'a', properties: {}, children: []},
'toString'
),
false
)
}
Expand All @@ -38,7 +33,9 @@ test('hasProperty', async function (t) {
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
tagName: 'a',
properties: {id: 'delta'},
children: []
},
'echo'
),
Expand All @@ -47,22 +44,16 @@ test('hasProperty', async function (t) {
}
)

await t.test('should return `false` if without `name`', async function () {
assert.equal(
// @ts-expect-error: check how a missing name is handled.
hasProperty({type: 'element', properties: {id: 'delta'}}),
false
)
})

await t.test(
'should return `true` if the property does exist',
async function () {
assert.equal(
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
tagName: 'a',
properties: {id: 'delta'},
children: []
},
'id'
),
Expand Down

0 comments on commit 9020a64

Please sign in to comment.