Use es6 notation in compliments module and cleanup jsdoc

This commit is contained in:
rejas 2021-03-20 20:37:45 +01:00
parent 7f1a3df25b
commit bda8f26511
6 changed files with 38 additions and 43 deletions

View File

@ -39,37 +39,35 @@ Module.register("compliments", {
this.lastComplimentIndex = -1; this.lastComplimentIndex = -1;
var self = this;
if (this.config.remoteFile !== null) { if (this.config.remoteFile !== null) {
this.complimentFile(function (response) { this.complimentFile((response) => {
self.config.compliments = JSON.parse(response); this.config.compliments = JSON.parse(response);
self.updateDom(); this.updateDom();
}); });
} }
// Schedule update timer. // Schedule update timer.
setInterval(function () { setInterval(() => {
self.updateDom(self.config.fadeSpeed); this.updateDom(this.config.fadeSpeed);
}, this.config.updateInterval); }, this.config.updateInterval);
}, },
/* randomIndex(compliments) /**
* Generate a random index for a list of compliments. * Generate a random index for a list of compliments.
* *
* argument compliments Array<String> - Array with compliments. * @param {string[]} compliments Array with compliments.
* * @returns {number} a random index of given array
* return Number - Random index.
*/ */
randomIndex: function (compliments) { randomIndex: function (compliments) {
if (compliments.length === 1) { if (compliments.length === 1) {
return 0; return 0;
} }
var generate = function () { const generate = function () {
return Math.floor(Math.random() * compliments.length); return Math.floor(Math.random() * compliments.length);
}; };
var complimentIndex = generate(); let complimentIndex = generate();
while (complimentIndex === this.lastComplimentIndex) { while (complimentIndex === this.lastComplimentIndex) {
complimentIndex = generate(); complimentIndex = generate();
@ -80,15 +78,15 @@ Module.register("compliments", {
return complimentIndex; return complimentIndex;
}, },
/* complimentArray() /**
* Retrieve an array of compliments for the time of the day. * Retrieve an array of compliments for the time of the day.
* *
* return compliments Array<String> - Array with compliments for the time of the day. * @returns {string[]} array with compliments for the time of the day.
*/ */
complimentArray: function () { complimentArray: function () {
var hour = moment().hour(); const hour = moment().hour();
var date = this.config.mockDate ? this.config.mockDate : moment().format("YYYY-MM-DD"); const date = this.config.mockDate ? this.config.mockDate : moment().format("YYYY-MM-DD");
var compliments; let compliments;
if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) { if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) {
compliments = this.config.compliments.morning.slice(0); compliments = this.config.compliments.morning.slice(0);
@ -99,7 +97,7 @@ Module.register("compliments", {
} }
if (typeof compliments === "undefined") { if (typeof compliments === "undefined") {
compliments = new Array(); compliments = [];
} }
if (this.currentWeatherType in this.config.compliments) { if (this.currentWeatherType in this.config.compliments) {
@ -108,7 +106,7 @@ Module.register("compliments", {
compliments.push.apply(compliments, this.config.compliments.anytime); compliments.push.apply(compliments, this.config.compliments.anytime);
for (var entry in this.config.compliments) { for (let entry in this.config.compliments) {
if (new RegExp(entry).test(date)) { if (new RegExp(entry).test(date)) {
compliments.push.apply(compliments, this.config.compliments[entry]); compliments.push.apply(compliments, this.config.compliments[entry]);
} }
@ -117,11 +115,13 @@ Module.register("compliments", {
return compliments; return compliments;
}, },
/* complimentFile(callback) /**
* Retrieve a file from the local filesystem * Retrieve a file from the local filesystem
*
* @param {Function} callback Called when the file is retrieved.
*/ */
complimentFile: function (callback) { complimentFile: function (callback) {
var xobj = new XMLHttpRequest(), const xobj = new XMLHttpRequest(),
isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0, isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0,
path = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile); path = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile);
xobj.overrideMimeType("application/json"); xobj.overrideMimeType("application/json");
@ -134,16 +134,16 @@ Module.register("compliments", {
xobj.send(null); xobj.send(null);
}, },
/* complimentArray() /**
* Retrieve a random compliment. * Retrieve a random compliment.
* *
* return compliment string - A compliment. * @returns {string} a compliment
*/ */
randomCompliment: function () { randomCompliment: function () {
// get the current time of day compliments list // get the current time of day compliments list
var compliments = this.complimentArray(); const compliments = this.complimentArray();
// variable for index to next message to display // variable for index to next message to display
let index = 0; let index;
// are we randomizing // are we randomizing
if (this.config.random) { if (this.config.random) {
// yes // yes
@ -159,16 +159,16 @@ Module.register("compliments", {
// Override dom generator. // Override dom generator.
getDom: function () { getDom: function () {
var wrapper = document.createElement("div"); const wrapper = document.createElement("div");
wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright pre-line"; wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright pre-line";
// get the compliment text // get the compliment text
var complimentText = this.randomCompliment(); const complimentText = this.randomCompliment();
// split it into parts on newline text // split it into parts on newline text
var parts = complimentText.split("\n"); const parts = complimentText.split("\n");
// create a span to hold it all // create a span to hold it all
var compliment = document.createElement("span"); const compliment = document.createElement("span");
// process all the parts of the compliment text // process all the parts of the compliment text
for (var part of parts) { for (const part of parts) {
// create a text element for each part // create a text element for each part
compliment.appendChild(document.createTextNode(part)); compliment.appendChild(document.createTextNode(part));
// add a break ` // add a break `

View File

@ -3,8 +3,7 @@
* By Rodrigo Ramírez Norambuena https://rodrigoramirez.com * By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
* MIT Licensed. * MIT Licensed.
*/ */
let config = {
var config = {
port: 8080, port: 8080,
ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"],

View File

@ -1,10 +1,8 @@
/* Magic Mirror Test config compliments with date type /* Magic Mirror Test config compliments with date type
* *
* By Rejas * By Rejas
*
* MIT Licensed. * MIT Licensed.
*/ */
let config = { let config = {
port: 8080, port: 8080,
ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"],

View File

@ -3,8 +3,7 @@
* By Rodrigo Ramírez Norambuena https://rodrigoramirez.com * By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
* MIT Licensed. * MIT Licensed.
*/ */
let config = {
var config = {
port: 8080, port: 8080,
ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"],

View File

@ -3,8 +3,7 @@
* By Rodrigo Ramírez Norambuena https://rodrigoramirez.com * By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
* MIT Licensed. * MIT Licensed.
*/ */
let config = {
var config = {
port: 8080, port: 8080,
ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"],

View File

@ -9,7 +9,7 @@ const afterEach = global.afterEach;
describe("Compliments module", function () { describe("Compliments module", function () {
helpers.setupTimeout(this); helpers.setupTimeout(this);
var app = null; let app = null;
beforeEach(function () { beforeEach(function () {
return helpers return helpers
@ -32,7 +32,7 @@ describe("Compliments module", function () {
}); });
it("if Morning compliments for that part of day", async function () { it("if Morning compliments for that part of day", async function () {
var hour = new Date().getHours(); const hour = new Date().getHours();
if (hour >= 3 && hour < 12) { if (hour >= 3 && hour < 12) {
// if morning check // if morning check
const elem = await app.client.$(".compliments"); const elem = await app.client.$(".compliments");
@ -43,9 +43,9 @@ describe("Compliments module", function () {
}); });
it("if Afternoon show Compliments for that part of day", async function () { it("if Afternoon show Compliments for that part of day", async function () {
var hour = new Date().getHours(); const hour = new Date().getHours();
if (hour >= 12 && hour < 17) { if (hour >= 12 && hour < 17) {
// if morning check // if afternoon check
const elem = await app.client.$(".compliments"); const elem = await app.client.$(".compliments");
return elem.getText(".compliments").then(function (text) { return elem.getText(".compliments").then(function (text) {
expect(text).to.be.oneOf(["Hello", "Good Afternoon", "Afternoon test"]); expect(text).to.be.oneOf(["Hello", "Good Afternoon", "Afternoon test"]);
@ -54,7 +54,7 @@ describe("Compliments module", function () {
}); });
it("if Evening show Compliments for that part of day", async function () { it("if Evening show Compliments for that part of day", async function () {
var hour = new Date().getHours(); const hour = new Date().getHours();
if (!(hour >= 3 && hour < 12) && !(hour >= 12 && hour < 17)) { if (!(hour >= 3 && hour < 12) && !(hour >= 12 && hour < 17)) {
// if evening check // if evening check
const elem = await app.client.$(".compliments"); const elem = await app.client.$(".compliments");