diff --git a/package.json b/package.json index 7183de7..6b8a369 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "prettier": "^2.8.1", "prettier-plugin-tailwindcss": "^0.2.1", "tailwindcss": "^3.2.0", + "type-fest": "^4.18.2", "typescript": "^4.9.4" }, "ct3aMetadata": { diff --git a/src/helpers.ts b/src/helpers.ts index 989223f..aa45f29 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,4 +1,11 @@ import type { SyntheticEvent } from "react"; +import type { Entries } from "type-fest"; + +declare global { + interface ObjectConstructor { + entries(o: T): Entries + } +} export function truthy(value: string | null | undefined) { if (value == undefined) return false; diff --git a/src/rdap.ts b/src/rdap.ts index b5fdc40..48167c1 100644 --- a/src/rdap.ts +++ b/src/rdap.ts @@ -758,18 +758,20 @@ export function createRDAPLink(url, title) { */ // TODO: Provide full domain, TLD, Ipv4 & Ipv6 validators -const URIPatterns: [RegExp, TargetType][] = [ - [/^\d+$/, "autnum"], - [/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/?\d*$/, "ip4"], - [/^[0-9a-f:]{2,}\/?\d*$/, "ip6"], - [/^https?:/, "url"], - [/^{/, "json"], - [/^\.\w+$/, "tld"], - [ - /[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?/, - "domain", - ], -]; +const TypeValidators: Record boolean> = { + autnum: (value) => /^\d+$/.test(value), + ip4: (value) => /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/?\d*$/.test(value), + ip6: (value) => /^[0-9a-f:]{2,}\/?\d*$/.test(value), + url: (value) => /^https?:/.test(value), + json: (value) => /^{/.test(value), + tld: (value) => /^\.\w+$/.test(value), + domain: (value) => + /[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?/.test( + value + ), + entity: (value) => false, + registrar: (value) => false, +}; /** * Retrieves the precise type of a given value based on matching patterns. @@ -779,8 +781,8 @@ const URIPatterns: [RegExp, TargetType][] = [ * otherwise an `Error` object. */ export function getType(value: string): Result { - for (const [pattern, type] of URIPatterns) { - if (pattern.test(value)) return Result.ok(type); + for (const [type, validator] of Object.entries(TypeValidators)) { + if (validator(value)) return Result.ok(type); } return Result.err(new Error("No patterns matched the input")); } diff --git a/yarn.lock b/yarn.lock index 7223ab4..e34f35d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2373,6 +2373,11 @@ type-fest@^0.20.2: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4" integrity sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ== +type-fest@^4.18.2: + version "4.18.2" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-4.18.2.tgz#8d765c42e7280a11f4d04fb77a00dacc417c8b05" + integrity sha512-+suCYpfJLAe4OXS6+PPXjW3urOS4IoP9waSiLuXfLgqZODKw/aWwASvzqE886wA0kQgGy0mIWyhd87VpqIy6Xg== + typed-array-length@^1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/typed-array-length/-/typed-array-length-1.0.4.tgz#89d83785e5c4098bec72e08b319651f0eac9c1bb"