|
|
import type { AnyNode } from 'domhandler'; |
|
|
import type { Cheerio } from './cheerio.js'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isCheerio<T>( |
|
|
maybeCheerio: unknown, |
|
|
): maybeCheerio is Cheerio<T> { |
|
|
return (maybeCheerio as Cheerio<T>).cheerio != null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function camelCase(str: string): string { |
|
|
return str.replace(/[._-](\w|$)/g, (_, x) => (x as string).toUpperCase()); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function cssCase(str: string): string { |
|
|
return str.replace(/[A-Z]/g, '-$&').toLowerCase(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function domEach< |
|
|
T extends AnyNode, |
|
|
Arr extends ArrayLike<T> = Cheerio<T>, |
|
|
>(array: Arr, fn: (elem: T, index: number) => void): Arr { |
|
|
const len = array.length; |
|
|
for (let i = 0; i < len; i++) fn(array[i], i); |
|
|
return array; |
|
|
} |
|
|
|
|
|
const enum CharacterCode { |
|
|
LowerA = 97, |
|
|
LowerZ = 122, |
|
|
UpperA = 65, |
|
|
UpperZ = 90, |
|
|
Exclamation = 33, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isHtml(str: string): boolean { |
|
|
const tagStart = str.indexOf('<'); |
|
|
|
|
|
if (tagStart === -1 || tagStart > str.length - 3) return false; |
|
|
|
|
|
const tagChar = str.charCodeAt(tagStart + 1) as CharacterCode; |
|
|
|
|
|
return ( |
|
|
((tagChar >= CharacterCode.LowerA && tagChar <= CharacterCode.LowerZ) || |
|
|
(tagChar >= CharacterCode.UpperA && tagChar <= CharacterCode.UpperZ) || |
|
|
tagChar === CharacterCode.Exclamation) && |
|
|
str.includes('>', tagStart + 2) |
|
|
); |
|
|
} |
|
|
|