What is hast-util-has-property?
The `hast-util-has-property` package is a utility for checking if a given HAST (Hypertext Abstract Syntax Tree) node has a specific property. This is particularly useful when working with HAST nodes in the context of HTML processing and transformations.
What are hast-util-has-property's main functionalities?
Check if a node has a specific property
This feature allows you to check if a given HAST node has a specific property. In the example, the node is an anchor element with an `href` property, and the function checks for the presence of this property.
const hasProperty = require('hast-util-has-property');
const node = { type: 'element', tagName: 'a', properties: { href: 'https://example.com' } };
const result = hasProperty(node, 'href');
console.log(result); // true
Other packages similar to hast-util-has-property
unist-util-visit
The `unist-util-visit` package is used to recursively walk through a Unist syntax tree, which includes HAST. It allows you to visit nodes and perform actions on them. While it doesn't specifically check for properties, it can be used to traverse nodes and then check for properties manually.
hast-util-select
The `hast-util-select` package provides CSS-like selectors to find nodes in a HAST tree. It can be used to select nodes based on their properties, among other criteria. This package offers more advanced querying capabilities compared to `hast-util-has-property`.
hast-util-is-element
The `hast-util-is-element` package checks if a node is a specific element. While it doesn't directly check for properties, it can be used in conjunction with other utilities to achieve similar functionality.
hast-util-has-property
Check whether a HAST node is an element with a property.
Installation
npm:
npm install hast-util-has-property
hast-util-has-property is also available as an AMD, CommonJS, and
globals module, uncompressed and compressed.
Usage
Dependencies:
var hasProperty = require('hast-util-has-property');
Checks:
var a = hasProperty({
'type': 'text',
'value': 'alpha'
}, 'bravo');
var b = hasProperty({
'type': 'element',
'tagName': 'div',
'properties': {
'id': 'bravo'
},
'children': []
}, 'className');
var c = hasProperty({
'type': 'element',
'tagName': 'div',
'properties': {
'id': 'charlie'
},
'children': []
}, 'id');
Yields:
a: false
b: false
c: true
API
hasProperty(node, name)
Parameters:
node
(Node
, optional) — Node to check.name
(string
) - Property name to check.
Returns:
boolean
— Whether node
is an Element
with a property
by name
.
License
MIT © Titus Wormer