using System; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace JobsJobsJobs.Shared { /// /// A short ID /// public record ShortId(string Id) { /// /// Validate the format of the short ID /// private static readonly Regex ValidShortId = new Regex("^[a-z0-9_-]{12}", RegexOptions.Compiled | RegexOptions.IgnoreCase); /// /// Create a new short ID /// /// A new short ID public static async Task Create() => new ShortId(await Nanoid.Nanoid.GenerateAsync(size: 12)); /// /// Try to parse a string of text into a short ID /// /// The text of the prospective short ID /// The short ID /// If the format is not valid public static ShortId Parse(string text) { if (text.Length == 12 && ValidShortId.IsMatch(text)) return new ShortId(text); throw new FormatException($"The string {text} is not a valid short ID"); } public override string ToString() => Id; } }