using SPTarkov.Server.Core.Models.Common; namespace SPTarkov.Server.Core.Extensions; public static class MongoIdExtensions { //Temporary, but necessary public static IEnumerable ToMongoIds(this IEnumerable source) { return source.Select(s => (MongoId)s); } /// /// Determines whether the specified is a valid 24-character hexadecimal string, /// which is the standard format for MongoDB ObjectIds. /// /// The to validate. /// if the is a valid MongoDB ObjectId; otherwise, . public static bool IsValidMongoId(this MongoId mongoId) { var span = mongoId.ToString().AsSpan(); if (span.Length != 24) { return false; } for (var i = 0; i < 24; i++) { var c = span[i]; var isHex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); if (!isHex) { return false; } } return true; } /// /// Determines whether the specified string is a valid 24-character hexadecimal representation /// of a MongoDB ObjectId. /// /// The string to validate as a MongoDB ObjectId. /// if the is a valid MongoDB ObjectId; otherwise, . public static bool IsValidMongoId(this string mongoId) { var span = mongoId.AsSpan(); if (span.Length != 24) { return false; } for (var i = 0; i < 24; i++) { var c = span[i]; var isHex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); if (!isHex) { return false; } } return true; } }