Skip to content

Commit 9020a64

Browse files
committed
Change to remove support for non-nodes, non-names
1 parent 8aabe35 commit 9020a64

File tree

3 files changed

+22
-41
lines changed

3 files changed

+22
-41
lines changed

lib/index.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,21 @@
55
const own = {}.hasOwnProperty
66

77
/**
8-
* Check if `node`is an element and has a `field` property.
8+
* Check if `node` is an element and has a `name` property.
99
*
10-
* @param {unknown} node
11-
* Thing to check (typically `Element`).
12-
* @param {unknown} field
13-
* Field name to check (typically `string`).
10+
* @param {Nodes} node
11+
* Node to check (typically `Element`).
12+
* @param {string} name
13+
* Property name to check.
1414
* @returns {boolean}
15-
* Whether `node` is an element that has a `field` property.
15+
* Whether `node` is an element that has a `name` property.
1616
*/
17-
export function hasProperty(node, field) {
17+
export function hasProperty(node, name) {
1818
const value =
19-
typeof field === 'string' &&
20-
isNode(node) &&
2119
node.type === 'element' &&
2220
node.properties &&
23-
own.call(node.properties, field) &&
24-
node.properties[field]
21+
own.call(node.properties, name) &&
22+
node.properties[name]
2523

2624
return value !== null && value !== undefined && value !== false
2725
}
28-
29-
/**
30-
* @param {unknown} value
31-
* @returns {value is Nodes}
32-
*/
33-
function isNode(value) {
34-
return Boolean(value && typeof value === 'object' && 'type' in value)
35-
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ Check if `node`is an element and has a `field` property.
9797

9898
###### Parameters
9999

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

103103
###### Returns
104104

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

107107
## Types
108108

test.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,18 @@ test('hasProperty', async function (t) {
99
])
1010
})
1111

12-
await t.test('should return `false` without node', async function () {
13-
assert.equal(hasProperty(null, 'alpha'), false)
14-
})
15-
1612
await t.test('should return `false` without `element`', async function () {
1713
assert.equal(hasProperty({type: 'text', value: 'alpha'}, 'bravo'), false)
1814
})
1915

20-
await t.test('should return `false` without properties', async function () {
21-
assert.equal(hasProperty({type: 'element'}, 'charlie'), false)
22-
})
23-
2416
await t.test(
2517
'should return `false` for prototypal properties',
2618
async function () {
2719
assert.equal(
28-
hasProperty({type: 'element', properties: {}}, 'toString'),
20+
hasProperty(
21+
{type: 'element', tagName: 'a', properties: {}, children: []},
22+
'toString'
23+
),
2924
false
3025
)
3126
}
@@ -38,7 +33,9 @@ test('hasProperty', async function (t) {
3833
hasProperty(
3934
{
4035
type: 'element',
41-
properties: {id: 'delta'}
36+
tagName: 'a',
37+
properties: {id: 'delta'},
38+
children: []
4239
},
4340
'echo'
4441
),
@@ -47,22 +44,16 @@ test('hasProperty', async function (t) {
4744
}
4845
)
4946

50-
await t.test('should return `false` if without `name`', async function () {
51-
assert.equal(
52-
// @ts-expect-error: check how a missing name is handled.
53-
hasProperty({type: 'element', properties: {id: 'delta'}}),
54-
false
55-
)
56-
})
57-
5847
await t.test(
5948
'should return `true` if the property does exist',
6049
async function () {
6150
assert.equal(
6251
hasProperty(
6352
{
6453
type: 'element',
65-
properties: {id: 'delta'}
54+
tagName: 'a',
55+
properties: {id: 'delta'},
56+
children: []
6657
},
6758
'id'
6859
),

0 commit comments

Comments
 (0)