summaryrefslogtreecommitdiffhomepage
path: root/packages/browser/src/helpers/isValidDomain.ts
blob: 22f045fd180fdb7f9d9b3ae9a242310737a1db50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 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)
  );
}