documentation

This commit is contained in:
FH 2022-11-16 11:34:12 +01:00
parent 76a782168e
commit 2fe115d294
8 changed files with 417 additions and 89 deletions

View file

@ -24,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\FCS.Lib.Utility.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>

View file

@ -31,18 +31,25 @@ using System.Security.Cryptography;
namespace FCS.Lib.Utility
{
/// <summary>
/// Generators
/// </summary>
public static class Generators
{
/// <summary>
/// Generate 6 character shortUrl
/// </summary>
/// <returns><see cref="string"/> of 6 characters</returns>
public static string ShortUrlGenerator()
{
return ShortUrlGenerator(6);
}
/// <summary>
/// Randoms the string.
/// Generate shortUrl with length
/// </summary>
/// <param name="length">The length.</param>
/// <returns>System.String.</returns>
/// <returns><see cref="string"/></returns>
/// <remarks>derived from https://sourceforge.net/projects/shorturl-dotnet/</remarks>
public static string ShortUrlGenerator(int length)
{
@ -160,10 +167,11 @@ namespace FCS.Lib.Utility
}
/// <summary>
/// Generates the username.
/// Username generator
/// </summary>
/// <param name="options">The options.</param>
/// <returns>System.String.</returns>
/// <returns><see cref="string"/></returns>
/// <seealso cref="StringOptions"/>
public static string GenerateUsername(StringOptions options = null)
{
options ??= new StringOptions
@ -180,10 +188,11 @@ namespace FCS.Lib.Utility
}
/// <summary>
/// Generates the password.
/// Password generator
/// </summary>
/// <param name="options">The options.</param>
/// <returns>System.String.</returns>
/// <returns><see cref="string"/></returns>
/// <seealso cref="StringOptions"/>
public static string GeneratePassword(StringOptions options = null)
{
options ??= new StringOptions
@ -200,10 +209,10 @@ namespace FCS.Lib.Utility
}
/// <summary>
/// Generates the random text.
/// Random string generator with length
/// </summary>
/// <param name="length">The length.</param>
/// <returns>System.String.</returns>
/// <returns><see cref="string"/></returns>
public static string GenerateRandomText(int length)
{
const string consonants = "bcdfghjklmnprstvxzBDFGHJKLMNPRSTVXZ";
@ -223,10 +232,10 @@ namespace FCS.Lib.Utility
}
/// <summary>
/// Generates the random password.
/// Random string generator - string options
/// </summary>
/// <param name="options">The options.</param>
/// <returns>System.String.</returns>
/// <returns><see cref="string"/></returns>
public static string GenerateRandomString(StringOptions options = null)
{
options ??= new StringOptions
@ -281,15 +290,16 @@ namespace FCS.Lib.Utility
}
/// <summary>
/// Randoms the seed.
/// Randomize random using RNGCrypto
/// </summary>
/// <returns>Random.</returns>
/// <returns><see cref="Random"/></returns>
/// <remarks>derived from https://sourceforge.net/projects/shorturl-dotnet/</remarks>
/// <seealso cref="RNGCryptoServiceProvider"/>
public static Random RandomSeed()
{
// As the default randomizer is based on the current time
// As the default Random is based on the current time
// so it produces the same "random" number within a second
// Use a crypto randomizer to create the seed value
// Use a crypto service to create the seed value
// 4-byte array to fill with random bytes
var randomBytes = new byte[4];

View file

@ -9,17 +9,40 @@ namespace FCS.Lib.Utility
/// <seealso href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122 - A Universally Unique IDentifier (UUID) URN Namespace</seealso>
public static class GuidGenerator
{
// number of bytes in guid
/// <summary>
/// number of bytes in guid
/// </summary>
public const int ByteArraySize = 16;
// multiplex variant info
/// <summary>
/// multiplex variant info - variant byte
/// </summary>
public const int VariantByte = 8;
/// <summary>
/// multiplex variant info - variant byte mask
/// </summary>
public const int VariantByteMask = 0x3f;
/// <summary>
/// multiplex variant info - variant byte shift
/// </summary>
public const int VariantByteShift = 0x80;
// multiplex version info
/// <summary>
/// multiplex version info - version byte
/// </summary>
public const int VersionByte = 7;
/// <summary>
/// multiplex version info - version byte mask
/// </summary>
public const int VersionByteMask = 0x0f;
/// <summary>
/// multiplex version info version byte shift
/// </summary>
public const int VersionByteShift = 4;
// indexes within the uuid array for certain boundaries
@ -30,8 +53,14 @@ namespace FCS.Lib.Utility
// offset to move from 1/1/0001, which is 0-time for .NET, to gregorian 0-time of 10/15/1582
private static readonly DateTimeOffset GregorianCalendarStart = new(1582, 10, 15, 0, 0, 0, TimeSpan.Zero);
// random clock sequence and node
/// <summary>
/// Default clock sequence
/// </summary>
public static byte[] DefaultClockSequence { get; set; }
/// <summary>
/// Default node
/// </summary>
public static byte[] DefaultNode { get; set; }
static GuidGenerator()
@ -44,18 +73,33 @@ namespace FCS.Lib.Utility
random.NextBytes(DefaultNode);
}
/// <summary>
/// Set default node
/// </summary>
/// <param name="nodeName"></param>
public static void SetDefaultNode(string nodeName)
{
var x = nodeName.GetHashCode();
var node = $"{x:X}";
DefaultNode = Encoding.UTF8.GetBytes(node.ToCharArray(), 0, 6);
}
/// <summary>
/// Get version
/// </summary>
/// <param name="guid"></param>
/// <returns><see cref="GuidVersion"/></returns>
public static GuidVersion GetVersion(this Guid guid)
{
var bytes = guid.ToByteArray();
return (GuidVersion)((bytes[VersionByte] & 0xFF) >> VersionByteShift);
}
/// <summary>
/// Get date time offset from guid
/// </summary>
/// <param name="guid"></param>
/// <returns><see cref="DateTimeOffset"/></returns>
public static DateTimeOffset GetDateTimeOffset(Guid guid)
{
var bytes = guid.ToByteArray();
@ -73,26 +117,50 @@ namespace FCS.Lib.Utility
return new DateTimeOffset(ticks, TimeSpan.Zero);
}
/// <summary>
/// get date time from guid
/// </summary>
/// <param name="guid"></param>
/// <returns><see cref="DateTime"/></returns>
public static DateTime GetDateTime(Guid guid)
{
return GetDateTimeOffset(guid).DateTime;
}
/// <summary>
/// get local date time from guid
/// </summary>
/// <param name="guid"></param>
/// <returns><see cref="DateTime"/></returns>
public static DateTime GetLocalDateTime(Guid guid)
{
return GetDateTimeOffset(guid).LocalDateTime;
}
/// <summary>
/// get utc date time from guid
/// </summary>
/// <param name="guid"></param>
/// <returns><see cref="DateTime"/></returns>
public static DateTime GetUtcDateTime(Guid guid)
{
return GetDateTimeOffset(guid).UtcDateTime;
}
/// <summary>
/// Generate time based guid
/// </summary>
/// <returns><see cref="Guid"/></returns>
public static Guid GenerateTimeBasedGuid()
{
return GenerateTimeBasedGuid(DateTimeOffset.UtcNow, DefaultClockSequence, DefaultNode);
}
/// <summary>
/// Generate time based guid providing a NodeName string
/// </summary>
/// <param name="nodeName"></param>
/// <returns><see cref="Guid"/></returns>
public static Guid GenerateTimeBasedGuid(string nodeName)
{
var x = nodeName.GetHashCode();
@ -101,21 +169,47 @@ namespace FCS.Lib.Utility
return GenerateTimeBasedGuid(DateTimeOffset.UtcNow, DefaultClockSequence, defaultNode);
}
/// <summary>
/// Generate time based guid providing a valid DateTime object
/// </summary>
/// <param name="dateTime"></param>
/// <returns><see cref="Guid"/></returns>
public static Guid GenerateTimeBasedGuid(DateTime dateTime)
{
return GenerateTimeBasedGuid(dateTime, DefaultClockSequence, DefaultNode);
}
/// <summary>
/// Generate time base guid providing a valid DateTimeOffset object
/// </summary>
/// <param name="dateTime"></param>
/// <returns><see cref="Guid"/></returns>
public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime)
{
return GenerateTimeBasedGuid(dateTime, DefaultClockSequence, DefaultNode);
}
/// <summary>
/// Generate time based guid providing a date time, byte array for clock sequence and node
/// </summary>
/// <param name="dateTime"></param>
/// <param name="clockSequence"></param>
/// <param name="node"></param>
/// <returns><see cref="Guid"/></returns>
public static Guid GenerateTimeBasedGuid(DateTime dateTime, byte[] clockSequence, byte[] node)
{
return GenerateTimeBasedGuid(new DateTimeOffset(dateTime), clockSequence, node);
}
/// <summary>
/// Generate time based guid providing a valid DateTimeOffset Object and byte arrays for clock sequence and node
/// </summary>
/// <param name="dateTime"></param>
/// <param name="clockSequence"></param>
/// <param name="node"></param>
/// <returns><see cref="Guid"/></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static Guid GenerateTimeBasedGuid(DateTimeOffset dateTime, byte[] clockSequence, byte[] node)
{
if (clockSequence == null)

View file

@ -1,11 +1,25 @@
namespace FCS.Lib.Utility
{
// guid version types
/// <summary>
/// Guid Version Enum
/// </summary>
public enum GuidVersion
{
/// <summary>
/// Time
/// </summary>
TimeBased = 0x01,
/// <summary>
/// Reserved
/// </summary>
Reserved = 0x02,
/// <summary>
/// Name
/// </summary>
NameBased = 0x03,
/// <summary>
/// Random
/// </summary>
Random = 0x04
}
}

View file

@ -35,18 +35,37 @@ using System.Text.RegularExpressions;
namespace FCS.Lib.Utility
{
/// <summary>
/// Mogrify between units
/// </summary>
public static class Mogrify
{
/// <summary>
/// Get month from timestamp
/// </summary>
/// <param name="timeStamp"></param>
/// <returns>integer</returns>
public static int MonthFromTimestamp(long timeStamp)
{
return TimeStampToDateTime(timeStamp).Month;
}
/// <summary>
/// validate if timestamp occurs in month
/// </summary>
/// <param name="timestamp"></param>
/// <param name="month"></param>
/// <returns>boolean</returns>
public static bool TimestampInMonth(long timestamp, int month)
{
return TimeStampToDateTime(timestamp).Month == month;
}
/// <summary>
/// Get timestamp range for given datetime
/// </summary>
/// <param name="dateTime"></param>
/// <returns>dictionary</returns>
public static Dictionary<string,long> DateToTimestampRange(DateTime dateTime)
{
var dt1 = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day,0,0,0);
@ -57,6 +76,11 @@ namespace FCS.Lib.Utility
};
}
/// <summary>
/// ISO date to timestamp
/// </summary>
/// <param name="isoDateString"></param>
/// <returns>long</returns>
public static long IsoDateToTimestamp(string isoDateString)
{
var result = DateTime.TryParse(isoDateString, out var test);
@ -65,78 +89,116 @@ namespace FCS.Lib.Utility
return $"{test:yyyy-MM-dd}" == isoDateString ? DateTimeToTimeStamp(test) : 0;
}
/// <summary>
/// ISO date from timestamp
/// </summary>
/// <param name="timestamp"></param>
/// <returns>string yyyy-MM-dd</returns>
public static string TimestampToIsoDate(long timestamp)
{
return $"{TimeStampToDateTime(timestamp):yyyy-MM-dd}";
}
/// <summary>
/// reverse boolean
/// </summary>
/// <param name="value"></param>
/// <returns>bool</returns>
public static bool BoolReverse(bool value)
{
return !value;
}
/// <summary>
/// number from bool
/// </summary>
/// <param name="value"></param>
/// <returns>integer</returns>
public static int BoolToInt(bool value)
{
return value ? 1 : 0;
}
/// <summary>
/// string from bool
/// </summary>
/// <param name="value"></param>
/// <returns>string true/false</returns>
public static string BoolToString(bool value)
{
return value ? "true" : "false";
}
/// <summary>
/// get bool from integer
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IntToBool(int value)
{
return value is > 0 or < 0;
}
/// <summary>
/// get the number value from enum
/// </summary>
/// <param name="enumeration"></param>
/// <returns>int</returns>
public static int EnumToInt(object enumeration)
{
return Convert.ToInt32(enumeration, CultureInfo.InvariantCulture);
}
/// <summary>
/// get string from enum
/// </summary>
/// <param name="value"></param>
/// <returns>string - name of the enum</returns>
public static string EnumToString(Enum value)
{
return value == null ? string.Empty : value.ToString();
}
/// <summary>
/// get list of enum of type T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>list of enum</returns>
public static IEnumerable<T> GetEnumList<T>()
{
return (T[])Enum.GetValues(typeof(T));
}
public static bool IntToBool(int value)
{
return value > 0 || value < 0;
}
/// <summary>
/// get enum from integer of type T
/// </summary>
/// <param name="value"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T IntToEnum<T>(int value)
{
return (T) Enum.ToObject(typeof(T), value);
}
public static string IntToLetter(int value)
{
var empty = string.Empty;
var num = 97;
var str = "";
var num1 = 0;
var num2 = 97;
for (var i = 0; i <= value; i++)
{
num1++;
empty = string.Concat(str, Convert.ToString(Convert.ToChar(num), CultureInfo.InvariantCulture));
num++;
if (num1 != 26) continue;
num1 = 0;
str = Convert.ToChar(num2).ToString(CultureInfo.InvariantCulture);
num2++;
num = 97;
}
return empty;
}
/// <summary>
/// get string from list using semicolon separator
/// </summary>
/// <param name="list"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static string ListToString<T>(List<T> list)
{
return ListToString(list, ";");
}
/// <summary>
/// get string from list using delimiter
/// </summary>
/// <param name="list"></param>
/// <param name="delimiter"></param>
/// <typeparam name="T"></typeparam>
/// <returns><see cref="string"/></returns>
public static string ListToString<T>(List<T> list, string delimiter)
{
var empty = string.Empty;
@ -149,12 +211,22 @@ namespace FCS.Lib.Utility
return empty;
}
/// <summary>
/// get lowercase dash separated string from Pascal case
/// </summary>
/// <param name="value"></param>
/// <returns><see cref="string"/></returns>
public static string PascalToLower(string value)
{
var result = string.Join("-", Regex.Split(value, @"(?<!^)(?=[A-Z])").ToArray());
return result.ToLower(CultureInfo.InvariantCulture);
}
/// <summary>
/// get bool from string
/// </summary>
/// <param name="value"></param>
/// <returns><see cref="bool"/></returns>
public static bool StringToBool(string value)
{
if (string.IsNullOrEmpty(value))
@ -176,6 +248,11 @@ namespace FCS.Lib.Utility
return flag;
}
/// <summary>
/// get decimal from string
/// </summary>
/// <param name="inString"></param>
/// <returns><see cref="decimal"/></returns>
public static decimal? StringToDecimal(string inString)
{
if (string.IsNullOrEmpty(inString)) return 0;
@ -186,16 +263,36 @@ namespace FCS.Lib.Utility
: decimal.Divide(num, new decimal((long) 100));
}
/// <summary>
/// get enum of type T from string
/// </summary>
/// <param name="value"></param>
/// <typeparam name="T"></typeparam>
/// <returns><see cref="Enum"/></returns>
public static T StringToEnum<T>(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
/// <summary>
/// get list from string using semicolon
/// </summary>
/// <param name="value"></param>
/// <typeparam name="T"></typeparam>
/// <returns><see cref="List{T}"/></returns>
public static List<T> StringToList<T>(string value)
{
return StringToList<T>(value, ";");
}
/// <summary>
/// get list from string using delimiter
/// </summary>
/// <param name="value"></param>
/// <param name="delimiter"></param>
/// <typeparam name="T"></typeparam>
/// <returns><see cref="List{T}"/></returns>
/// <exception cref="ArgumentNullException"></exception>
public static List<T> StringToList<T>(string value, string delimiter)
{
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value));
@ -225,32 +322,42 @@ namespace FCS.Lib.Utility
return ts;
}
/// <summary>
/// get string from stream (default encoding)
/// </summary>
/// <param name="value"></param>
/// <returns><see cref="Stream"/></returns>
public static Stream StringToStream(string value)
{
return StringToStream(value, Encoding.Default);
}
/// <summary>
/// get stream from string (using encoding)
/// </summary>
/// <param name="value"></param>
/// <param name="encoding"></param>
/// <returns><see cref="Stream"/></returns>
public static Stream StringToStream(string value, Encoding encoding)
{
return encoding == null ? null : new MemoryStream(encoding.GetBytes(value ?? ""));
}
/// <summary>
/// get timestamp from current date time
/// </summary>
/// <returns><see cref="long"/></returns>
public static long CurrentDateTimeToTimeStamp()
{
return Convert.ToUInt32(DateTimeToTimeStamp(DateTime.Now));
}
public static string CurrentDateTimeToAlpha()
{
var dt = DateTime.UtcNow.ToString("yy MM dd HH MM ss");
var sb = new StringBuilder();
var dts = dt.Split(' ');
sb.Append((char) int.Parse(dts[0]) + 65);
sb.Append((char) int.Parse(dts[1]) + 65);
sb.Append((char) int.Parse(dts[2]) + 97);
sb.Append((char) int.Parse(dts[3]) + 65);
sb.Append((char) int.Parse(dts[4]) + 97);
sb.Append((char) int.Parse(dts[5]) + 97);
return sb.ToString();
}
/// <summary>
/// get timestamp from date time
/// </summary>
/// <param name="dateTime"></param>
/// <returns><see cref="long"/></returns>
public static long DateTimeToTimeStamp(DateTime dateTime)
{
var bigDate = new DateTime(2038, 1, 19, 0, 0, 0, 0);
@ -263,25 +370,89 @@ namespace FCS.Lib.Utility
return Convert.ToInt64((dateTime - nixDate).TotalSeconds);
}
/// <summary>
/// get date time from timestamp
/// </summary>
/// <param name="timeStamp"></param>
/// <returns><see cref="DateTime"/></returns>
public static DateTime TimeStampToDateTime(long timeStamp)
{
var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return nixDate.AddSeconds(timeStamp);
}
/// <summary>
/// get seconds from timespan
/// </summary>
/// <param name="timespan"></param>
/// <returns><see cref="long"/></returns>
public static long TimeSpanToSeconds(TimeSpan timespan)
{
return Convert.ToUInt32(timespan.Ticks / 10000000L);
}
/// <summary>
/// get timespan from seconds
/// </summary>
/// <param name="seconds"></param>
/// <returns><see cref="TimeSpan"/></returns>
public static TimeSpan SecondsToTimeSpan(long seconds)
{
return TimeSpan.FromTicks(10000000L * seconds);
}
/// <summary>
/// get minutes from timespan
/// </summary>
/// <param name="timespan"></param>
/// <returns><see cref="long"/></returns>
public static long TimespanToMinutes(TimeSpan timespan)
{
return Convert.ToUInt32(timespan.Ticks / 10000000L) / 60;
}
///// <summary>
///// get string from date time
///// </summary>
///// <returns></returns>
//public static string CurrentDateTimeToAlpha()
//{
// var dt = DateTime.UtcNow.ToString("yy MM dd HH MM ss");
// var sb = new StringBuilder();
// var dts = dt.Split(' ');
// sb.Append((char) int.Parse(dts[0]) + 65);
// sb.Append((char) int.Parse(dts[1]) + 65);
// sb.Append((char) int.Parse(dts[2]) + 97);
// sb.Append((char) int.Parse(dts[3]) + 65);
// sb.Append((char) int.Parse(dts[4]) + 97);
// sb.Append((char) int.Parse(dts[5]) + 97);
// return sb.ToString();
//}
///// <summary>
///// integer to letter
///// </summary>
///// <param name="value"></param>
///// <returns>string</returns>
//public static string IntToLetter(int value)
//{
// var empty = string.Empty;
// var num = 97;
// var str = "";
// var num1 = 0;
// var num2 = 97;
// for (var i = 0; i <= value; i++)
// {
// num1++;
// empty = string.Concat(str, Convert.ToString(Convert.ToChar(num), CultureInfo.InvariantCulture));
// num++;
// if (num1 != 26) continue;
// num1 = 0;
// str = Convert.ToChar(num2).ToString(CultureInfo.InvariantCulture);
// num2++;
// num = 97;
// }
// return empty;
//}
}
}

View file

@ -38,37 +38,60 @@ namespace FCS.Lib.Utility
{
// https://stackoverflow.com/a/45761590
public static IQueryable<TModel> OrderBy<TModel>
(this IQueryable<TModel> q, string name, bool desc)
/// <summary>
/// OrderBy
/// </summary>
/// <param name="q"></param>
/// <param name="name"></param>
/// <param name="desc"></param>
/// <typeparam name="TModel"></typeparam>
/// <returns></returns>
public static IQueryable<TModel> OrderBy<TModel> (this IQueryable<TModel> q, string name, bool desc)
{
var entityType = typeof(TModel);
var p = entityType.GetProperty(name);
var m = typeof(QueryHelper)
.GetMethod("OrderByProperty")
?.MakeGenericMethod(entityType, p.PropertyType);
return(IQueryable<TModel>) m.Invoke(null, new object[] {
q, p , desc });
return(IQueryable<TModel>) m.Invoke(null, new object[] { q, p , desc });
}
public static IQueryable<TModel> ThenBy<TModel>
(this IQueryable<TModel> q, string name, bool desc)
/// <summary>
/// ThenBy
/// </summary>
/// <param name="q"></param>
/// <param name="name"></param>
/// <param name="desc"></param>
/// <typeparam name="TModel"></typeparam>
/// <returns></returns>
public static IQueryable<TModel> ThenBy<TModel> (this IQueryable<TModel> q, string name, bool desc)
{
var entityType = typeof(TModel);
var p = entityType.GetProperty(name);
var m = typeof(QueryHelper)
.GetMethod("OrderByProperty")
?.MakeGenericMethod(entityType, p.PropertyType);
return(IQueryable<TModel>) m.Invoke(null, new object[] {
q, p , desc });
return(IQueryable<TModel>) m.Invoke(null, new object[] { q, p , desc });
}
public static IQueryable<TModel> OrderByProperty<TModel, TRet>
(IQueryable<TModel> q, PropertyInfo p, bool desc)
/// <summary>
/// OrderByProperty
/// </summary>
/// <param name="q"></param>
/// <param name="p"></param>
/// <param name="desc"></param>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TRet"></typeparam>
/// <returns></returns>
public static IQueryable<TModel> OrderByProperty<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p, bool desc)
{
var pe = Expression.Parameter(typeof(TModel));
Expression se = Expression.Convert(Expression.Property(pe, p), typeof(object));
var exp = Expression.Lambda<Func<TModel, TRet>>(se, pe);
return desc ? q.OrderByDescending(exp) : q.OrderBy(exp);
}

View file

@ -32,31 +32,31 @@ namespace FCS.Lib.Utility
public class StringOptions
{
/// <summary>
/// Gets or sets the length of the required.
/// Gets or sets the required length of a string
/// </summary>
/// <value>The length of the required.</value>
public int RequiredLength { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require non letter or digit].
/// Gets or sets a value indicating whether to [require non letter or digit].
/// </summary>
/// <value><c>true</c> if [require non letter or digit]; otherwise, <c>false</c>.</value>
public bool RequireNonLetterOrDigit { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require digit].
/// Gets or sets a value indicating whether to [require digit].
/// </summary>
/// <value><c>true</c> if [require digit]; otherwise, <c>false</c>.</value>
public bool RequireDigit { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require lowercase].
/// Gets or sets a value indicating whether to [require lowercase].
/// </summary>
/// <value><c>true</c> if [require lowercase]; otherwise, <c>false</c>.</value>
public bool RequireLowercase { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require uppercase].
/// Gets or sets a value indicating whether to [require uppercase].
/// </summary>
/// <value><c>true</c> if [require uppercase]; otherwise, <c>false</c>.</value>
public bool RequireUppercase { get; set; }
@ -68,7 +68,7 @@ namespace FCS.Lib.Utility
public int RequiredUniqueChars { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [require non alphanumeric].
/// Gets or sets a value indicating whether to [require non alphanumeric].
/// </summary>
/// <value><c>true</c> if [require non alphanumeric]; otherwise, <c>false</c>.</value>
public bool RequireNonAlphanumeric { get; set; }

View file

@ -27,11 +27,20 @@ using System.Linq;
namespace FCS.Lib.Utility
{
/// <summary>
/// Vat format validator
/// </summary>
public static class VatFormatValidator
{
// https://ec.europa.eu/taxation_customs/vies/faqvies.do#item_11
// https://ec.europa.eu/taxation_customs/vies/
/// <summary>
/// Check vat number format
/// </summary>
/// <param name="countryCode"></param>
/// <param name="vatNumber"></param>
/// <returns>bool indicating if the vat number conform to country specification</returns>
public static bool CheckVat(string countryCode, string vatNumber)
{
return countryCode.ToUpperInvariant() switch
@ -43,6 +52,24 @@ namespace FCS.Lib.Utility
};
}
/// <summary>
/// sanitize vat number
/// </summary>
/// <param name="vatNumber"></param>
/// <returns>sanitized string</returns>
public static string SanitizeVatNumber(string vatNumber)
{
vatNumber = vatNumber.ToUpperInvariant();
return vatNumber
.Replace(" ", "")
.Replace("-", "")
.Replace("_", "")
.Replace("DK", "")
.Replace("NO", "")
.Replace("SE","")
.Replace("MVA", "");
}
private static bool ValidateFormatDk(string vatNumber)
{
// https://wiki.scn.sap.com/wiki/display/CRM/Denmark
@ -119,18 +146,6 @@ namespace FCS.Lib.Utility
}
public static string SanitizeVatNumber(string vatNumber)
{
vatNumber = vatNumber.ToUpperInvariant();
return vatNumber
.Replace(" ", "")
.Replace("-", "")
.Replace("DK", "")
.Replace("NO", "")
.Replace("SE","")
.Replace("MVA", "");
}
//private static bool ValidateMod10(string number)
//{
// if (long.Parse(number) == 0)