Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed Feb 17, 2019
1 parent 97b81d2 commit d4b8b00
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/endpoints/getEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getEvent = (config: HLTVConfig) => async ({
const prizePool = $('td.prizepool').text()
const location = {
name: $('img.flag').attr('title'),
code: (popSlashSource($('img.flag')) as string).split('.')[0]
code: popSlashSource($('img.flag'))!.split('.')[0]
}

const teams = toArray($('.team-box')).map(teamEl => ({
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/getMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const getMatch = (config: HLTVConfig) => async ({

const highlightedPlayer: Player | undefined = highlightedPlayerLink
? {
name: highlightedPlayerLink.split('/').pop() as string,
name: highlightedPlayerLink.split('/').pop()!,
id: Number(highlightedPlayerLink.split('/')[2])
}
: undefined
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/getMatchMapStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const getMatchMapStats = (config: HLTVConfig) => async ({
const overview = { ...teamStats, ...mostX } as MatchStatsOverview

const fullRoundHistory: WeakRoundOutcome[] = toArray(m$('.round-history-outcome')).map(
mapRoundElementToModel(team1.id as number, team2.id as number)
mapRoundElementToModel(team1.id!, team2.id!)
)
const [rh1, rh2] = [fullRoundHistory.slice(0, 30), fullRoundHistory.slice(30, 60)]

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/getMatchStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const getMatchStats = (config: HLTVConfig) => async ({
ADR: Number(rowEl.find('.st-adr').text()),
firstKillsDifference: Number(rowEl.find('.st-fkdiff').text()),
rating: Number(rowEl.find('.st-rating').text())
} as PlayerStats
}
})

const playerStats = {
Expand Down
6 changes: 2 additions & 4 deletions src/endpoints/getMatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const getMatches = (config: HLTVConfig) => async (): Promise<

const event: Event = {
name: matchEl.find('.event-logo').attr('title'),
id: Number((popSlashSource(matchEl.find('.event-logo')) as string).split('.')[0]) || undefined
id: Number(popSlashSource(matchEl.find('.event-logo'))!.split('.')[0]) || undefined
}

return { id, team1, team2, event, format, maps, stars, live: true }
Expand Down Expand Up @@ -70,9 +70,7 @@ export const getMatches = (config: HLTVConfig) => async (): Promise<
}
event = {
name: matchEl.find('.event-logo').attr('alt'),
id:
Number((popSlashSource(matchEl.find('img.event-logo')) as string).split('.')[0]) ||
undefined
id: Number(popSlashSource(matchEl.find('img.event-logo'))!.split('.')[0]) || undefined
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/getMatchesStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ export const getMatchesStats = (config: HLTVConfig) => async ({
startDate,
endDate,
matchType,
maps
maps = []
}: GetMatchesStatsParams = {}): Promise<MatchStats[]> => {
const query = `startDate=${startDate}&endDate=${endDate}&matchtype=${matchType}${[
'',
...(maps || [])
...maps
].join('&maps=')}`

let page = 0
let $: CheerioStatic
let matches = [] as MatchStats[]
let matches: MatchStats[] = []

do {
$ = await fetchPage(
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/getPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ export const getPlayer = (config: HLTVConfig) => async ({
const country = isStandardPlayer
? {
name: $('.player-realname .flag').attr('alt'),
code: (popSlashSource($('.player-realname .flag')) as string).split('.')[0]
code: popSlashSource($('.player-realname .flag'))!.split('.')[0]
}
: {
name: $('.playerRealname .flag').attr('alt'),
code: (popSlashSource($('.playerRealname .flag')) as string).split('.')[0]
code: popSlashSource($('.playerRealname .flag'))!.split('.')[0]
}

let team: Team | undefined
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/getPlayerStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getPlayerStats = (config: HLTVConfig) => async ({

const country = {
name: getInfo(2).text(),
code: (popSlashSource($('.image-and-label .flag')) as string).split('.')[0]
code: popSlashSource($('.image-and-label .flag'))!.split('.')[0]
}

let team: Team | undefined
Expand Down
5 changes: 3 additions & 2 deletions src/endpoints/getRecentThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export const getRecentThreads = (config: HLTVConfig) => async (): Promise<Thread
.last()
.text()
)
const category = (threadEl
const category = threadEl
.attr('class')
.split(' ')
.find(c => c.includes('Cat')) as string).replace('Cat', '') as ThreadCategory
.find(c => c.includes('Cat'))!
.replace('Cat', '') as ThreadCategory

return { title, link, replies, category }
})
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/getResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const getResults = (config: HLTVConfig) => async ({ pages = 1 } = {}): Pr

const event: Event = {
name: matchEl.find('.event-logo').attr('alt'),
id: Number((popSlashSource(matchEl.find('.event-logo')) as string).split('.')[0])
id: Number(popSlashSource(matchEl.find('.event-logo'))!.split('.')[0])
}

const date = Number(matchEl.parent().attr('data-zonedgrouping-entry-unix'))
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/getTeamStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const getTeamStats = (config: HLTVConfig) => async ({
const matches = toArray(m$('.stats-table tbody tr')).map(matchEl => ({
dateApproximate: getTimestamp(matchEl.find('.time a').text()),
event: {
id: Number((popSlashSource(matchEl.find('.image-and-label img')) as string).split('.')[0]),
id: Number(popSlashSource(matchEl.find('.image-and-label img'))!.split('.')[0]),
name: matchEl.find('.image-and-label img').attr('title')
},
enemyTeam: {
Expand Down
6 changes: 4 additions & 2 deletions src/playground.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import HLTV from './index'
import HLTV from './index'
// HLTV.getMatch({id: 2326616}).then(res => console.dir(res, {depth: null})).catch(err => console.log(err))
// HLTV.getMatches().then(res => console.log(res))
// HLTV.getResults({pages: 1}).then(res => console.log(res))
Expand All @@ -20,7 +20,9 @@
// HLTV.getMatch({id: 2312432}).then(res => console.dir(res, {depth: null})).catch(err => console.log(err))
// HLTV.getMatchMapStats({id: 49968}).then(res => console.dir(res, { depth: null }))
// HLTV.getMatchStats({id: 62979}).then(res => console.dir(res, { depth: null }))
// HLTV.getTeam({id: 6118}).then(res => console.dir(res, { depth: null })).catch(err => console.log(err))
HLTV.getTeam({ id: 9481 })
.then(res => console.dir(res, { depth: null }))
.catch(err => console.log(err))
// HLTV.getTeamStats({id: 6669}).then(res => console.dir(res, { depth: null })).catch(err => console.log(err))
// HLTV.getPlayer({id: 9216}).then(res => console.dir(res, { depth: null })).catch(err => console.log(err))
// HLTV.getEvent({id: 3773}).then(res => console.dir(res, { depth: null })).catch(err => console.log(err))
8 changes: 3 additions & 5 deletions src/utils/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const mapVetoElementToModel = (el: Cheerio, team1: Team, team2: Team): Ve
}

return {
team: [team1, team2].find(t => t.name === teamName.trim()) as Team,
team: [team1, team2].find(t => t.name === teamName.trim())!,
map: getMapSlug(map.trim()),
type: el.text().includes('picked') ? 'picked' : 'removed'
}
Expand Down Expand Up @@ -134,10 +134,8 @@ export const getMapsStatistics = (source: string): { [key: string]: MapStatistic
return
}

const maps = (source.match(labelRegex) as RegExpMatchArray).map(x => x.split(':"')[1])
const values = (source.match(valueRegex) as RegExpMatchArray).map(x =>
Number(x.split(':"')[1].split(splitRegex)[0])
)
const maps = source.match(labelRegex)!.map(x => x.split(':"')[1])
const values = source.match(valueRegex)!.map(x => Number(x.split(':"')[1].split(splitRegex)[0]))

const getStats = index => ({
winningPercentage: values[index],
Expand Down

0 comments on commit d4b8b00

Please sign in to comment.