summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/helpers/isValidDomain.ts
diff options
context:
space:
mode:
authorMatthew Miller <matthew@millerti.me>2022-03-08 20:42:52 -0800
committerGitHub <noreply@github.com>2022-03-08 20:42:52 -0800
commit30ed8e8913ab59f97587258b4a2f5e3e8b867f5b (patch)
treeba4859ee4b70b0ca646d631d2c14d1e1981a386f /packages/browser/src/helpers/isValidDomain.ts
parent4a8fb255d4fd6fbc146dedf0a2efc938b99f5973 (diff)
parentc62c1ce7451cb3876851c802d52254dd9fe6d91c (diff)
Merge pull request #184 from MasterKale/feat/identify-errors
feat/identify-errors
Diffstat (limited to 'packages/browser/src/helpers/isValidDomain.ts')
-rw-r--r--packages/browser/src/helpers/isValidDomain.ts14
1 files changed, 14 insertions, 0 deletions
diff --git a/packages/browser/src/helpers/isValidDomain.ts b/packages/browser/src/helpers/isValidDomain.ts
new file mode 100644
index 0000000..4d2eedd
--- /dev/null
+++ b/packages/browser/src/helpers/isValidDomain.ts
@@ -0,0 +1,14 @@
+/**
+ * A simple test to determine if a hostname is a properly-formatted domain name
+ *
+ * A "valid domain" is defined here: https://url.spec.whatwg.org/#valid-domain
+ *
+ * Regex sourced from here:
+ * https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch08s15.html
+ */
+export function isValidDomain(hostname: string): boolean {
+ return (
+ // Consider localhost valid as well since it's okay wrt Secure Contexts
+ hostname === 'localhost' || /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i.test(hostname)
+ );
+}