mirror of
https://github.com/jakejarvis/rdapper.git
synced 2026-09-23 01:25:31 -04:00
Update getDomainParts to allow passing options to tldts.parse().
This commit is contained in:
+6
-1
@@ -118,5 +118,10 @@ export async function isRegistered(
|
||||
return res.record.isRegistered === true;
|
||||
}
|
||||
|
||||
export { toRegistrableDomain } from "./lib/domain";
|
||||
export {
|
||||
getDomainParts,
|
||||
getDomainTld,
|
||||
isLikelyDomain,
|
||||
toRegistrableDomain,
|
||||
} from "./lib/domain";
|
||||
export type * from "./types";
|
||||
|
||||
+29
-6
@@ -1,11 +1,28 @@
|
||||
import { parse } from "tldts";
|
||||
|
||||
type ParseOptions = Parameters<typeof parse>[1];
|
||||
|
||||
/**
|
||||
* Parse a domain into its parts.
|
||||
* Parse a domain into its parts. Accepts options which are passed to tldts.parse().
|
||||
* @see https://github.com/remusao/tldts/blob/master/packages/tldts-core/src/options.ts
|
||||
*/
|
||||
export function getDomainParts(domain: string) {
|
||||
const result = parse(domain);
|
||||
return result;
|
||||
export function getDomainParts(
|
||||
domain: string,
|
||||
opts?: ParseOptions,
|
||||
): ReturnType<typeof parse> {
|
||||
return parse(domain, { ...opts });
|
||||
}
|
||||
|
||||
/** Get the TLD (ICANN-only public suffix) of a domain. */
|
||||
export function getDomainTld(
|
||||
domain: string,
|
||||
opts?: ParseOptions,
|
||||
): string | null {
|
||||
const result = getDomainParts(domain, {
|
||||
allowPrivateDomains: false,
|
||||
...opts,
|
||||
});
|
||||
return result.publicSuffix ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,11 +49,17 @@ export function punyToUnicode(domain: string): string {
|
||||
* Normalize arbitrary input (domain or URL) to its registrable domain (eTLD+1).
|
||||
* Returns null when the input is not a valid ICANN domain (e.g., invalid TLD, IPs).
|
||||
*/
|
||||
export function toRegistrableDomain(input: string): string | null {
|
||||
export function toRegistrableDomain(
|
||||
input: string,
|
||||
opts?: ParseOptions,
|
||||
): string | null {
|
||||
const raw = (input ?? "").trim();
|
||||
if (raw === "") return null;
|
||||
|
||||
const result = parse(raw);
|
||||
const result = getDomainParts(raw, {
|
||||
allowPrivateDomains: false,
|
||||
...opts,
|
||||
});
|
||||
|
||||
// Reject IPs and non-ICANN/public suffixes.
|
||||
if (result.isIp) return null;
|
||||
|
||||
Reference in New Issue
Block a user