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

View File

@ -3,8 +3,7 @@
* By Rodrigo Ramírez Norambuena https://rodrigoramirez.com
* MIT Licensed.
*/
var config = {
let config = {
port: 8080,
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
*
* By Rejas
*
* MIT Licensed.
*/
let config = {
port: 8080,
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
* MIT Licensed.
*/
var config = {
let config = {
port: 8080,
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
* MIT Licensed.
*/
var config = {
let config = {
port: 8080,
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 () {
helpers.setupTimeout(this);
var app = null;
let app = null;
beforeEach(function () {
return helpers
@ -32,7 +32,7 @@ describe("Compliments module", 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 morning check
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 () {
var hour = new Date().getHours();
const hour = new Date().getHours();
if (hour >= 12 && hour < 17) {
// if morning check
// if afternoon check
const elem = await app.client.$(".compliments");
return elem.getText(".compliments").then(function (text) {
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 () {
var hour = new Date().getHours();
const hour = new Date().getHours();
if (!(hour >= 3 && hour < 12) && !(hour >= 12 && hour < 17)) {
// if evening check
const elem = await app.client.$(".compliments");