forked from pizzimathy/cli-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
166 lines (129 loc) · 4.22 KB
/
data.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Created by apizzimenti on 12/29/15.
*/
var chalk = require('chalk'),
red = chalk.red,
blue = chalk.blue,
table = require('cli-table');
function collectDates(data) {
var dates = [];
data.forEach(function (obj) {
var date = new Date(obj.time * 1000);
dates.push(date.toDateString());
});
return dates;
}
function collectHighs(data, units) {
var highs = [],
temp,
point;
data.forEach(function (obj) {
point = Math.floor(obj.temperatureMax);
if (units.tmp.includes('C')) {
temp = 'high: ' + (point > 0 ? chalk.red(point + units.tmp) : chalk.blue(point + units.tmp));
} else {
temp = 'high: ' + (point > 32 ? chalk.red(point + units.tmp) : chalk.blue(point + units.tmp));
}
highs.push(temp);
});
return highs;
}
function collectLows(data, units) {
var lows = [],
temp,
point;
data.forEach(function (obj) {
point = Math.floor(obj.temperatureMin);
if (units.tmp.includes('C')) {
temp = 'low: ' + (point > 0 ? chalk.red(point + units.tmp) : chalk.blue(point + units.tmp));
} else {
temp = 'low: ' + (point > 32 ? chalk.red(point + units.tmp) : chalk.blue(point + units.tmp));
}
lows.push(temp);
});
return lows;
}
function collectSummary(data) {
var summaries = [];
data.forEach(function (obj) {
summaries.push(obj.summary.toLowerCase());
});
return summaries;
}
function collectPrecip(data) {
var precips = [];
data.forEach(function (obj) {
precips.push(Math.ceil(obj.precipProbability * 100) + '% precip.');
});
return precips;
}
function display(now, units) {
var current = '\nCurrent Conditions:\n------------------\n',
temp = '';
if (units.c) {
temp = (now.temperature > 0 ? chalk.red(Math.ceil(now.temperature) + units.tmp) : chalk.blue(Math.floor(now.temperature) + units.tmp)) + ' -- feels like ' +
(now.apparentTemperature > 0 ? chalk.red(Math.ceil(now.apparentTemperature) + units.tmp) : chalk.blue(Math.floor(now.apparentTemperature)));
} else {
temp = (now.temperature > 32 ? chalk.red(Math.ceil(now.temperature) + units.tmp) : chalk.blue(Math.floor(now.temperature) + units.tmp)) + ' -- feels like ' +
(now.apparentTemperature > 32 ? chalk.red(Math.ceil(now.apparentTemperature) + units.tmp) : chalk.blue(Math.floor(now.apparentTemperature) + units.tmp));
}
var wind = (now.windSpeed % 1 ? Math.floor(now.windSpeed) : Math.ceil(now.windSpeed)) + units.speed + ' wind';
current += temp + ' -- ' + wind + ' -- ' + now.summary.toLowerCase();
return current + '\n';
}
function displayTable(headers, highs, lows, icons, precips) {
var Table = new table({
head: headers,
chars: {
'mid': '', 'left-mid': '', 'mid-mid': '', 'right-mid': ''
},
style: {
head:['dim']
}
});
Table.push(highs, lows, icons, precips);
return Table.toString();
}
function icon(array) {
var icons = [];
array.forEach(function (point) {
var name = point.icon;
switch (name) {
case 'clear-day':case 'clear-night':
icons.push('☀ clear');
break;
case 'rain':
icons.push('☂ rain');
break;
case 'snow':
icons.push('❄ snow');
break;
case 'sleet':
icons.push('☂❄ sleet');
break;
case 'wind':
icons.push('➳ wind');
break;
case 'fog':
icons.push('☁ fog');
break;
case 'partly-cloudy-day':case 'partly-cloudy-night':
icons.push('☁☀ partly cloudy');
break;
default:
icons.push('☁ clouds');
break;
}
});
return icons;
}
module.exports = {
collectDates: collectDates,
collectHighs: collectHighs,
collectLows: collectLows,
collectSummary: collectSummary,
collectPrecip: collectPrecip,
display: display,
displayTable: displayTable,
icon: icon
};