libresolver/dist/util.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-08-18 14:58:17 +00:00
export function getTld(domain) {
if (domain.includes(".")) {
domain = domain.split(".")[domain.split(".").length - 1];
}
return domain;
}
export function getSld(domain) {
if (domain.includes(".")) {
domain = domain
.split(".")
.slice(0, domain.split(".").length - 1)
.join(".");
}
return domain;
}
export function isIp(ip) {
return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip);
}
export function isDomain(domain) {
return /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/.test(domain);
}
2022-08-18 18:03:40 +00:00
export function resolverEmptyResponse() {
return {
records: [],
};
}
export function resolverError(e) {
if (!(e instanceof Error)) {
e = new Error(e);
}
return {
records: [],
error: e,
};
}
export function resolveSuccess(records) {
return {
records,
};
}
2022-08-18 18:32:18 +00:00
export function normalizeDomain(domain) {
return domain.replace(/^\.+|\.+$/g, "").replace(/^\/+|\/+$/g, "");
}
2022-08-19 00:10:31 +00:00
export function isPromise(obj) {
return (!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function");
}