using System.Text.RegularExpressions; namespace Khaemhat.Data; /// /// Rules for company codes and the names of their databases (SPEC ยง5.4): the code is 1 to 20 upper-case letters, /// digits or underscores starting with a letter; the database is khaemhat_<code in lower case>. /// public static partial class CompanyCodes { /// The prefix of every company database name. public const string DatabasePrefix = "khaemhat_"; /// Whether (already normalized) is a valid company code. /// The code. /// if valid. public static bool IsValid(string? code) => code is not null && CodePattern().IsMatch(code); /// Trims and upper-cases a code, then validates it. /// The code as typed. /// The normalized code. /// The code is not valid. public static string Normalize(string code) { ArgumentNullException.ThrowIfNull(code); var normalized = code.Trim().ToUpperInvariant(); if (!IsValid(normalized)) { throw new ArgumentException( "A company code has 1 to 20 characters: letters, digits or underscore, starting with a letter.", nameof(code)); } return normalized; } /// The database name of a company, for example khaemhat_demo for DEMO. /// The company code. /// The database name. public static string DatabaseName(string code) => DatabasePrefix + Normalize(code).ToLowerInvariant(); [GeneratedRegex("^[A-Z][A-Z0-9_]{0,19}$", RegexOptions.CultureInvariant)] private static partial Regex CodePattern(); }