import cheerio from "cheerio" ; const IMG_REGEX = /\/\$_\d+\.(?:JPG|PNG)$/ ; /**
 * Kijiji scraping method
 */ export enum ScraperType { /**
     * Scrape using the Kijiji mobile API
     */ API = "api" , /**
     * Scrape by parsing the HTML of Kijiji.ca
     */ HTML = "html" } ; /**
 * Options to pass to the scraper
 */ export type ScraperOptions = { /**
     * Which scraping method to use. Either "api" (default) or "html"
     */ scraperType ? : ScraperType ; } ; export function sleep ( ms ) { return new Promise ( resolve => { setTimeout ( resolve , ms ) ; } ) ; } export function isNumber ( value ) { value = value . trim ( ) ; return value . length > 0 && ! Number . isNaN ( Number ( value ) ) && Number . isFinite ( Number ( value ) ) ; } ; export function getLargeImageURL ( url ) { return url . replace ( IMG_REGEX , "/$_57.JPG" ) ; } export function cleanAdDescription ( text ) { const $ = cheerio . load ( text ) ; $ ( "label" ) . remove ( ) ; return $ . root ( ) . text ( ) . trim ( ) ; } export function getScraperOptions ( options ) { const scraperOptions = { ... options } ; if ( scraperOptions . scraperType === undefined ) { scraperOptions . scraperType = ScraperType . API ; } const validScraperTypes = Object . values ( ScraperType ) ; if ( validScraperTypes . find ( k => k === scraperOptions . scraperType ) === undefined ) { throw new Error ( "Invalid value for scraper option 'scraperType'. Valid values are: " + validScraperTypes . join ( ", " ) ) ; } return scraperOptions as Required < ScraperOptions > ; }