diff --git a/lib/index.js b/lib/index.js index f063752..b091b92 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) -} diff --git a/readme.md b/readme.md index 3fe01ec..8fd1efb 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test.js b/test.js index 952f32b..2e31630 100644 --- a/test.js +++ b/test.js @@ -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 ) } @@ -38,7 +33,9 @@ test('hasProperty', async function (t) { hasProperty( { type: 'element', - properties: {id: 'delta'} + tagName: 'a', + properties: {id: 'delta'}, + children: [] }, 'echo' ), @@ -47,14 +44,6 @@ 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 () { @@ -62,7 +51,9 @@ test('hasProperty', async function (t) { hasProperty( { type: 'element', - properties: {id: 'delta'} + tagName: 'a', + properties: {id: 'delta'}, + children: [] }, 'id' ),