isSubDomain static method

bool isSubDomain(
  1. String? s
)

Check if the given string s is a subdomain Example: api.domain.com => true domain.de.com => false

Implementation

static bool isSubDomain(String? s) {
  if (StringUtils.isNotNullOrEmpty(s)) {
    var labels = splitDomainName(s!);
    if (labels.length == 2) {
      // Only 2 labels, so it has to be a normal domain name
      return false;
    }
    if (labels.length == 3) {
      // 3 labels, check for www and if the second label is a subtld
      if (labels.elementAt(0) == 'www') {
        // Found www at the first label, it is a subdomain
        return true;
      }
      // If the domain name has a subtld, return false otherwise return true
      return !isSubTld(labels.elementAt(2), labels.elementAt(1));
    }
    if (labels.length > 3) {
      // More than 3 labels, so it is a sub domain name
      return true;
    }
    return false;
  } else {
    return false;
  }
}