-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
59 lines (48 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const fetch = require('node-fetch')
const mojiTranslate = require('moji-translate')
const MAX_COUNTRY_LENGTH = 10;
const MAX_GOALS_LENGTH = 2;
function rpad( value, char, length ) {
if ( typeof value === 'undefined' ) {
return undefined;
}
return ( value + char.repeat( length ) ).substring(0, length);
}
function status (response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response)
} else {
return Promise.reject(new Error(response.statusText))
}
}
function json (response) {
return response.json()
}
function isCompletedOrInProgress(match) {
return match.status === 'completed' || match.status === 'in progress';
}
function getCountryFlag( value ) {
return mojiTranslate.translate( value.replace(/ /g, '_'), true );
}
function toConsoleOutput(match) {
let homeFlag = getCountryFlag( match.home_team.country );
if ( ! homeFlag ) {
homeFlag = ' ';
}
const home = rpad( match.home_team.country, ' ', MAX_COUNTRY_LENGTH );
const homeGoals = rpad( match.home_team.goals, ' ', MAX_GOALS_LENGTH );
const awayFlag = getCountryFlag( match.away_team.country );
const away = rpad( match.away_team.country, ' ', MAX_COUNTRY_LENGTH );
const awayGoals = rpad( match.away_team.goals, ' ', MAX_GOALS_LENGTH );
return `${homeFlag} ${home} ${homeGoals} x ${awayGoals} ${away} ${awayFlag}`;
}
fetch('http://worldcup.sfg.io/matches')
.then(status)
.then(json)
.then(matches => {
matches
.filter( isCompletedOrInProgress )
.map( toConsoleOutput )
.forEach( r => console.log( r ) );
})
.catch(error => console.log('Request failed', error))