isElement
lang
Checks if value is likely a DOM element.
Installation
Import
import { isElement } from '@tulx/utils';Source Code
Implementation
/**
* Checks if value is likely a DOM element.
*
* @param value - The value to check.
* @returns Returns true if value is a DOM element, else false.
*
* @example
* ```ts
* isElement(document.body); // true
* isElement('<body>'); // false
* ```
*/
export function isElement(value: unknown): boolean {
if (typeof value !== 'object' || value === null) {
return false;
}
const obj = value as { nodeType?: number; nodeName?: unknown };
return obj.nodeType === 1 && typeof obj.nodeName === 'string';
}
Example
import { isElement } from '@tulx/utils';
isElement(document.body); // true
isElement('<body>'); // falseRelated Functions
castArray
Casts value as an array if it's not one.
clone
Creates a shallow clone of value.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.