Cheerio provides a set of utility functions for common operations when working with DOM elements and data structures. These functions are available through the /utils export.
import { isCheerio, camelCase, cssCase, domEach, isHtml } from "cheerio/utils";Utility function for checking if an object is a Cheerio instance.
/**
* Checks if an object is a Cheerio instance.
* @param maybeCheerio - The object to check
* @returns Whether the object is a Cheerio instance
*/
function isCheerio<T>(maybeCheerio: unknown): maybeCheerio is Cheerio<T>;Usage Example:
import { isCheerio } from "cheerio/utils";
import * as cheerio from "cheerio";
const $ = cheerio.load('<div>Hello</div>');
const div = $('div');
console.log(isCheerio(div)); // true
console.log(isCheerio('<div>Hello</div>')); // false
console.log(isCheerio({})); // falseFunctions for converting between different string case formats, commonly used in DOM attribute manipulation.
/**
* Convert a string to camel case notation.
* @param str - The string to be converted
* @returns String in camel case notation
*/
function camelCase(str: string): string;
/**
* Convert a string from camel case to "CSS case", where word boundaries are
* described by hyphens ("-") and all characters are lower-case.
* @param str - The string to be converted
* @returns String in "CSS case"
*/
function cssCase(str: string): string;Usage Examples:
import { camelCase, cssCase } from "cheerio/utils";
// Convert to camelCase
console.log(camelCase('background-color')); // "backgroundColor"
console.log(camelCase('font_size')); // "fontSize"
console.log(camelCase('data.value')); // "dataValue"
// Convert to CSS case
console.log(cssCase('backgroundColor')); // "background-color"
console.log(cssCase('fontSize')); // "font-size"
console.log(cssCase('borderTopWidth')); // "border-top-width"Efficient iteration utility for DOM elements without creating intermediary Cheerio instances.
/**
* Iterate over each DOM element without creating intermediary Cheerio instances.
* This is intended for use internally to avoid otherwise unnecessary memory
* pressure introduced by _make.
* @param array - The array to iterate over
* @param fn - Function to call for each element
* @returns The original instance
*/
function domEach<T extends AnyNode, Arr extends ArrayLike<T> = Cheerio<T>>(
array: Arr,
fn: (elem: T, index: number) => void
): Arr;Usage Example:
import { domEach } from "cheerio/utils";
import * as cheerio from "cheerio";
const $ = cheerio.load('<div>One</div><div>Two</div><div>Three</div>');
const divs = $('div');
// Efficient iteration without creating Cheerio instances
domEach(divs, (elem, index) => {
console.log(`Element ${index}: ${elem.tagName}`);
// Element 0: div
// Element 1: div
// Element 2: div
});Function for detecting whether a string contains HTML markup.
/**
* Check if string is HTML.
* Tests for a `<` within a string, immediately followed by a letter and
* eventually followed by a `>`.
* @param str - The string to check
* @returns Indicates if `str` is HTML
*/
function isHtml(str: string): boolean;Usage Examples:
import { isHtml } from "cheerio/utils";
console.log(isHtml('<div>Hello</div>')); // true
console.log(isHtml('<p>Content</p>')); // true
console.log(isHtml('<!DOCTYPE html>')); // true
console.log(isHtml('<img src="image.jpg">')); // true
console.log(isHtml('Hello World')); // false
console.log(isHtml('3 < 5')); // false
console.log(isHtml('<>')); // false
console.log(isHtml('< div>')); // false (space after <)// Core DOM Node Types (from domhandler)
interface AnyNode {
type: string;
parent?: ParentNode | null;
prev?: AnyNode | null;
next?: AnyNode | null;
}
// Cheerio collection type
abstract class Cheerio<T> implements ArrayLike<T> {
length: number;
[index: number]: T;
options: InternalOptions;
}