libresolver/dist/util.js

54 lines
1.4 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");
}
2022-08-29 04:56:17 +00:00
export function ensureUniqueRecords(records) {
return [
...new Set(records.map((item) => JSON.stringify(item))),
].map((item) => JSON.parse(item));
}