import * as cheerio from 'cheerio' ; import { parseNumber } from './utils' ; export interface HLTVPage extends cheerio . Root { ( selector ) : HLTVPageElement ; } export interface HLTVPageElement { length : number ; trimText ( ) : string | undefined ; numFromAttr ( attr ) : number | undefined ; numFromText ( ) : number | undefined ; lines ( ) : string [ ] ; exists ( ) : boolean ; find ( selector ) : HLTVPageElement ; attr ( attr ) : string ; text ( ) : string ; textThen < T > ( then ) : T ; first ( ) : HLTVPageElement ; last ( ) : HLTVPageElement ; toArray ( ) : HLTVPageElement [ ] ; data ( name ) : any ; attrThen < T > ( attr , then ) : T ; next ( selector ? ) : HLTVPageElement ; eq ( index ) : HLTVPageElement ; parent ( ) : HLTVPageElement ; children ( selector ? ) : HLTVPageElement ; prev ( selector ? ) : HLTVPageElement ; contents ( ) : HLTVPageElement ; filter ( func ) : HLTVPageElement ; } const attachMethods = ( root ) : HLTVPageElement => { const obj = { length : root . length , find ( selector ) { return attachMethods ( root . find ( selector ) ) ; } , attr ( attr ) { return root . attr ( attr ) ! ; } , attrThen < T > ( attr , then ) { return then ( root . attr ( attr ) ! ) ; } , text ( ) { return root . text ( ) ; } , textThen < T > ( then ) { return then ( root . text ( ) ) ; } , first ( ) { return attachMethods ( root . first ( ) ) ; } , last ( ) { return attachMethods ( root . last ( ) ) ; } , data ( name ) { return root . data ( name ) ; } , trimText ( ) { return root . text ( ) . trim ( ) || undefined ; } , numFromAttr ( attr ) { return parseNumber ( root . attr ( attr ) ) ; } , numFromText ( ) { return parseNumber ( root . text ( ) ) ; } , lines ( ) { return root . text ( ) . split ( '\n' ) ; } , exists ( ) { return root . length !== 0 ; } , toArray ( ) { return root . toArray ( ) . map ( cheerio . default ) . map ( attachMethods ) ; } , prev ( selector ? ) { return attachMethods ( root . prev ( selector ) ) ; } , next ( selector ? ) { return attachMethods ( root . next ( selector ) ) ; } , eq ( index ) { return attachMethods ( root . eq ( index ) ) ; } , children ( selector ? ) { return attachMethods ( root . children ( selector ) ) ; } , parent ( ) { return attachMethods ( root . parent ( ) ) ; } , contents ( ) { return attachMethods ( root . contents ( ) ) ; } , filter ( func ) { return attachMethods ( root . filter ( ( i , el ) => func ( i , attachMethods ( cheerio . default ( el ) ) ) ) ) ; } } ; return obj ; } ; export const HLTVScraper = ( root ) : HLTVPage => { const selector = ( selector ) : HLTVPageElement => { return attachMethods ( root ( selector ) ) ; } ; Object . assign ( selector , root ) ; return selector as HLTVPage ; } ;