Updated domain parsing logic
This commit is contained in:
1 parent
ea4ddfc157
commit
2b24f1943c
1 file changed
+8
-12
@@ -1,3 +1,5 @@
|
|||||||
|
import { capitalizeFirstLetter } from "./stringUtils";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to get duration since last check or the last date checked
|
* Helper function to get duration since last check or the last date checked
|
||||||
* @param {Array} checks Array of check objects.
|
* @param {Array} checks Array of check objects.
|
||||||
@@ -22,21 +24,15 @@ export const parseDomainName = (url) => {
|
|||||||
url = url.replace(/^\.+|\.+$/g, "");
|
url = url.replace(/^\.+|\.+$/g, "");
|
||||||
// Split by dots
|
// Split by dots
|
||||||
const parts = url.split(".");
|
const parts = url.split(".");
|
||||||
// Remove common prefixes and empty parts
|
// 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 !== "");
|
const cleanParts = parts.filter((part) => part !== "www" && part !== "").slice(0, -1);
|
||||||
if (cleanParts.length > 2) {
|
// If there's more than one part, append the two words and capitalize the first letters (e.g. ["api", "test"] -> "Api Test")
|
||||||
// Don't know how to parse this, return URL
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
// If there's more than one part, take the second to last
|
|
||||||
const domainPart =
|
const domainPart =
|
||||||
cleanParts.length > 1
|
cleanParts.length > 1
|
||||||
? cleanParts[cleanParts.length - 2]
|
? cleanParts.map((part) => capitalizeFirstLetter(part)).join(" ")
|
||||||
: cleanParts[cleanParts.length - 1];
|
: capitalizeFirstLetter(cleanParts[0]);
|
||||||
|
|
||||||
if (domainPart) {
|
if (domainPart) return domainPart;
|
||||||
return domainPart.charAt(0).toUpperCase() + domainPart.slice(1).toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user