purescript-cheerio/src/Cheerio.js

54 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-10-28 20:49:52 +00:00
import {load} from 'cheerio'
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @typedef {import('cheerio').Element} Element */
/** @typedef {import('cheerio').Cheerio<Element>} CheerioNode */
2023-10-28 20:49:52 +00:00
/** @type {(_1: string) => () => CheerioNode} */
export const loadImpl = html => () => {
const root = load(html).root()
const htmlC = root.first().children().first()
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
if (!htmlC.is('html')) {
throw new Error('invariant condition: root node should contain HTML!')
}
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
if (htmlC.length !== 1) {
throw new Error('invariant condition: HTML element should be only child!')
}
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
return htmlC
2018-01-16 04:35:48 +00:00
}
2023-10-28 20:49:52 +00:00
/** @type {(_: CheerioNode) => () => Array<CheerioNode>} */
export const toArrayImpl = n => () => Array(n.length) .fill(undefined) .map((_, ix) => n.slice(ix, ix + 1))
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_: CheerioNode) => () => CheerioNode | null} */
export const toNullableImpl = n => () => n.length === 0 ? null : n.first()
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_: CheerioNode) => () => CheerioNode} */
export const childrenImpl = n => () => n.children()
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_: CheerioNode) => () => CheerioNode} */
export const siblingsImpl = n => () => n.siblings()
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_: CheerioNode) => () => CheerioNode} */
export const parentImpl = n => () => n.parent()
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type { (_2: CheerioNode) => () => Record<string, string>} */
export const attrsImpl = n => () => n.attr() || {}
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_1: string) => (_2: CheerioNode) => () => string | null} */
export const attrImpl = k => n => () => n.attr(k) || null
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_2: CheerioNode) => () => Record<string, string>} */
export const cssImpl = n => () => n.css() || {}
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_2: CheerioNode) => () => string} */
export const htmlImpl = n => () => n.html() || ''
2018-01-16 04:35:48 +00:00
2023-10-28 20:49:52 +00:00
/** @type {(_2: CheerioNode) => () => string} */
export const textImpl = n => () => n.text() || ''
/** @type {(_1: string) => (_2: CheerioNode) => () => CheerioNode} */
export const findImpl = s => n => () => n.find(s)