From 2b24f1943cc8d9b3e0cb1d0373479327dddbe2e2 Mon Sep 17 00:00:00 2001 From: Peter Carl Pardo Date: Sat, 30 Nov 2024 23:00:06 +0800 Subject: [PATCH] Updated domain parsing logic --- Client/src/Utils/monitorUtils.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Client/src/Utils/monitorUtils.js b/Client/src/Utils/monitorUtils.js index 393d903b7..cc92a86ae 100644 --- a/Client/src/Utils/monitorUtils.js +++ b/Client/src/Utils/monitorUtils.js @@ -1,3 +1,5 @@ +import { capitalizeFirstLetter } from "./stringUtils"; + /** * Helper function to get duration since last check or the last date checked * @param {Array} checks Array of check objects. @@ -22,21 +24,15 @@ export const parseDomainName = (url) => { url = url.replace(/^\.+|\.+$/g, ""); // Split by dots const parts = url.split("."); - // Remove common prefixes and empty parts - const cleanParts = parts.filter((part) => part !== "www" && part !== ""); - if (cleanParts.length > 2) { - // Don't know how to parse this, return URL - return url; - } - // If there's more than one part, take the second to last + // Remove common prefixes and empty parts and exclude the last element of the array (the last element should be the TLD) + const cleanParts = parts.filter((part) => part !== "www" && part !== "").slice(0, -1); + // If there's more than one part, append the two words and capitalize the first letters (e.g. ["api", "test"] -> "Api Test") const domainPart = cleanParts.length > 1 - ? cleanParts[cleanParts.length - 2] - : cleanParts[cleanParts.length - 1]; + ? cleanParts.map((part) => capitalizeFirstLetter(part)).join(" ") + : capitalizeFirstLetter(cleanParts[0]); - if (domainPart) { - return domainPart.charAt(0).toUpperCase() + domainPart.slice(1).toLowerCase(); - } + if (domainPart) return domainPart; return url; };