PR feedback + remove redundant code + add a test

pull/1119/head
Jens Neuber 3 years ago
parent f455e3a454
commit 6acc9546a0

@ -6,6 +6,11 @@ const badgeConstants = {
defaultUpColor: "#66c20a", defaultUpColor: "#66c20a",
defaultDownColor: "#c2290a", defaultDownColor: "#c2290a",
defaultPingColor: "blue", // as defined by badge-maker / shields.io defaultPingColor: "blue", // as defined by badge-maker / shields.io
defaultStyle: "flat",
defaultPingValueSuffix: "ms",
defaultPingLabelSuffix: "h",
defaultUptimeValueSuffix: "%",
defaultUptimeLabelSuffix: "h",
}; };
module.exports = { module.exports = {

@ -1,5 +1,5 @@
let express = require("express"); let express = require("express");
const { allowDevAllOrigin, getSettings, setting, percentageToColor, allowAllOrigin } = require("../util-server"); const { allowDevAllOrigin, getSettings, setting, percentageToColor, allowAllOrigin, filterAndJoin } = require("../util-server");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const server = require("../server"); const server = require("../server");
const apicache = require("../modules/apicache"); const apicache = require("../modules/apicache");
@ -225,8 +225,8 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response
downLabel = "Down", downLabel = "Down",
upColor = badgeConstants.defaultUpColor, upColor = badgeConstants.defaultUpColor,
downColor = badgeConstants.defaultDownColor, downColor = badgeConstants.defaultDownColor,
style = "flat", style = badgeConstants.defaultStyle,
value // for demo purpose only value, // for demo purpose only
} = request.query; } = request.query;
try { try {
@ -275,13 +275,13 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques
const { const {
label, label,
labelPrefix, labelPrefix,
labelSuffix = "h", labelSuffix = badgeConstants.defaultUptimeLabelSuffix,
prefix, prefix,
suffix = "%", suffix = badgeConstants.defaultUptimeValueSuffix,
color, color,
labelColor, labelColor,
style = "flat", style = badgeConstants.defaultStyle,
value // for demo purpose only value, // for demo purpose only
} = request.query; } = request.query;
try { try {
@ -305,7 +305,6 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques
if (!publicMonitor) { if (!publicMonitor) {
// return a "N/A" badge in naColor (grey), if monitor is not public / not available / non exsitant // return a "N/A" badge in naColor (grey), if monitor is not public / not available / non exsitant
badgeValues.message = "N/A"; badgeValues.message = "N/A";
badgeValues.color = badgeConstants.naColor; badgeValues.color = badgeConstants.naColor;
} else { } else {
@ -314,16 +313,13 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques
requestedMonitorId requestedMonitorId
); );
// use a given, custom color or calculate one based on the uptime value
badgeValues.color = color ?? percentageToColor(uptime); badgeValues.color = color ?? percentageToColor(uptime);
// use a given, custom labelColor or use the default badge label color ( defined by badge-maker)
badgeValues.labelColor = labelColor ?? ""; badgeValues.labelColor = labelColor ?? "";
// build a lable string. If a custom label is given, override the default one ( requestedDuration )
badgeValues.label = [labelPrefix, label ?? requestedDuration, labelSuffix] badgeValues.label = filterAndJoin([labelPrefix, label ?? requestedDuration, labelSuffix]);
.filter((part) => part ?? part !== "") badgeValues.message = filterAndJoin([prefix, `${uptime * 100}`, suffix]);
.join("");
badgeValues.message = [prefix, `${uptime * 100}`, suffix]
.filter((part) => part ?? part !== "")
.join("");
} }
// build the SVG based on given values // build the SVG based on given values
@ -342,13 +338,13 @@ router.get("/api/badge/:id/ping/:duration?", cache("5 minutes"), async (request,
const { const {
label, label,
labelPrefix, labelPrefix,
labelSuffix = "h", labelSuffix = badgeConstants.defaultPingLabelSuffix,
prefix, prefix,
suffix = "ms", suffix = badgeConstants.defaultPingValueSuffix,
color = badgeConstants.defaultPingColor, color = badgeConstants.defaultPingColor,
labelColor, labelColor,
style = "flat", style = badgeConstants.defaultStyle,
value // for demo purpose only value, // for demo purpose only
} = request.query; } = request.query;
try { try {
@ -382,15 +378,11 @@ router.get("/api/badge/:id/ping/:duration?", cache("5 minutes"), async (request,
const avgPing = parseInt(overrideValue ?? publicAvgPing); const avgPing = parseInt(overrideValue ?? publicAvgPing);
badgeValues.color = color; badgeValues.color = color;
// use a given, custom labelColor or use the default badge label color ( defined by badge-maker)
badgeValues.labelColor = labelColor ?? ""; badgeValues.labelColor = labelColor ?? "";
// build a lable string. If a custom label is given, override the default one ( requestedDuration )
badgeValues.label = [labelPrefix, label ?? requestedDuration, labelSuffix] badgeValues.label = filterAndJoin([labelPrefix, label ?? requestedDuration, labelSuffix]);
.filter((part) => part ?? part !== "") badgeValues.message = filterAndJoin([prefix, avgPing, suffix]);
.join("");
badgeValues.message = [prefix, avgPing, suffix]
.filter((part) => part ?? part !== "")
.join("");
} }
// build the SVG based on given values // build the SVG based on given values

@ -391,3 +391,14 @@ exports.percentageToColor = (percentage, maxHue = 90, minHue = 10) => {
return badgeConstants.naColor; return badgeConstants.naColor;
} }
}; };
/**
* Joins and array of string to one string after filtering out empty values
*
* @param {string[]} parts
* @param {string} connector
* @returns {string}
*/
exports.filterAndJoin = (parts, connector = "") => {
return parts.filter((part) => !!part && part !== "").join(connector);
};

@ -164,3 +164,25 @@ describe("Test reset-password", () => {
}, 120000); }, 120000);
}); });
describe("The function filterAndJoin", () => {
it("should join and array of strings to one string", () => {
const result = utilServerRewire.filterAndJoin(["one", "two", "three"]);
expect(result).toBe("onetwothree");
});
it("should join strings using a given connector", () => {
const result = utilServerRewire.filterAndJoin(["one", "two", "three"], "-");
expect(result).toBe("one-two-three");
});
it("should filter null, undefined and empty strings before joining", () => {
const result = utilServerRewire.filterAndJoin([undefined, "", "three"], "--");
expect(result).toBe("three");
});
it("should return an empty string if all parts are filtered out", () => {
const result = utilServerRewire.filterAndJoin([undefined, "", ""], "--");
expect(result).toBe("");
});
});

Loading…
Cancel
Save