hast-util-is-element
Advanced tools
Comparing version 2.1.2 to 2.1.3
158
index.d.ts
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('hast').Element} Element | ||
* | ||
* @typedef {string} TagName | ||
* @typedef {null|undefined|TagName|TestFunctionAnything|Array.<TagName|TestFunctionAnything>} Test | ||
*/ | ||
/** | ||
* @template {Element} T | ||
* @typedef {null|undefined|T['tagName']|TestFunctionPredicate<T>|Array.<T['tagName']|TestFunctionPredicate<T>>} PredicateTest | ||
*/ | ||
/** | ||
* Check if an element passes a test | ||
* @typedef {null | undefined | string | TestFunctionAnything | Array<string | TestFunctionAnything>} Test | ||
* Check for an arbitrary element, unaware of TypeScript inferral. | ||
* | ||
* @callback TestFunctionAnything | ||
* Check if an element passes a test, unaware of TypeScript inferral. | ||
* @param {Element} element | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* @returns {boolean|void} | ||
* An element. | ||
* @param {number | null | undefined} [index] | ||
* The element’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The element’s parent. | ||
* @returns {boolean | void} | ||
* Whether this element passes the test. | ||
*/ | ||
/** | ||
* Check if an element passes a certain node test | ||
* @template {Element} T | ||
* Element type. | ||
* @typedef {T['tagName'] | TestFunctionPredicate<T> | Array<T['tagName'] | TestFunctionPredicate<T>>} PredicateTest | ||
* Check for an element that can be inferred by TypeScript. | ||
*/ | ||
/** | ||
* Check if an element passes a certain node test. | ||
* | ||
* @template {Element} X | ||
* @template {Element} T | ||
* Element type. | ||
* @callback TestFunctionPredicate | ||
* Complex test function for an element that can be inferred by TypeScript. | ||
* @param {Element} element | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* @returns {element is X} | ||
* An element. | ||
* @param {number | null | undefined} [index] | ||
* The element’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The element’s parent. | ||
* @returns {element is T} | ||
* Whether this element passes the test. | ||
*/ | ||
/** | ||
* Check if a node is an element and passes a certain node test | ||
* | ||
* @callback AssertAnything | ||
* Check that an arbitrary value is an element, unaware of TypeScript inferral. | ||
* @param {unknown} [node] | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* Anything (typically a node). | ||
* @param {number | null | undefined} [index] | ||
* The node’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The node’s parent. | ||
* @returns {boolean} | ||
* Whether this is an element and passes a test. | ||
*/ | ||
@@ -44,20 +57,35 @@ /** | ||
* | ||
* @template {Element} Y | ||
* @template {Element} T | ||
* Element type. | ||
* @callback AssertPredicate | ||
* Check that an arbitrary value is a specific element, aware of TypeScript. | ||
* @param {unknown} [node] | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* @returns {node is Y} | ||
* Anything (typically a node). | ||
* @param {number | null | undefined} [index] | ||
* The node’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The node’s parent. | ||
* @returns {node is T} | ||
* Whether this is an element and passes a test. | ||
*/ | ||
/** | ||
* Check if `node` is an `Element` and whether it passes the given test. | ||
* | ||
* @param node | ||
* Thing to check, typically `Node`. | ||
* @param test | ||
* A check for a specific element. | ||
* @param index | ||
* The node’s position in its parent. | ||
* @param parent | ||
* The node’s parent. | ||
* @returns | ||
* Whether `node` is an element and passes a test. | ||
*/ | ||
export const isElement: (() => false) & | ||
(<T extends import('hast').Element = import('hast').Element>( | ||
node: unknown, | ||
test?: PredicateTest<T>, | ||
index?: number | undefined, | ||
parent?: | ||
| import('unist').Parent< | ||
import('unist').Node<import('unist').Data>, | ||
import('unist').Data | ||
> | ||
| undefined, | ||
test?: PredicateTest<T> | undefined, | ||
index?: number, | ||
parent?: Parent, | ||
context?: unknown | ||
@@ -68,11 +96,23 @@ ) => node is T) & | ||
test: Test, | ||
index?: number | undefined, | ||
parent?: | ||
| import('unist').Parent< | ||
import('unist').Node<import('unist').Data>, | ||
import('unist').Data | ||
> | ||
| undefined, | ||
index?: number, | ||
parent?: Parent, | ||
context?: unknown | ||
) => boolean) | ||
/** | ||
* Generate an assertion from a test. | ||
* | ||
* Useful if you’re going to test many nodes, for example when creating a | ||
* utility where something else passes a compatible test. | ||
* | ||
* The created function is a bit faster because it expects valid input only: | ||
* a `node`, `index`, and `parent`. | ||
* | ||
* @param test | ||
* * When nullish, checks if `node` is an `Element`. | ||
* * When `string`, works like passing `(element) => element.tagName === test`. | ||
* * When `function` checks if function passed the element is true. | ||
* * When `array`, checks any one of the subtests pass. | ||
* @returns | ||
* An assertion. | ||
*/ | ||
export const convertElement: (<T extends import('hast').Element>( | ||
@@ -82,20 +122,15 @@ test: T['tagName'] | TestFunctionPredicate<T> | ||
((test?: Test) => AssertAnything) | ||
export type Node = import('unist').Node | ||
export type Parent = import('unist').Parent | ||
export type Element = import('hast').Element | ||
export type TagName = string | ||
/** | ||
* Check for an arbitrary element, unaware of TypeScript inferral. | ||
*/ | ||
export type Test = | ||
| null | ||
| undefined | ||
| TagName | ||
| string | ||
| TestFunctionAnything | ||
| Array<TagName | TestFunctionAnything> | ||
export type PredicateTest<T extends import('hast').Element> = | ||
| null | ||
| undefined | ||
| T['tagName'] | ||
| TestFunctionPredicate<T> | ||
| Array<T['tagName'] | TestFunctionPredicate<T>> | ||
| Array<string | TestFunctionAnything> | ||
/** | ||
* Check if an element passes a test | ||
* Check if an element passes a test, unaware of TypeScript inferral. | ||
*/ | ||
@@ -108,11 +143,18 @@ export type TestFunctionAnything = ( | ||
/** | ||
* Check if an element passes a certain node test | ||
* Check for an element that can be inferred by TypeScript. | ||
*/ | ||
export type TestFunctionPredicate<X extends import('hast').Element> = ( | ||
export type PredicateTest<T extends import('hast').Element> = | ||
| T['tagName'] | ||
| TestFunctionPredicate<T> | ||
| Array<T['tagName'] | TestFunctionPredicate<T>> | ||
/** | ||
* Complex test function for an element that can be inferred by TypeScript. | ||
*/ | ||
export type TestFunctionPredicate<T extends import('hast').Element> = ( | ||
element: Element, | ||
index?: number | null | undefined, | ||
parent?: Parent | null | undefined | ||
) => element is X | ||
) => element is T | ||
/** | ||
* Check if a node is an element and passes a certain node test | ||
* Check that an arbitrary value is an element, unaware of TypeScript inferral. | ||
*/ | ||
@@ -125,8 +167,8 @@ export type AssertAnything = ( | ||
/** | ||
* Check if a node is an element and passes a certain node test | ||
* Check that an arbitrary value is a specific element, aware of TypeScript. | ||
*/ | ||
export type AssertPredicate<Y extends import('hast').Element> = ( | ||
export type AssertPredicate<T extends import('hast').Element> = ( | ||
node?: unknown, | ||
index?: number | null | undefined, | ||
parent?: Parent | null | undefined | ||
) => node is Y | ||
) => node is T |
154
index.js
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Parent} Parent | ||
* @typedef {import('hast').Element} Element | ||
* | ||
* @typedef {string} TagName | ||
* @typedef {null|undefined|TagName|TestFunctionAnything|Array.<TagName|TestFunctionAnything>} Test | ||
*/ | ||
/** | ||
* @template {Element} T | ||
* @typedef {null|undefined|T['tagName']|TestFunctionPredicate<T>|Array.<T['tagName']|TestFunctionPredicate<T>>} PredicateTest | ||
*/ | ||
/** | ||
* Check if an element passes a test | ||
* @typedef {null | undefined | string | TestFunctionAnything | Array<string | TestFunctionAnything>} Test | ||
* Check for an arbitrary element, unaware of TypeScript inferral. | ||
* | ||
* @callback TestFunctionAnything | ||
* Check if an element passes a test, unaware of TypeScript inferral. | ||
* @param {Element} element | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* @returns {boolean|void} | ||
* An element. | ||
* @param {number | null | undefined} [index] | ||
* The element’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The element’s parent. | ||
* @returns {boolean | void} | ||
* Whether this element passes the test. | ||
*/ | ||
/** | ||
* Check if an element passes a certain node test | ||
* @template {Element} T | ||
* Element type. | ||
* @typedef {T['tagName'] | TestFunctionPredicate<T> | Array<T['tagName'] | TestFunctionPredicate<T>>} PredicateTest | ||
* Check for an element that can be inferred by TypeScript. | ||
*/ | ||
/** | ||
* Check if an element passes a certain node test. | ||
* | ||
* @template {Element} X | ||
* @template {Element} T | ||
* Element type. | ||
* @callback TestFunctionPredicate | ||
* Complex test function for an element that can be inferred by TypeScript. | ||
* @param {Element} element | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* @returns {element is X} | ||
* An element. | ||
* @param {number | null | undefined} [index] | ||
* The element’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The element’s parent. | ||
* @returns {element is T} | ||
* Whether this element passes the test. | ||
*/ | ||
/** | ||
* Check if a node is an element and passes a certain node test | ||
* | ||
* @callback AssertAnything | ||
* Check that an arbitrary value is an element, unaware of TypeScript inferral. | ||
* @param {unknown} [node] | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* Anything (typically a node). | ||
* @param {number | null | undefined} [index] | ||
* The node’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The node’s parent. | ||
* @returns {boolean} | ||
* Whether this is an element and passes a test. | ||
*/ | ||
@@ -49,16 +62,32 @@ | ||
* | ||
* @template {Element} Y | ||
* @template {Element} T | ||
* Element type. | ||
* @callback AssertPredicate | ||
* Check that an arbitrary value is a specific element, aware of TypeScript. | ||
* @param {unknown} [node] | ||
* @param {number|null|undefined} [index] | ||
* @param {Parent|null|undefined} [parent] | ||
* @returns {node is Y} | ||
* Anything (typically a node). | ||
* @param {number | null | undefined} [index] | ||
* The node’s position in its parent. | ||
* @param {Parent | null | undefined} [parent] | ||
* The node’s parent. | ||
* @returns {node is T} | ||
* Whether this is an element and passes a test. | ||
*/ | ||
// Check if `node` is an `element` and whether it passes the given test. | ||
/** | ||
* Check if `node` is an `Element` and whether it passes the given test. | ||
* | ||
* @param node | ||
* Thing to check, typically `Node`. | ||
* @param test | ||
* A check for a specific element. | ||
* @param index | ||
* The node’s position in its parent. | ||
* @param parent | ||
* The node’s parent. | ||
* @returns | ||
* Whether `node` is an element and passes a test. | ||
*/ | ||
export const isElement = | ||
/** | ||
* Check if a node is an element and passes a test. | ||
* When a `parent` node is known the `index` of node should also be given. | ||
* | ||
* @type {( | ||
@@ -72,14 +101,8 @@ * (() => false) & | ||
/** | ||
* Check if a node passes a test. | ||
* When a `parent` node is known the `index` of node should also be given. | ||
* | ||
* @param {unknown} [node] Node to check | ||
* @param {Test} [test] When nullish, checks if `node` is a `Node`. | ||
* When `string`, works like passing `function (node) {return node.type === test}`. | ||
* When `function` checks if function passed the node is true. | ||
* When `array`, checks any one of the subtests pass. | ||
* @param {number} [index] Position of `node` in `parent` | ||
* @param {Parent} [parent] Parent of `node` | ||
* @param {unknown} [context] Context object to invoke `test` with | ||
* @returns {boolean} Whether test passed and `node` is an `Element` (object with `type` set to `element` and `tagName` set to a non-empty string). | ||
* @param {unknown} [node] | ||
* @param {Test | undefined} [test] | ||
* @param {number | null | undefined} [index] | ||
* @param {Parent | null | undefined} [parent] | ||
* @param {unknown} [context] | ||
* @returns {boolean} | ||
*/ | ||
@@ -124,6 +147,23 @@ // eslint-disable-next-line max-params | ||
/** | ||
* Generate an assertion from a test. | ||
* | ||
* Useful if you’re going to test many nodes, for example when creating a | ||
* utility where something else passes a compatible test. | ||
* | ||
* The created function is a bit faster because it expects valid input only: | ||
* a `node`, `index`, and `parent`. | ||
* | ||
* @param test | ||
* * When nullish, checks if `node` is an `Element`. | ||
* * When `string`, works like passing `(element) => element.tagName === test`. | ||
* * When `function` checks if function passed the element is true. | ||
* * When `array`, checks any one of the subtests pass. | ||
* @returns | ||
* An assertion. | ||
*/ | ||
export const convertElement = | ||
/** | ||
* @type {( | ||
* (<T extends Element>(test: T['tagName']|TestFunctionPredicate<T>) => AssertPredicate<T>) & | ||
* (<T extends Element>(test: T['tagName'] | TestFunctionPredicate<T>) => AssertPredicate<T>) & | ||
* ((test?: Test) => AssertAnything) | ||
@@ -134,9 +174,3 @@ * )} | ||
/** | ||
* Generate an assertion from a check. | ||
* @param {Test} [test] | ||
* When nullish, checks if `node` is a `Node`. | ||
* When `string`, works like passing `function (node) {return node.type === test}`. | ||
* When `function` checks if function passed the node is true. | ||
* When `object`, checks that all keys in test are in node, and that they have (strictly) equal values. | ||
* When `array`, checks any one of the subtests pass. | ||
* @param {Test | null | undefined} [test] | ||
* @returns {AssertAnything} | ||
@@ -166,7 +200,9 @@ */ | ||
/** | ||
* @param {Array.<TagName|TestFunctionAnything>} tests | ||
* Handle multiple tests. | ||
* | ||
* @param {Array<string | TestFunctionAnything>} tests | ||
* @returns {AssertAnything} | ||
*/ | ||
function anyFactory(tests) { | ||
/** @type {Array.<AssertAnything>} */ | ||
/** @type {Array<AssertAnything>} */ | ||
const checks = [] | ||
@@ -183,3 +219,3 @@ let index = -1 | ||
* @this {unknown} | ||
* @param {unknown[]} parameters | ||
* @param {Array<unknown>} parameters | ||
* @returns {boolean} | ||
@@ -201,6 +237,5 @@ */ | ||
/** | ||
* Utility to convert a string into a function which checks a given node’s tag | ||
* name for said string. | ||
* Turn a string into a test for an element with a certain tag name. | ||
* | ||
* @param {TagName} check | ||
* @param {string} check | ||
* @returns {AssertAnything} | ||
@@ -221,2 +256,4 @@ */ | ||
/** | ||
* Turn a custom test into a test for an element that passes that test. | ||
* | ||
* @param {TestFunctionAnything} check | ||
@@ -231,3 +268,3 @@ * @returns {AssertAnything} | ||
* @param {unknown} node | ||
* @param {Array.<unknown>} parameters | ||
* @param {Array<unknown>} parameters | ||
* @returns {boolean} | ||
@@ -242,3 +279,4 @@ */ | ||
/** | ||
* Utility to return true if this is an element. | ||
* Make sure something is an element. | ||
* | ||
* @param {unknown} node | ||
@@ -245,0 +283,0 @@ * @returns {node is Element} |
{ | ||
"name": "hast-util-is-element", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "hast utility to check if a node is a (certain) element", | ||
@@ -39,21 +39,19 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^10.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"tsd": "^0.19.0", | ||
"tsd": "^0.25.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unified": "^10.0.0", | ||
"xo": "^0.47.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && tsd && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && tsd && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -60,0 +58,0 @@ }, |
294
readme.md
@@ -11,12 +11,45 @@ # hast-util-is-element | ||
[**hast**][hast] utility to check if a [*node*][node] is a (certain) | ||
[*element*][element]. | ||
[hast][] utility to check if a node is a (certain) element. | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`isElement(node[, test[, index, parent[, context]]])`](#iselementnode-test-index-parent-context) | ||
* [`convertElement(test)`](#convertelementtest) | ||
* [`AssertAnything`](#assertanything) | ||
* [`AssertPredicate`](#assertpredicate) | ||
* [`Test`](#test) | ||
* [`TestFunctionAnything`](#testfunctionanything) | ||
* [`PredicateTest`](#predicatetest) | ||
* [`TestFunctionPredicate`](#testfunctionpredicate) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This package is a small utility that checks that a node is a certain element. | ||
## When should I use this? | ||
Use this small utility if you find yourself repeating code for checking what | ||
elements nodes are. | ||
A similar package, [`unist-util-is`][unist-util-is], works on any unist node. | ||
For more advanced tests, [`hast-util-select`][hast-util-select] can be used | ||
to match against CSS selectors. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -26,2 +59,16 @@ npm install hast-util-is-element | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {isElement} from 'https://esm.sh/hast-util-is-element@2' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {isElement} from 'https://esm.sh/hast-util-is-element@2?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -33,5 +80,4 @@ | ||
isElement({type: 'text', value: 'foo'}) // => false | ||
isElement({type: 'element', tagName: 'a'}) // => true | ||
isElement({type: 'element', tagName: 'a'}, 'a') // => true | ||
isElement({type: 'element', tagName: 'a'}, ['a', 'area']) // => true | ||
@@ -42,3 +88,4 @@ ``` | ||
This package exports the following identifiers: `isElement`, `convertElement`. | ||
This package exports the identifiers [`isElement`][iselement] and | ||
[`convertElement`][convertelement]. | ||
There is no default export. | ||
@@ -48,48 +95,175 @@ | ||
Check if the given value is a (certain) [*element*][element]. | ||
Check if `node` is an `Element` and whether it passes the given test. | ||
* `node` ([`Node`][node]) — Node to check. | ||
* `test` ([`Function`][test], `string`, or `Array.<Test>`, optional) | ||
— When `array`, checks if any one of the subtests pass. | ||
When `string`, checks that the element has that tag name. | ||
When `function`, see [`test`][test] | ||
* `index` (`number`, optional) — [Index][] of `node` in `parent` | ||
* `parent` ([`Node`][node], optional) — [Parent][] of `node` | ||
* `context` (`*`, optional) — Context object to invoke `test` with | ||
###### Parameters | ||
* `node` (`unknown`) | ||
— thing to check, typically [`Node`][node] | ||
* `test` ([`Test`][test] or [`PredicateTest`][predicatetest], optional) | ||
— a check for a specific element | ||
* `index` (`number`, optional) | ||
— the node’s position in its parent | ||
* `parent` ([`Node`][node], optional) | ||
— the node’s parent | ||
* `context` (`any`, optional) | ||
— context object (`this`) to call `test` with | ||
###### Returns | ||
`boolean` — Whether `test` passed *and* `node` is an [`Element`][element]. | ||
Whether `node` is an [`Element`][element] and passes a test (`boolean`). | ||
###### Throws | ||
`Error` — When an incorrect `test`, `index`, or `parent` is given. | ||
A `node` that is not a node, or not an element, does not throw. | ||
When an incorrect `test`, `index`, or `parent` is given. | ||
There is no error thrown when `node` is not a node or not an element. | ||
#### `function test(element[, index, parent])` | ||
### `convertElement(test)` | ||
Generate a check from a test. | ||
Useful if you’re going to test many nodes, for example when creating a | ||
utility where something else passes a compatible test. | ||
The created function is a bit faster because it expects valid input only: | ||
a `node`, `index`, and `parent`. | ||
###### Parameters | ||
* `element` ([`Element`][element]) — Element to check | ||
* `index` (`number?`) — [Index][] of `node` in `parent` | ||
* `parent` ([`Node?`][node]) — [Parent][] of `node` | ||
* `test` ([`Test`][test] or [`PredicateTest`][predicatetest], optional) | ||
— a check for a specific element | ||
###### Context | ||
###### Returns | ||
`*` — The to `is` given `context`. | ||
An assertion ([`AssertAnything`][assertanything] or | ||
[`AssertPredicate`][assertpredicate]). | ||
### `AssertAnything` | ||
Check that an arbitrary value is an element, unaware of TypeScript inferral | ||
(TypeScript type). | ||
###### Parameters | ||
* `node` (`unknown`) | ||
— anything (typically a node) | ||
* `index` (`number`, optional) | ||
— the node’s position in its parent | ||
* `parent` ([`Node`][node], optional) | ||
— the node’s parent | ||
###### Returns | ||
`boolean?` — Whether `element` matches. | ||
Whether this is an element and passes a test (`boolean`). | ||
### `convertElement(test)` | ||
### `AssertPredicate` | ||
Create a test function from `test`, that can later be called with a `node`, | ||
`index`, and `parent`. | ||
Useful if you’re going to test many nodes, for example when creating a utility | ||
where something else passes a compatible test. | ||
Check that an arbitrary value is a specific element, aware of TypeScript | ||
(TypeScript type). | ||
The created function is slightly faster because it expects valid input only. | ||
Therefore, passing invalid input, yields unexpected results. | ||
###### Type parameters | ||
* `T` ([`Element`][element]) | ||
— element type | ||
###### Parameters | ||
* `node` (`unknown`) | ||
— anything (typically a node) | ||
* `index` (`number`, optional) | ||
— the node’s position in its parent | ||
* `parent` ([`Node`][node], optional) | ||
— the node’s parent | ||
###### Returns | ||
Whether this is an element and passes a test (`node is T`). | ||
### `Test` | ||
Check for an arbitrary element, unaware of TypeScript inferral (TypeScript | ||
type). | ||
###### Type | ||
```ts | ||
type Test = null | undefined | string | TestFunctionAnything | Array<string | TestFunctionAnything> | ||
``` | ||
Checks that the given thing is an element, and then: | ||
* when `string`, checks that the element has that tag name | ||
* when `function`, see [`TestFunctionAnything`][testfunctionanything] | ||
* when `Array`, checks if one of the subtests pass | ||
### `TestFunctionAnything` | ||
Check if an element passes a test, unaware of TypeScript inferral (TypeScript | ||
type). | ||
###### Parameters | ||
* `element` ([`Element`][element]) | ||
— an element | ||
* `index` (`number`, optional) | ||
— the element’s position in its parent | ||
* `parent` ([`Node`][node], optional) | ||
— the element’s parent | ||
###### Returns | ||
Whether this element passes the test (`boolean`). | ||
### `PredicateTest` | ||
Check for an element that can be inferred by TypeScript (TypeScript type). | ||
###### Type | ||
```ts | ||
type PredicateTest<T extends Element> = | ||
| T['tagName'] | ||
| TestFunctionPredicate<T> | ||
| Array<T['tagName'] | TestFunctionPredicate<T>> | ||
``` | ||
See [`TestFunctionPredicate`][testfunctionpredicate]. | ||
### `TestFunctionPredicate` | ||
Check if an element passes a certain node test (TypeScript type). | ||
###### Type parameters | ||
* `T` ([`Element`][element]) | ||
— element type | ||
###### Parameters | ||
* `element` ([`Element`][element]) | ||
— an element | ||
* `index` (`number`, optional) | ||
— the element’s position in its parent | ||
* `parent` ([`Node`][node], optional) | ||
— the element’s parent | ||
###### Returns | ||
Whether this element passes the test (`element is T`). | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types [`AssertAnything`][assertanything], | ||
[`AssertPredicate`][assertpredicate], [`Test`][test], | ||
[`TestFunctionAnything`][testfunctionanything], | ||
[`TestFunctionPredicate`][testfunctionpredicate], and | ||
[`PredicateTest`][predicatetest]. | ||
## Compatibility | ||
Projects maintained by the unified collective are compatible with all maintained | ||
versions of Node.js. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
## Security | ||
@@ -104,9 +278,9 @@ | ||
— check if a node has a property | ||
* [`hast-util-is-body-ok-link`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-body-ok-link) | ||
* [`hast-util-is-body-ok-link`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-body-ok-link) | ||
— check if a node is “Body OK” link element | ||
* [`hast-util-is-conditional-comment`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-conditional-comment) | ||
* [`hast-util-is-conditional-comment`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-conditional-comment) | ||
— check if a node is a conditional comment | ||
* [`hast-util-is-css-link`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-css-link) | ||
* [`hast-util-is-css-link`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-css-link) | ||
— check if a node is a CSS link element | ||
* [`hast-util-is-css-style`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-css-style) | ||
* [`hast-util-is-css-style`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-css-style) | ||
— check if a node is a CSS style element | ||
@@ -119,3 +293,3 @@ * [`hast-util-embedded`](https://github.com/syntax-tree/hast-util-embedded) | ||
— check if a node is interactive | ||
* [`hast-util-is-javascript`](https://github.com/rehypejs/rehype-minify/tree/HEAD/packages/hast-util-is-javascript) | ||
* [`hast-util-is-javascript`](https://github.com/rehypejs/rehype-minify/tree/main/packages/hast-util-is-javascript) | ||
— check if a node is a JavaScript script element | ||
@@ -137,4 +311,4 @@ * [`hast-util-labelable`](https://github.com/syntax-tree/hast-util-labelable) | ||
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get | ||
started. | ||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for | ||
ways to get started. | ||
See [`support.md`][support] for ways to get help. | ||
@@ -180,2 +354,8 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[typescript]: https://www.typescriptlang.org | ||
[license]: license | ||
@@ -185,8 +365,10 @@ | ||
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md | ||
[health]: https://github.com/syntax-tree/.github | ||
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md | ||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md | ||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md | ||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md | ||
[hast]: https://github.com/syntax-tree/hast | ||
@@ -198,8 +380,22 @@ | ||
[parent]: https://github.com/syntax-tree/unist#parent-1 | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting | ||
[index]: https://github.com/syntax-tree/unist#index | ||
[unist-util-is]: https://github.com/syntax-tree/unist-util-is | ||
[test]: #function-testelement-index-parent | ||
[hast-util-select]: https://github.com/syntax-tree/hast-util-select | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting | ||
[iselement]: #iselementnode-test-index-parent-context | ||
[convertelement]: #convertelementtest | ||
[assertanything]: #assertanything | ||
[assertpredicate]: #assertpredicate | ||
[test]: #test | ||
[testfunctionanything]: #testfunctionanything | ||
[predicatetest]: #predicatetest | ||
[testfunctionpredicate]: #testfunctionpredicate |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27463
10
426
391