Switch URIPatterns to TypeValidators, Record, add typing to Object.entries with type-fest pkg

This commit is contained in:
2024-05-10 02:14:03 -05:00
parent f1fb5d422e
commit 16584baef4
4 changed files with 29 additions and 14 deletions

View File

@@ -41,6 +41,7 @@
"prettier": "^2.8.1", "prettier": "^2.8.1",
"prettier-plugin-tailwindcss": "^0.2.1", "prettier-plugin-tailwindcss": "^0.2.1",
"tailwindcss": "^3.2.0", "tailwindcss": "^3.2.0",
"type-fest": "^4.18.2",
"typescript": "^4.9.4" "typescript": "^4.9.4"
}, },
"ct3aMetadata": { "ct3aMetadata": {

View File

@@ -1,4 +1,11 @@
import type { SyntheticEvent } from "react"; import type { SyntheticEvent } from "react";
import type { Entries } from "type-fest";
declare global {
interface ObjectConstructor {
entries<T extends object>(o: T): Entries<T>
}
}
export function truthy(value: string | null | undefined) { export function truthy(value: string | null | undefined) {
if (value == undefined) return false; if (value == undefined) return false;

View File

@@ -758,18 +758,20 @@ export function createRDAPLink(url, title) {
*/ */
// TODO: Provide full domain, TLD, Ipv4 & Ipv6 validators // TODO: Provide full domain, TLD, Ipv4 & Ipv6 validators
const URIPatterns: [RegExp, TargetType][] = [ const TypeValidators: Record<TargetType, (value: string) => boolean> = {
[/^\d+$/, "autnum"], autnum: (value) => /^\d+$/.test(value),
[/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/?\d*$/, "ip4"], ip4: (value) => /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/?\d*$/.test(value),
[/^[0-9a-f:]{2,}\/?\d*$/, "ip6"], ip6: (value) => /^[0-9a-f:]{2,}\/?\d*$/.test(value),
[/^https?:/, "url"], url: (value) => /^https?:/.test(value),
[/^{/, "json"], json: (value) => /^{/.test(value),
[/^\.\w+$/, "tld"], 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])?/, /[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?/.test(
"domain", value
], ),
]; entity: (value) => false,
registrar: (value) => false,
};
/** /**
* Retrieves the precise type of a given value based on matching patterns. * Retrieves the precise type of a given value based on matching patterns.
@@ -779,8 +781,8 @@ const URIPatterns: [RegExp, TargetType][] = [
* otherwise an `Error` object. * otherwise an `Error` object.
*/ */
export function getType(value: string): Result<TargetType, Error> { export function getType(value: string): Result<TargetType, Error> {
for (const [pattern, type] of URIPatterns) { for (const [type, validator] of Object.entries(TypeValidators)) {
if (pattern.test(value)) return Result.ok(type); if (validator(value)) return Result.ok(type);
} }
return Result.err(new Error("No patterns matched the input")); return Result.err(new Error("No patterns matched the input"));
} }

View File

@@ -2373,6 +2373,11 @@ type-fest@^0.20.2:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4" resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4"
integrity sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ== 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: typed-array-length@^1.0.4:
version "1.0.4" version "1.0.4"
resolved "https://registry.yarnpkg.com/typed-array-length/-/typed-array-length-1.0.4.tgz#89d83785e5c4098bec72e08b319651f0eac9c1bb" resolved "https://registry.yarnpkg.com/typed-array-length/-/typed-array-length-1.0.4.tgz#89d83785e5c4098bec72e08b319651f0eac9c1bb"