Skip to content

Commit dcec8ea

Browse files
committed
Weather: Unify formatting and more modern style of javascript
1 parent bc375c5 commit dcec8ea

File tree

5 files changed

+124
-126
lines changed

5 files changed

+124
-126
lines changed

apps/weather/app.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ var layout = new Layout({type:"v", bgCol: g.theme.bg, c: [
1616
render: l => {
1717
if (!current || current.uv === undefined) return;
1818
const uv = Math.min(parseInt(current.uv), 11); // Cap at 11
19-
19+
2020
// UV color thresholds: [max_value, color] based on WHO standards
2121
const colors = [[2,"#0F0"], [5,"#FF0"], [7,"#F80"], [10,"#F00"], [11,"#F0F"]];
2222
const color = colors.find(c => uv <= c[0])[1];
23-
23+
2424
// Setup and measure label
2525
g.setFont("6x8").setFontAlign(-1, 0);
2626
const label = "UV: ";
2727
const labelW = g.stringWidth(label);
28-
28+
2929
// Calculate centered position (4px block + 1px spacing) * blocks - last spacing
3030
const totalW = labelW + uv * 5 - (uv > 0 ? 1 : 0);
3131
const x = l.x + (l.w - totalW) / 2;
3232
const y = l.y + l.h+6;
33-
33+
3434
// Draw label
3535
g.setColor(g.theme.fg).drawString(label, x, y);
36-
36+
3737
// Draw UV blocks
3838
g.setColor(color);
3939
for (let i = 0; i < uv; i++) {
@@ -58,7 +58,7 @@ var layout = new Layout({type:"v", bgCol: g.theme.bg, c: [
5858
{type: "txt", font: "6x8", pad: 2, halign: 1, label: /*LANG*/"Hum:"},
5959
{type: "txt", font: "9%", pad: 2, halign: 1, id: "hum", label: "000%"},
6060
]},
61-
61+
6262
{filly: 1},
6363
{type: "txt", font: "6x8", pad: 2, halign: -1, label: /*LANG*/"Wind"},
6464
{type: "h", halign: -1, c: [
@@ -79,7 +79,7 @@ var layout = new Layout({type:"v", bgCol: g.theme.bg, c: [
7979
]}, {lazy: true});
8080

8181
function formatDuration(millis) {
82-
let pluralize = (n, w) => n + " " + w + (n == 1 ? "" : "s");
82+
let pluralize = (n, w) => `${n} ${w}${n === 1 ? "" : "s"}`;
8383
if (millis < 60000) return /*LANG*/"< 1 minute";
8484
if (millis < 3600000) return pluralize(Math.floor(millis/60000), /*LANG*/"minute");
8585
if (millis < 86400000) return pluralize(Math.floor(millis/3600000), /*LANG*/"hour");
@@ -98,11 +98,11 @@ function draw() {
9898
}else{
9999
layout.feelslike.label = feelsLikeTemp[1]+feelsLikeTemp[2];
100100
}
101-
102-
layout.hum.label = current.hum+"%";
101+
102+
layout.hum.label = `${current.hum}%`;
103103
const wind = locale.speed(current.wind).match(/^(\D*\d*)(.*)$/);
104104
layout.wind.label = wind[1];
105-
layout.windUnit.label = wind[2] + " " + (current.wrose||'').toUpperCase();
105+
layout.windUnit.label = `${wind[2]} ${(current.wrose||'').toUpperCase()}`;
106106
layout.cond.label = current.txt.charAt(0).toUpperCase()+(current.txt||'').slice(1);
107107
layout.loc.label = current.loc;
108108
layout.updateTime.label = `${formatDuration(Date.now() - current.time)} ago`; // How to autotranslate this and similar?

apps/weather/clkinfo.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
(function() {
1+
(() => {
32
var weather;
43
var weatherLib = require("weather");
54

@@ -8,9 +7,9 @@
87
if(weather){
98
weather.temp = require("locale").temp(weather.temp-273.15);
109
weather.feels = require("locale").temp(weather.feels-273.15);
11-
weather.hum = weather.hum + "%";
10+
weather.hum = `${weather.hum}%`;
1211
weather.wind = require("locale").speed(weather.wind).match(/^(\D*\d*)(.*)$/);
13-
weather.wind = Math.round(weather.wind[1]) + "kph";
12+
weather.wind = `${Math.round(weather.wind[1])}kph`;
1413
} else {
1514
weather = {
1615
temp: "?",

apps/weather/lib.js

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
const storage = require('Storage');
2-
const B2 = process.env.HWVERSION===2;
1+
const storage = require("Storage");
2+
const B2 = process.env.HWVERSION === 2;
33

44
let expiryTimeout;
55
function scheduleExpiry(json) {
66
if (expiryTimeout) {
77
clearTimeout(expiryTimeout);
88
expiryTimeout = undefined;
99
}
10-
let expiry = "expiry" in json ? json.expiry : 2*3600000;
10+
let expiry = "expiry" in json ? json.expiry : 2 * 3600000;
1111
if (json.weather && json.weather.time && expiry) {
1212
let t = json.weather.time + expiry - Date.now();
1313
expiryTimeout = setTimeout(update, t);
1414
}
1515
}
1616

1717
function update(weatherEvent) {
18-
let json = storage.readJSON('weather.json')||{};
18+
let json = storage.readJSON("weather.json") || {};
1919

2020
if (weatherEvent) {
2121
let weather = weatherEvent.clone();
@@ -24,103 +24,101 @@ function update(weatherEvent) {
2424
if (weather.wdir != null) {
2525
// Convert numeric direction into human-readable label
2626
let deg = weather.wdir;
27-
while (deg<0 || deg>360) {
28-
deg = (deg+360)%360;
27+
while (deg < 0 || deg > 360) {
28+
deg = (deg + 360) % 360;
2929
}
30-
weather.wrose = ['n','ne','e','se','s','sw','w','nw','n'][Math.floor((deg+22.5)/45)];
30+
weather.wrose = ["n", "ne", "e", "se", "s", "sw", "w", "nw", "n"][Math.floor((deg + 22.5) / 45)];
3131
}
3232

3333
json.weather = weather;
34-
}
35-
else {
34+
} else {
3635
delete json.weather;
3736
}
3837

39-
40-
storage.write('weather.json', json);
38+
storage.write("weather.json", json);
4139
scheduleExpiry(json);
4240
exports.emit("update", json.weather);
4341
}
4442
exports.update = update;
4543
const _GB = global.GB;
4644
global.GB = (event) => {
47-
if (event.t==="weather") update(event);
45+
if (event.t === "weather") update(event);
4846
if (_GB) setTimeout(_GB, 0, event);
4947
};
5048

51-
exports.get = function() {
52-
return (storage.readJSON('weather.json')||{}).weather;
53-
}
49+
exports.get = () => {
50+
return (storage.readJSON("weather.json") || {}).weather;
51+
};
5452

55-
scheduleExpiry(storage.readJSON('weather.json')||{});
53+
scheduleExpiry(storage.readJSON("weather.json") || {});
5654

5755
function getPalette(monochrome, ovr) {
5856
var palette;
59-
if(monochrome) {
57+
if (monochrome) {
6058
palette = {
61-
sun: '#FFF',
62-
cloud: '#FFF',
63-
bgCloud: '#FFF',
64-
rain: '#FFF',
65-
lightning: '#FFF',
66-
snow: '#FFF',
67-
mist: '#FFF',
68-
background: '#000'
59+
sun: "#FFF",
60+
cloud: "#FFF",
61+
bgCloud: "#FFF",
62+
rain: "#FFF",
63+
lightning: "#FFF",
64+
snow: "#FFF",
65+
mist: "#FFF",
66+
background: "#000",
6967
};
7068
} else {
7169
if (B2) {
7270
if (ovr.theme.dark) {
7371
palette = {
74-
sun: '#FF0',
75-
cloud: '#FFF',
76-
bgCloud: '#777', // dithers on B2, but that's ok
77-
rain: '#0FF',
78-
lightning: '#FF0',
79-
snow: '#FFF',
80-
mist: '#FFF'
72+
sun: "#FF0",
73+
cloud: "#FFF",
74+
bgCloud: "#777", // dithers on B2, but that's ok
75+
rain: "#0FF",
76+
lightning: "#FF0",
77+
snow: "#FFF",
78+
mist: "#FFF",
8179
};
8280
} else {
8381
palette = {
84-
sun: '#FF0',
85-
cloud: '#777', // dithers on B2, but that's ok
86-
bgCloud: '#000',
87-
rain: '#00F',
88-
lightning: '#FF0',
89-
snow: '#0FF',
90-
mist: '#0FF'
82+
sun: "#FF0",
83+
cloud: "#777", // dithers on B2, but that's ok
84+
bgCloud: "#000",
85+
rain: "#00F",
86+
lightning: "#FF0",
87+
snow: "#0FF",
88+
mist: "#0FF",
9189
};
9290
}
9391
} else {
9492
if (ovr.theme.dark) {
9593
palette = {
96-
sun: '#FE0',
97-
cloud: '#BBB',
98-
bgCloud: '#777',
99-
rain: '#0CF',
100-
lightning: '#FE0',
101-
snow: '#FFF',
102-
mist: '#FFF'
94+
sun: "#FE0",
95+
cloud: "#BBB",
96+
bgCloud: "#777",
97+
rain: "#0CF",
98+
lightning: "#FE0",
99+
snow: "#FFF",
100+
mist: "#FFF",
103101
};
104102
} else {
105103
palette = {
106-
sun: '#FC0',
107-
cloud: '#000',
108-
bgCloud: '#777',
109-
rain: '#07F',
110-
lightning: '#FC0',
111-
snow: '#CCC',
112-
mist: '#CCC'
104+
sun: "#FC0",
105+
cloud: "#000",
106+
bgCloud: "#777",
107+
rain: "#07F",
108+
lightning: "#FC0",
109+
snow: "#CCC",
110+
mist: "#CCC",
113111
};
114112
}
115113
}
116114
}
117115
return palette;
118116
}
119117

120-
exports.getColor = function(code) {
118+
exports.getColor = (code) => {
121119
const codeGroup = Math.round(code / 100);
122120
const palette = getPalette(0, g);
123-
const cloud = g.blendColor(palette.cloud, palette.bgCloud, .5); //theme independent
121+
const cloud = g.blendColor(palette.cloud, palette.bgCloud, 0.5); //theme independent
124122
switch (codeGroup) {
125123
case 2: return g.blendColor(cloud, palette.lightning, .5);
126124
case 3: return palette.rain;
@@ -144,7 +142,7 @@ exports.getColor = function(code) {
144142
}
145143
default: return cloud;
146144
}
147-
}
145+
};
148146

149147
/**
150148
*
@@ -158,9 +156,9 @@ exports.getColor = function(code) {
158156
* @param ovr Graphics instance (or undefined for g)
159157
* @param monochrome If true, produce a monochromatic icon
160158
*/
161-
exports.drawIcon = function(cond, x, y, r, ovr, monochrome) {
159+
exports.drawIcon = (cond, x, y, r, ovr, monochrome) => {
162160
var palette;
163-
if(!ovr) ovr = g;
161+
if (!ovr) ovr = g;
164162

165163
palette = getPalette(monochrome, ovr);
166164

@@ -257,7 +255,7 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) {
257255

258256
function drawSnow(x, y, r) {
259257
function rotatePoints(points, pivotX, pivotY, angle) {
260-
for(let i = 0; i<points.length; i += 2) {
258+
for (let i = 0; i < points.length; i += 2) {
261259
const x = points[i];
262260
const y = points[i+1];
263261
points[i] = Math.cos(angle)*(x-pivotX)-Math.sin(angle)*(y-pivotY)+
@@ -269,7 +267,7 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) {
269267

270268
ovr.setColor(palette.snow);
271269
const w = 1/12*r;
272-
for(let i = 0; i<=6; ++i) {
270+
for (let i = 0; i <= 6; ++i) {
273271
const points = [
274272
x+w, y,
275273
x-w, y,
@@ -279,7 +277,7 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) {
279277
rotatePoints(points, x, y, i/3*Math.PI);
280278
ovr.fillPoly(points);
281279

282-
for(let j = -1; j<=1; j += 2) {
280+
for (let j = -1; j <= 1; j += 2) {
283281
const points = [
284282
x+w, y+7/12*r,
285283
x-w, y+7/12*r,
@@ -317,8 +315,8 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) {
317315
}
318316

319317
/*
320-
* Choose weather icon to display based on weather description
321-
*/
318+
* Choose weather icon to display based on weather description
319+
*/
322320
function chooseIconByTxt(txt) {
323321
if (!txt) return () => {};
324322
txt = txt.toLowerCase();
@@ -336,24 +334,26 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) {
336334
if (txt.includes("few clouds")) return drawFewClouds;
337335
if (txt.includes("scattered clouds")) return drawCloud;
338336
if (txt.includes("clouds")) return drawBrokenClouds;
339-
if (txt.includes("mist") ||
337+
if (
338+
txt.includes("mist") ||
340339
txt.includes("smoke") ||
341340
txt.includes("haze") ||
342341
txt.includes("sand") ||
343342
txt.includes("dust") ||
344343
txt.includes("fog") ||
345344
txt.includes("ash") ||
346345
txt.includes("squalls") ||
347-
txt.includes("tornado")) {
346+
txt.includes("tornado")
347+
) {
348348
return drawMist;
349349
}
350350
return drawUnknown;
351351
}
352352

353353
/*
354-
* Choose weather icon to display based on weather conditition code
355-
* https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2
356-
*/
354+
* Choose weather icon to display based on weather condition code
355+
* https://openweathermap.org/weather-conditions#Weather-Condition-Codes-2
356+
*/
357357
function chooseIconByCode(code) {
358358
const codeGroup = Math.round(code / 100);
359359
switch (codeGroup) {
@@ -382,16 +382,15 @@ exports.drawIcon = function(cond, x, y, r, ovr, monochrome) {
382382
}
383383

384384
function chooseIcon(cond) {
385-
if (typeof (cond)==="object") {
385+
if (typeof cond === "object") {
386386
if ("code" in cond) return chooseIconByCode(cond.code);
387387
if ("txt" in cond) return chooseIconByTxt(cond.txt);
388-
} else if (typeof (cond)==="number") {
388+
} else if (typeof cond === "number") {
389389
return chooseIconByCode(cond.code);
390-
} else if (typeof (cond)==="string") {
390+
} else if (typeof cond === "string") {
391391
return chooseIconByTxt(cond.txt);
392392
}
393393
return drawUnknown;
394394
}
395395
chooseIcon(cond)(x, y, r);
396-
397396
};

0 commit comments

Comments
 (0)