import * as cheerio from "cheerio" ; const $ = cheerio ; interface LinksObj { classAttr : string ; div : Cheerio ; href : string ; hrefArr : string [ ] ; } export class BoxrecCommonLinks { param { Cheerio } html param { T } obj returns { T } static parseLinkInformation < T > ( html , obj ) { html . find ( "a" ) . each ( ( i , elem ) => { const { href , hrefArr } = BoxrecCommonLinks . parseLinksColumn ( elem ) ; return BoxrecCommonLinks . parseLinks < T > ( hrefArr , href , obj ) ; } ) ; return obj ; } param { CheerioElement } elem returns { LinksObj } static parseLinksColumn ( elem ) { const div = $ ( elem ) . find ( "div" ) ; const href = $ ( elem ) . attr ( "href" ) ; const classAttr = div . attr ( "class" ) ; const hrefArr = classAttr . split ( " " ) ; return { classAttr , div , href , hrefArr , } ; } param hrefArr param href param obj private static parseLinks < T > ( hrefArr , href , obj ) { hrefArr . forEach ( ( cls ) => { if ( cls !== "primaryIcon" && cls !== "clickableIcon" ) { const matches = href . match ( /([\d\/]+)$/ ) ; if ( matches && matches [ 1 ] && matches [ 1 ] !== "other" ) { let formattedCls = cls ; if ( cls . slice ( - 1 ) === "P" ) { formattedCls = cls . slice ( 0 , - 1 ) ; } formattedCls = formattedCls === "bio_open" || formattedCls === "bio_closed" ? "bio" : formattedCls ; if ( matches [ 1 ] . includes ( "/" ) ) { const formattedMatch = matches [ 1 ] . substring ( 1 ) ; const numberOfSlashes = matches [ 1 ] . match ( /(\/)/g ) ; ( obj as any ) [ formattedCls ] = numberOfSlashes && numberOfSlashes . length > 1 ? formattedMatch : parseInt ( formattedMatch , 10 ) ; } else { ( obj as any ) [ formattedCls ] = parseInt ( matches [ 1 ] , 10 ) ; } } else if ( ! href . includes ( "javascript" ) ) { ( obj as any ) . other . push ( href ) ; } } } ) ; return obj ; } }