import { BoxrecCommonTablesColumnsClass } from "../../boxrec-common-tables/boxrec-common-tables-columns.class" ; import { BoutsGetter , BoutsInterface } from "../../decorators/bouts.decorator" ; import { OutputGetter , OutputInterface } from "../../decorators/output.decorator" ; import { parseHeight , trimRemoveLineBreaks } from "../../helpers" ; import { Record , Stance } from "../boxrec.constants" ; import { WeightDivision } from "../champions/boxrec.champions.constants" ; import { BoxrecPageProfile } from "./boxrec.page.profile" ; import { BoxrecPageProfileBoxerBoutRow } from "./boxrec.page.profile.boxer.bout.row" ; import { BoxrecProfileBoxerOutput } from "./boxrec.page.profile.constants" ; import { BoxrecProfileTable } from "./boxrec.profile.constants" ; /**
 * BoxRec Boxer Profile Page
 * <pre>ex. http://boxrec.com/en/boxer/155774</pre>
 */ @ BoutsGetter ( "table" , BoxrecPageProfileBoxerBoutRow , 1 , true ) @ OutputGetter ( [ "KOs" , "alias" , "birthName" , "birthPlace" , "born" , { function : ( bouts ) => bouts . map ( bout => bout . output ) , method : "bouts" , } , "debut" , "division" , "globalId" , "hasBoutScheduled" , "height" , "name" , "nationality" , "numberOfBouts" , "otherInfo" , "picture" , "ranking" , "rating" , "reach" , "record" , "residence" , "role" , "rounds" , "stance" , "status" , "suspended" , "titlesHeld" , "vadacbp" , ] ) export class BoxrecPageProfileBoxer extends BoxrecPageProfile implements BoutsInterface , OutputInterface { returns { BoxrecPageProfileBoxerBoutRow [ ] } bouts : BoxrecPageProfileBoxerBoutRow [ ] ; output : BoxrecProfileBoxerOutput ; returns { number | null } get KOs ( ) : number | null { const ko = this . $ ( ".profileWLD tr:nth-child(2) .textWon" ) . text ( ) ; if ( ko ) { const kos = parseInt ( ko , 10 ) ; if ( ! isNaN ( kos ) ) { return kos ; } } return null ; } returns { string | null } get alias ( ) : string | null { const alias = this . parseProfileTableData ( BoxrecProfileTable . alias ) ; if ( alias ) { return BoxrecCommonTablesColumnsClass . parseAlias ( alias ) ; } return null ; } returns { string | null } get debut ( ) : string | null { const debut = this . parseProfileTableData ( BoxrecProfileTable . debut ) ; if ( debut ) { return debut ; } return null ; } returns { WeightDivision | null } get division ( ) : WeightDivision | null { const division = this . parseProfileTableData ( BoxrecProfileTable . division ) ; if ( division ) { return BoxrecCommonTablesColumnsClass . parseDivision ( division ) ; } return null ; } /**
     * Returns an array of things this fighter is enrolled under
     * todo `sport` is broken when it comes to interface (boxrec-requests), it needs to be fixed to replace string
     */ get enrollments ( ) : Array < { by : string ; expires : string ; id : number ; sport : string ; } > { const enrollmentsTableData = this . $ ( "h2:contains('Enrollments') + .boxerSectionContent table tbody tr" ) ; const enrollments = [ ] ; if ( enrollmentsTableData ) { enrollmentsTableData . each ( ( i , elem ) : void => { const children = this . $ ( elem ) . get ( 0 ) . children ; let by = children [ 0 ] . children [ 0 ] . data ; let sport = children [ 1 ] . children [ 0 ] . data ; let expires = children [ 2 ] . children [ 0 ] . data ; const id = children [ 3 ] . children [ 0 ] . children [ 0 ] . data ; if ( by && sport && expires && id ) { by = trimRemoveLineBreaks ( by ) ; sport = trimRemoveLineBreaks ( sport ) ; expires = trimRemoveLineBreaks ( expires ) ; const idConverted = parseInt ( trimRemoveLineBreaks ( id . replace ( /\D+/ , "" ) ) , 10 ) ; enrollments . push ( { by , expires , id : idConverted , sport , } ) ; } } ) ; } return enrollments ; } example returns { boolean } get hasBoutScheduled ( ) : boolean { return this . bouts . length > this . numberOfBouts ; } returns { number [ ] | null } get height ( ) : number [ ] | null { const height = this . parseProfileTableData ( BoxrecProfileTable . height ) ; return parseHeight ( height ) ; } returns { string | null } get nationality ( ) : string | null { const nationality = this . parseProfileTableData ( BoxrecProfileTable . nationality ) ; if ( nationality ) { return this . $ ( nationality ) . text ( ) . trimLeft ( ) ; } return null ; } returns { number } get numberOfBouts ( ) : number { const bouts = this . parseProfileTableData ( BoxrecProfileTable . bouts ) ; if ( bouts ) { return parseInt ( bouts , 10 ) ; } return 0 ; } returns { number [ ] [ ] | null } get ranking ( ) : number [ ] [ ] | null { const ranking = this . parseRanking ( ) ; if ( ranking ) { const html = this . $ ( `<div>${ ranking }</div>` ) ; const links = html . find ( "a" ) ; const rankings = [ ] ; links . each ( ( i , elem ) => { const text = this . $ ( elem ) . text ( ) ; const parsedArr = text . trim ( ) . replace ( "," , "" ) . split ( "/" ) . map ( ( str ) => parseInt ( str , 10 ) ) ; rankings . push ( parsedArr ) ; } ) ; return rankings ; } return null ; } returns { number | null } get rating ( ) : number | null { const rating = this . parseProfileTableData ( BoxrecProfileTable . rating ) ; const html = this . $ ( rating ) ; if ( html . get ( 0 ) ) { const widthString = html . get ( 0 ) . attribs . style ; const regex = /width\:(\d+)px\;/ ; const matches = widthString . match ( regex ) ; if ( matches && matches [ 1 ] ) { return parseInt ( matches [ 1 ] , 10 ) ; } } return null ; } returns { number [ ] | null } get reach ( ) : number [ ] | null { const reach = this . parseProfileTableData ( BoxrecProfileTable . reach ) ; if ( reach ) { const regex = /^(\d{2})\&\#x2033\;\s\&\#xA0\;\s\/\s\&\#xA0\;\s(\d{3})cm$/ ; const reachMatch = reach . match ( regex ) ; if ( reachMatch ) { const [ , inches , centimeters ] = reachMatch ; return [ parseInt ( inches , 10 ) , parseInt ( centimeters , 10 ) , ] ; } } return null ; } get record ( ) : Record { const record = { draw : null , loss : null , win : null , } ; const recordBar = this . $ ( ".profileWLD tr:nth-child(1)" ) ; const win = recordBar . find ( "td:nth-child(1)" ) . text ( ) ; const loss = recordBar . find ( "td:nth-child(2)" ) . text ( ) ; const draw = recordBar . find ( "td:nth-child(3)" ) . text ( ) ; if ( win ) { record . win = parseInt ( win , 10 ) ; } if ( loss ) { record . loss = parseInt ( loss , 10 ) ; } if ( draw ) { record . draw = parseInt ( draw , 10 ) ; } return record ; } returns { number | null } get rounds ( ) : number | null { const rounds = this . parseProfileTableData ( BoxrecProfileTable . rounds ) ; if ( rounds ) { return parseInt ( rounds , 10 ) ; } return null ; } returns { string | null } get stance ( ) : Stance | null { const stance = this . parseProfileTableData ( BoxrecProfileTable . stance ) ; return stance ? stance as Stance : null ; } returns { string | null } get suspended ( ) : string | null { const el = this . $ ( "body" ) . find ( ".profileTable div:contains('suspended'):nth-child(1)" ) ; if ( el . length ) { return trimRemoveLineBreaks ( el . text ( ) ) ; } return null ; } returns { string [ ] } get titlesHeld ( ) : string [ ] { const titlesHeld = this . parseProfileTableData ( BoxrecProfileTable . titlesHeld ) ; if ( titlesHeld ) { const html = this . $ ( titlesHeld ) ; const tmpThis = this . $ ; return html . find ( "a" ) . map ( function ( this ) : string { let text = tmpThis ( this ) . text ( ) ; text = text . replace ( /\s{2,}/g , " " ) ; return text . trim ( ) ; } ) . get ( ) ; } return [ ] ; } example returns { string | null } get vadacbp ( ) : boolean { return ! ! this . parseProfileTableData ( BoxrecProfileTable . vadacbp ) ; } private parseRanking ( ) { const rankings = this . $ ( `.profileTable table td a[href*="/en/ratings"]` ) ; return rankings ? rankings . parent ( ) . html ( ) : null ; } }