update headers
This commit is contained in:
parent
d8f5895dbb
commit
477a3a2e2b
13 changed files with 2151 additions and 2096 deletions
|
@ -1,25 +1,25 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 27-08-2016
|
// Created : 2022 12 17 13:33
|
||||||
//
|
//
|
||||||
// Last Modified By : Frede H.
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="ExtensionsEx.cs" company="FCS-TECH">
|
// <copyright file="ExtensionsEx.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
@ -27,23 +27,22 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class ExtensionsEx.
|
||||||
|
/// </summary>
|
||||||
|
public static class ExtensionsEx
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class ExtensionsEx.
|
/// ForEach loop
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class ExtensionsEx
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="items">The items.</param>
|
||||||
|
/// <param name="action">The action.</param>
|
||||||
|
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
|
||||||
{
|
{
|
||||||
/// <summary>
|
foreach (var item in items)
|
||||||
/// ForEach loop
|
action(item);
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T"></typeparam>
|
|
||||||
/// <param name="items">The items.</param>
|
|
||||||
/// <param name="action">The action.</param>
|
|
||||||
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
|
|
||||||
{
|
|
||||||
foreach (var item in items)
|
|
||||||
action(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
577
Generators.cs
577
Generators.cs
|
@ -1,25 +1,25 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 2020-07-01
|
// Created : 2023 02 02 06:59
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="Generators.cs" company="FCS-TECH">
|
// <copyright file="Generators.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2023-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
@ -29,295 +29,292 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generators
|
||||||
|
/// </summary>
|
||||||
|
public static class Generators
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generators
|
/// Generate 6 character shortUrl
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Generators
|
/// <returns><see cref="string"/> of 6 characters</returns>
|
||||||
|
public static string ShortUrlGenerator()
|
||||||
{
|
{
|
||||||
/// <summary>
|
return ShortUrlGenerator(6);
|
||||||
/// Generate 6 character shortUrl
|
}
|
||||||
/// </summary>
|
|
||||||
/// <returns><see cref="string"/> of 6 characters</returns>
|
/// <summary>
|
||||||
public static string ShortUrlGenerator()
|
/// Generate shortUrl with length
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="length">The length.</param>
|
||||||
|
/// <returns><see cref="string"/></returns>
|
||||||
|
/// <remarks>derived from https://sourceforge.net/projects/shorturl-dotnet/</remarks>
|
||||||
|
public static string ShortUrlGenerator(int length)
|
||||||
|
{
|
||||||
|
const string charsLower = "abcdefghijkmnopqrstuvwxyz";
|
||||||
|
const string charsUpper = "ABCDFEGHJKLMNPQRSTUVWXYZ-";
|
||||||
|
const string charsNumeric = "1234567890";
|
||||||
|
|
||||||
|
// Create a local array containing supported short-url characters
|
||||||
|
// grouped by types.
|
||||||
|
var charGroups = new[]
|
||||||
{
|
{
|
||||||
return ShortUrlGenerator(6);
|
charsLower.ToCharArray(),
|
||||||
}
|
charsUpper.ToCharArray(),
|
||||||
|
charsNumeric.ToCharArray()
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
// Use this array to track the number of unused characters in each
|
||||||
/// Generate shortUrl with length
|
// character group.
|
||||||
/// </summary>
|
var charsLeftInGroup = new int[charGroups.Length];
|
||||||
/// <param name="length">The length.</param>
|
|
||||||
/// <returns><see cref="string"/></returns>
|
// Initially, all characters in each group are not used.
|
||||||
/// <remarks>derived from https://sourceforge.net/projects/shorturl-dotnet/</remarks>
|
for (var i = 0; i < charsLeftInGroup.Length; i++)
|
||||||
public static string ShortUrlGenerator(int length)
|
charsLeftInGroup[i] = charGroups[i].Length;
|
||||||
|
|
||||||
|
// Use this array to track (iterate through) unused character groups.
|
||||||
|
var leftGroupsOrder = new int[charGroups.Length];
|
||||||
|
|
||||||
|
// Initially, all character groups are not used.
|
||||||
|
for (var i = 0; i < leftGroupsOrder.Length; i++)
|
||||||
|
leftGroupsOrder[i] = i;
|
||||||
|
|
||||||
|
// Using our private random number generator
|
||||||
|
var random = RandomSeed();
|
||||||
|
|
||||||
|
// This array will hold short-url characters.
|
||||||
|
// Allocate appropriate memory for the short-url.
|
||||||
|
var shortUrl = new char[random.Next(length, length)];
|
||||||
|
|
||||||
|
// Index of the last non-processed group.
|
||||||
|
var lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
|
||||||
|
|
||||||
|
// Generate short-url characters one at a time.
|
||||||
|
for (var i = 0; i < shortUrl.Length; i++)
|
||||||
{
|
{
|
||||||
const string charsLower = "abcdefghijkmnopqrstuvwxyz";
|
// If only one character group remained unprocessed, process it;
|
||||||
const string charsUpper = "ABCDFEGHJKLMNPQRSTUVWXYZ-";
|
// otherwise, pick a random character group from the unprocessed
|
||||||
const string charsNumeric = "1234567890";
|
// group list. To allow a special character to appear in the
|
||||||
|
// first position, increment the second parameter of the Next
|
||||||
|
// function call by one, i.e. lastLeftGroupsOrderIdx + 1.
|
||||||
|
int nextLeftGroupsOrderIdx;
|
||||||
|
if (lastLeftGroupsOrderIdx == 0)
|
||||||
|
nextLeftGroupsOrderIdx = 0;
|
||||||
|
else
|
||||||
|
nextLeftGroupsOrderIdx = random.Next(0,
|
||||||
|
lastLeftGroupsOrderIdx);
|
||||||
|
|
||||||
// Create a local array containing supported short-url characters
|
// Get the actual index of the character group, from which we will
|
||||||
// grouped by types.
|
// pick the next character.
|
||||||
var charGroups = new[]
|
var nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];
|
||||||
|
|
||||||
|
// Get the index of the last unprocessed characters in this group.
|
||||||
|
var lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;
|
||||||
|
|
||||||
|
// If only one unprocessed character is left, pick it; otherwise,
|
||||||
|
// get a random character from the unused character list.
|
||||||
|
var nextCharIdx = lastCharIdx == 0 ? 0 : random.Next(0, lastCharIdx + 1);
|
||||||
|
|
||||||
|
// Add this character to the short-url.
|
||||||
|
shortUrl[i] = charGroups[nextGroupIdx][nextCharIdx];
|
||||||
|
|
||||||
|
// If we processed the last character in this group, start over.
|
||||||
|
if (lastCharIdx == 0)
|
||||||
{
|
{
|
||||||
charsLower.ToCharArray(),
|
charsLeftInGroup[nextGroupIdx] =
|
||||||
charsUpper.ToCharArray(),
|
charGroups[nextGroupIdx].Length;
|
||||||
charsNumeric.ToCharArray()
|
}
|
||||||
};
|
// There are more unprocessed characters left.
|
||||||
|
else
|
||||||
// Use this array to track the number of unused characters in each
|
|
||||||
// character group.
|
|
||||||
var charsLeftInGroup = new int[charGroups.Length];
|
|
||||||
|
|
||||||
// Initially, all characters in each group are not used.
|
|
||||||
for (var i = 0; i < charsLeftInGroup.Length; i++)
|
|
||||||
charsLeftInGroup[i] = charGroups[i].Length;
|
|
||||||
|
|
||||||
// Use this array to track (iterate through) unused character groups.
|
|
||||||
var leftGroupsOrder = new int[charGroups.Length];
|
|
||||||
|
|
||||||
// Initially, all character groups are not used.
|
|
||||||
for (var i = 0; i < leftGroupsOrder.Length; i++)
|
|
||||||
leftGroupsOrder[i] = i;
|
|
||||||
|
|
||||||
// Using our private randomizer
|
|
||||||
var random = RandomSeed();
|
|
||||||
|
|
||||||
// This array will hold short-url characters.
|
|
||||||
// Allocate appropriate memory for the short-url.
|
|
||||||
var shortUrl = new char[random.Next(length, length)];
|
|
||||||
|
|
||||||
// Index of the last non-processed group.
|
|
||||||
var lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
|
|
||||||
|
|
||||||
// Generate short-url characters one at a time.
|
|
||||||
for (var i = 0; i < shortUrl.Length; i++)
|
|
||||||
{
|
{
|
||||||
// If only one character group remained unprocessed, process it;
|
// Swap processed character with the last unprocessed character
|
||||||
// otherwise, pick a random character group from the unprocessed
|
// so that we don't pick it until we process all characters in
|
||||||
// group list. To allow a special character to appear in the
|
// this group.
|
||||||
// first position, increment the second parameter of the Next
|
if (lastCharIdx != nextCharIdx)
|
||||||
// function call by one, i.e. lastLeftGroupsOrderIdx + 1.
|
(charGroups[nextGroupIdx][lastCharIdx], charGroups[nextGroupIdx][nextCharIdx]) = (
|
||||||
int nextLeftGroupsOrderIdx;
|
charGroups[nextGroupIdx][nextCharIdx], charGroups[nextGroupIdx][lastCharIdx]);
|
||||||
if (lastLeftGroupsOrderIdx == 0)
|
|
||||||
nextLeftGroupsOrderIdx = 0;
|
|
||||||
else
|
|
||||||
nextLeftGroupsOrderIdx = random.Next(0,
|
|
||||||
lastLeftGroupsOrderIdx);
|
|
||||||
|
|
||||||
// Get the actual index of the character group, from which we will
|
// Decrement the number of unprocessed characters in
|
||||||
// pick the next character.
|
// this group.
|
||||||
var nextGroupIdx = leftGroupsOrder[nextLeftGroupsOrderIdx];
|
charsLeftInGroup[nextGroupIdx]--;
|
||||||
|
|
||||||
// Get the index of the last unprocessed characters in this group.
|
|
||||||
var lastCharIdx = charsLeftInGroup[nextGroupIdx] - 1;
|
|
||||||
|
|
||||||
// If only one unprocessed character is left, pick it; otherwise,
|
|
||||||
// get a random character from the unused character list.
|
|
||||||
var nextCharIdx = lastCharIdx == 0 ? 0 : random.Next(0, lastCharIdx + 1);
|
|
||||||
|
|
||||||
// Add this character to the short-url.
|
|
||||||
shortUrl[i] = charGroups[nextGroupIdx][nextCharIdx];
|
|
||||||
|
|
||||||
// If we processed the last character in this group, start over.
|
|
||||||
if (lastCharIdx == 0)
|
|
||||||
{
|
|
||||||
charsLeftInGroup[nextGroupIdx] =
|
|
||||||
charGroups[nextGroupIdx].Length;
|
|
||||||
}
|
|
||||||
// There are more unprocessed characters left.
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Swap processed character with the last unprocessed character
|
|
||||||
// so that we don't pick it until we process all characters in
|
|
||||||
// this group.
|
|
||||||
if (lastCharIdx != nextCharIdx)
|
|
||||||
{
|
|
||||||
(charGroups[nextGroupIdx][lastCharIdx], charGroups[nextGroupIdx][nextCharIdx]) = (charGroups[nextGroupIdx][nextCharIdx], charGroups[nextGroupIdx][lastCharIdx]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decrement the number of unprocessed characters in
|
|
||||||
// this group.
|
|
||||||
charsLeftInGroup[nextGroupIdx]--;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we processed the last group, start all over.
|
|
||||||
if (lastLeftGroupsOrderIdx == 0)
|
|
||||||
{
|
|
||||||
lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
|
|
||||||
}
|
|
||||||
// There are more unprocessed groups left.
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Swap processed group with the last unprocessed group
|
|
||||||
// so that we don't pick it until we process all groups.
|
|
||||||
if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
|
|
||||||
{
|
|
||||||
(leftGroupsOrder[lastLeftGroupsOrderIdx], leftGroupsOrder[nextLeftGroupsOrderIdx]) = (leftGroupsOrder[nextLeftGroupsOrderIdx], leftGroupsOrder[lastLeftGroupsOrderIdx]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decrement the number of unprocessed groups.
|
|
||||||
lastLeftGroupsOrderIdx--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert password characters into a string and return the result.
|
// If we processed the last group, start all over.
|
||||||
return new string(shortUrl);
|
if (lastLeftGroupsOrderIdx == 0)
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Username generator
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options">The options.</param>
|
|
||||||
/// <returns><see cref="string"/></returns>
|
|
||||||
/// <seealso cref="StringOptions"/>
|
|
||||||
public static string GenerateUsername(StringOptions options = null)
|
|
||||||
{
|
|
||||||
options ??= new StringOptions
|
|
||||||
{
|
{
|
||||||
RequiredLength = 10,
|
lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1;
|
||||||
RequireDigit = true,
|
|
||||||
RequireLowercase = true,
|
|
||||||
RequireUppercase = true,
|
|
||||||
RequiredUniqueChars = 4,
|
|
||||||
RequireNonLetterOrDigit = false,
|
|
||||||
RequireNonAlphanumeric = false
|
|
||||||
};
|
|
||||||
return GenerateRandomString(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Password generator
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options">The options.</param>
|
|
||||||
/// <returns><see cref="string"/></returns>
|
|
||||||
/// <seealso cref="StringOptions"/>
|
|
||||||
public static string GeneratePassword(StringOptions options = null)
|
|
||||||
{
|
|
||||||
options ??= new StringOptions
|
|
||||||
{
|
|
||||||
RequiredLength = 10,
|
|
||||||
RequireDigit = true,
|
|
||||||
RequireLowercase = true,
|
|
||||||
RequireUppercase = true,
|
|
||||||
RequiredUniqueChars = 4,
|
|
||||||
RequireNonLetterOrDigit = true,
|
|
||||||
RequireNonAlphanumeric = true
|
|
||||||
};
|
|
||||||
return GenerateRandomString(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Random string generator with length
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="length">The length.</param>
|
|
||||||
/// <returns><see cref="string"/></returns>
|
|
||||||
public static string GenerateRandomText(int length)
|
|
||||||
{
|
|
||||||
const string consonants = "bcdfghjklmnprstvxzBDFGHJKLMNPRSTVXZ";
|
|
||||||
const string vowels = "aeiouyAEIOUY";
|
|
||||||
|
|
||||||
var rndString = "";
|
|
||||||
var randomNum = RandomSeed();
|
|
||||||
|
|
||||||
while (rndString.Length < length)
|
|
||||||
{
|
|
||||||
rndString += consonants[randomNum.Next(consonants.Length)];
|
|
||||||
if (rndString.Length < length)
|
|
||||||
rndString += vowels[randomNum.Next(vowels.Length)];
|
|
||||||
}
|
}
|
||||||
|
// There are more unprocessed groups left.
|
||||||
return rndString;
|
else
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Random string generator - string options
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="options">The options.</param>
|
|
||||||
/// <returns><see cref="string"/></returns>
|
|
||||||
public static string GenerateRandomString(StringOptions options = null)
|
|
||||||
{
|
|
||||||
options ??= new StringOptions
|
|
||||||
{
|
{
|
||||||
RequiredLength = 10,
|
// Swap processed group with the last unprocessed group
|
||||||
RequireDigit = true,
|
// so that we don't pick it until we process all groups.
|
||||||
RequireLowercase = true,
|
if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
|
||||||
RequireUppercase = true,
|
(leftGroupsOrder[lastLeftGroupsOrderIdx], leftGroupsOrder[nextLeftGroupsOrderIdx]) = (
|
||||||
RequiredUniqueChars = 4,
|
leftGroupsOrder[nextLeftGroupsOrderIdx], leftGroupsOrder[lastLeftGroupsOrderIdx]);
|
||||||
RequireNonLetterOrDigit = true,
|
|
||||||
RequireNonAlphanumeric = true
|
|
||||||
};
|
|
||||||
|
|
||||||
var randomChars = new[]
|
// Decrement the number of unprocessed groups.
|
||||||
{
|
lastLeftGroupsOrderIdx--;
|
||||||
"ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase
|
|
||||||
"abcdefghijkmnopqrstuvwxyz", // lowercase
|
|
||||||
"0123456789", // digits
|
|
||||||
"!@$?_-" // non-alphanumeric
|
|
||||||
};
|
|
||||||
|
|
||||||
// Using our private randomizer
|
|
||||||
var rand = RandomSeed();
|
|
||||||
|
|
||||||
var chars = new List<char>();
|
|
||||||
|
|
||||||
if (options.RequireUppercase)
|
|
||||||
chars.Insert(rand.Next(0, chars.Count),
|
|
||||||
randomChars[0][rand.Next(0, randomChars[0].Length)]);
|
|
||||||
|
|
||||||
if (options.RequireLowercase)
|
|
||||||
chars.Insert(rand.Next(0, chars.Count),
|
|
||||||
randomChars[1][rand.Next(0, randomChars[1].Length)]);
|
|
||||||
|
|
||||||
if (options.RequireDigit)
|
|
||||||
chars.Insert(rand.Next(0, chars.Count),
|
|
||||||
randomChars[2][rand.Next(0, randomChars[2].Length)]);
|
|
||||||
|
|
||||||
if (options.RequireNonAlphanumeric)
|
|
||||||
chars.Insert(rand.Next(0, chars.Count),
|
|
||||||
randomChars[3][rand.Next(0, randomChars[3].Length)]);
|
|
||||||
|
|
||||||
var rcs = randomChars[rand.Next(0, randomChars.Length)];
|
|
||||||
for (var i = chars.Count;
|
|
||||||
i < options.RequiredLength
|
|
||||||
|| chars.Distinct().Count() < options.RequiredUniqueChars;
|
|
||||||
i++)
|
|
||||||
chars.Insert(rand.Next(0, chars.Count),
|
|
||||||
rcs[rand.Next(0, rcs.Length)]);
|
|
||||||
|
|
||||||
return new string(chars.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Randomize random using RNGCrypto
|
|
||||||
/// </summary>
|
|
||||||
/// <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 Random is based on the current time
|
|
||||||
// so it produces the same "random" number within a second
|
|
||||||
// Use a crypto service to create the seed value
|
|
||||||
|
|
||||||
// 4-byte array to fill with random bytes
|
|
||||||
var randomBytes = new byte[4];
|
|
||||||
|
|
||||||
// Generate 4 random bytes.
|
|
||||||
using (var rng = new RNGCryptoServiceProvider())
|
|
||||||
{
|
|
||||||
rng.GetBytes(randomBytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert 4 bytes into a 32-bit integer value.
|
|
||||||
var seed = ((randomBytes[0] & 0x7f) << 24) |
|
|
||||||
(randomBytes[1] << 16) |
|
|
||||||
(randomBytes[2] << 8) |
|
|
||||||
randomBytes[3];
|
|
||||||
|
|
||||||
// Return a truly randomized random generator
|
|
||||||
return new Random(seed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert password characters into a string and return the result.
|
||||||
|
return new string(shortUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Username generator
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">The options.</param>
|
||||||
|
/// <returns><see cref="string"/></returns>
|
||||||
|
/// <seealso cref="StringOptions"/>
|
||||||
|
public static string GenerateUsername(StringOptions options = null)
|
||||||
|
{
|
||||||
|
options ??= new StringOptions
|
||||||
|
{
|
||||||
|
RequiredLength = 16,
|
||||||
|
RequireDigit = true,
|
||||||
|
RequireLowercase = true,
|
||||||
|
RequireUppercase = true,
|
||||||
|
RequiredUniqueChars = 4,
|
||||||
|
RequireNonLetterOrDigit = false,
|
||||||
|
RequireNonAlphanumeric = false
|
||||||
|
};
|
||||||
|
return GenerateRandomString(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Password generator
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">The options.</param>
|
||||||
|
/// <returns><see cref="string"/></returns>
|
||||||
|
/// <seealso cref="StringOptions"/>
|
||||||
|
public static string GeneratePassword(StringOptions options = null)
|
||||||
|
{
|
||||||
|
options ??= new StringOptions
|
||||||
|
{
|
||||||
|
RequiredLength = 16,
|
||||||
|
RequireDigit = true,
|
||||||
|
RequireLowercase = true,
|
||||||
|
RequireUppercase = true,
|
||||||
|
RequiredUniqueChars = 8,
|
||||||
|
RequireNonLetterOrDigit = false,
|
||||||
|
RequireNonAlphanumeric = false
|
||||||
|
};
|
||||||
|
return GenerateRandomString(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Random string generator with length
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="length">The length.</param>
|
||||||
|
/// <returns><see cref="string"/></returns>
|
||||||
|
public static string GenerateRandomText(int length)
|
||||||
|
{
|
||||||
|
const string consonants = "bcdfghjklmnprstvxzBDFGHJKLMNPRSTVXZ";
|
||||||
|
const string vowels = "aeiouyAEIOUY";
|
||||||
|
|
||||||
|
var rndString = "";
|
||||||
|
var randomNum = RandomSeed();
|
||||||
|
|
||||||
|
while (rndString.Length < length)
|
||||||
|
{
|
||||||
|
rndString += consonants[randomNum.Next(consonants.Length)];
|
||||||
|
if (rndString.Length < length)
|
||||||
|
rndString += vowels[randomNum.Next(vowels.Length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return rndString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Random string generator - string options
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">The options.</param>
|
||||||
|
/// <returns><see cref="string"/></returns>
|
||||||
|
public static string GenerateRandomString(StringOptions options = null)
|
||||||
|
{
|
||||||
|
options ??= new StringOptions
|
||||||
|
{
|
||||||
|
RequiredLength = 16,
|
||||||
|
RequireDigit = true,
|
||||||
|
RequireLowercase = true,
|
||||||
|
RequireUppercase = true,
|
||||||
|
RequiredUniqueChars = 4,
|
||||||
|
RequireNonLetterOrDigit = true,
|
||||||
|
RequireNonAlphanumeric = true
|
||||||
|
};
|
||||||
|
|
||||||
|
var randomChars = new[]
|
||||||
|
{
|
||||||
|
"ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase
|
||||||
|
"abcdefghijkmnopqrstuvwxyz", // lowercase
|
||||||
|
"0123456789", // digits
|
||||||
|
"!@$?_-" // non-alphanumeric
|
||||||
|
};
|
||||||
|
|
||||||
|
// Using our private random number generator
|
||||||
|
var rand = RandomSeed();
|
||||||
|
|
||||||
|
var chars = new List<char>();
|
||||||
|
|
||||||
|
if (options.RequireUppercase)
|
||||||
|
chars.Insert(rand.Next(0, chars.Count),
|
||||||
|
randomChars[0][rand.Next(0, randomChars[0].Length)]);
|
||||||
|
|
||||||
|
if (options.RequireLowercase)
|
||||||
|
chars.Insert(rand.Next(0, chars.Count),
|
||||||
|
randomChars[1][rand.Next(0, randomChars[1].Length)]);
|
||||||
|
|
||||||
|
if (options.RequireDigit)
|
||||||
|
chars.Insert(rand.Next(0, chars.Count),
|
||||||
|
randomChars[2][rand.Next(0, randomChars[2].Length)]);
|
||||||
|
|
||||||
|
if (options.RequireNonAlphanumeric)
|
||||||
|
chars.Insert(rand.Next(0, chars.Count),
|
||||||
|
randomChars[3][rand.Next(0, randomChars[3].Length)]);
|
||||||
|
|
||||||
|
var rcs = randomChars[rand.Next(0, randomChars.Length)];
|
||||||
|
for (var i = chars.Count;
|
||||||
|
i < options.RequiredLength
|
||||||
|
|| chars.Distinct().Count() < options.RequiredUniqueChars;
|
||||||
|
i++)
|
||||||
|
chars.Insert(rand.Next(0, chars.Count),
|
||||||
|
rcs[rand.Next(0, rcs.Length)]);
|
||||||
|
|
||||||
|
return new string(chars.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Randomize random using RNGCrypto
|
||||||
|
/// </summary>
|
||||||
|
/// <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 Random is based on the current time
|
||||||
|
// so it produces the same "random" number within a second
|
||||||
|
// Use a crypto service to create the seed value
|
||||||
|
|
||||||
|
// 4-byte array to fill with random bytes
|
||||||
|
var randomBytes = new byte[4];
|
||||||
|
|
||||||
|
// Generate 4 random bytes.
|
||||||
|
using (var rng = new RNGCryptoServiceProvider())
|
||||||
|
{
|
||||||
|
rng.GetBytes(randomBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert 4 bytes into a 32-bit integer value.
|
||||||
|
var seed = ((randomBytes[0] & 0x7f) << 24) |
|
||||||
|
(randomBytes[1] << 16) |
|
||||||
|
(randomBytes[2] << 8) |
|
||||||
|
randomBytes[3];
|
||||||
|
|
||||||
|
// Return a truly randomized random generator
|
||||||
|
return new Random(seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
511
GuidGenerator.cs
511
GuidGenerator.cs
|
@ -1,251 +1,276 @@
|
||||||
using System;
|
// ***********************************************************************
|
||||||
|
// Assembly : FCS.Lib.Utility
|
||||||
|
// Author : fhdk
|
||||||
|
// Created : 2022 12 17 13:33
|
||||||
|
//
|
||||||
|
// Last Modified By: fhdk
|
||||||
|
// Last Modified On : 2023 03 14 09:16
|
||||||
|
// ***********************************************************************
|
||||||
|
// <copyright file="GuidGenerator.cs" company="FCS">
|
||||||
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as
|
||||||
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
|
// License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
|
// </copyright>
|
||||||
|
// <summary></summary>
|
||||||
|
// ***********************************************************************
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used for generating UUID based on RFC 4122.
|
||||||
|
/// </summary>
|
||||||
|
/// <seealso href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122 - A Universally Unique IDentifier (UUID) URN Namespace</seealso>
|
||||||
|
public static class GuidGenerator
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Used for generating UUID based on RFC 4122.
|
/// number of bytes in guid
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <seealso href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122 - A Universally Unique IDentifier (UUID) URN Namespace</seealso>
|
public const int ByteArraySize = 16;
|
||||||
public static class GuidGenerator
|
|
||||||
|
/// <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;
|
||||||
|
|
||||||
|
/// <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
|
||||||
|
private const byte TimestampByte = 0;
|
||||||
|
private const byte GuidClockSequenceByte = 8;
|
||||||
|
private const byte NodeByte = 10;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
static GuidGenerator()
|
||||||
{
|
{
|
||||||
|
DefaultClockSequence = new byte[2];
|
||||||
/// <summary>
|
DefaultNode = new byte[6];
|
||||||
/// number of bytes in guid
|
|
||||||
/// </summary>
|
|
||||||
public const int ByteArraySize = 16;
|
|
||||||
|
|
||||||
/// <summary>
|
var random = new Random();
|
||||||
/// multiplex variant info - variant byte
|
random.NextBytes(DefaultClockSequence);
|
||||||
/// </summary>
|
random.NextBytes(DefaultNode);
|
||||||
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;
|
|
||||||
|
|
||||||
/// <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
|
|
||||||
private const byte TimestampByte = 0;
|
|
||||||
private const byte GuidClockSequenceByte = 8;
|
|
||||||
private const byte NodeByte = 10;
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Default clock sequence
|
|
||||||
/// </summary>
|
|
||||||
public static byte[] DefaultClockSequence { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Default node
|
|
||||||
/// </summary>
|
|
||||||
public static byte[] DefaultNode { get; set; }
|
|
||||||
|
|
||||||
static GuidGenerator()
|
|
||||||
{
|
|
||||||
DefaultClockSequence = new byte[2];
|
|
||||||
DefaultNode = new byte[6];
|
|
||||||
|
|
||||||
var random = new Random();
|
|
||||||
random.NextBytes(DefaultClockSequence);
|
|
||||||
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();
|
|
||||||
|
|
||||||
// reverse the version
|
|
||||||
bytes[VersionByte] &= VersionByteMask;
|
|
||||||
bytes[VersionByte] |= (byte)GuidVersion.TimeBased >> VersionByteShift;
|
|
||||||
|
|
||||||
var timestampBytes = new byte[8];
|
|
||||||
Array.Copy(bytes, TimestampByte, timestampBytes, 0, 8);
|
|
||||||
|
|
||||||
var timestamp = BitConverter.ToInt64(timestampBytes, 0);
|
|
||||||
var ticks = timestamp + GregorianCalendarStart.Ticks;
|
|
||||||
|
|
||||||
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();
|
|
||||||
var node = $"{x:X}";
|
|
||||||
var defaultNode = Encoding.UTF8.GetBytes(node.ToCharArray(), 0, 6);
|
|
||||||
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)
|
|
||||||
throw new ArgumentNullException(nameof(clockSequence));
|
|
||||||
|
|
||||||
if (node == null)
|
|
||||||
throw new ArgumentNullException(nameof(node));
|
|
||||||
|
|
||||||
if (clockSequence.Length != 2)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(clockSequence), "The clockSequence must be 2 bytes.");
|
|
||||||
|
|
||||||
if (node.Length != 6)
|
|
||||||
throw new ArgumentOutOfRangeException(nameof(node), "The node must be 6 bytes.");
|
|
||||||
|
|
||||||
var ticks = (dateTime - GregorianCalendarStart).Ticks;
|
|
||||||
var guid = new byte[ByteArraySize];
|
|
||||||
var timestamp = BitConverter.GetBytes(ticks);
|
|
||||||
|
|
||||||
// copy node
|
|
||||||
Array.Copy(node, 0, guid, NodeByte, Math.Min(6, node.Length));
|
|
||||||
|
|
||||||
// copy clock sequence
|
|
||||||
Array.Copy(clockSequence, 0, guid, GuidClockSequenceByte, Math.Min(2, clockSequence.Length));
|
|
||||||
|
|
||||||
// copy timestamp
|
|
||||||
Array.Copy(timestamp, 0, guid, TimestampByte, Math.Min(8, timestamp.Length));
|
|
||||||
|
|
||||||
// set the variant
|
|
||||||
guid[VariantByte] &= VariantByteMask;
|
|
||||||
guid[VariantByte] |= VariantByteShift;
|
|
||||||
|
|
||||||
// set the version
|
|
||||||
guid[VersionByte] &= VersionByteMask;
|
|
||||||
guid[VersionByte] |= (byte)GuidVersion.TimeBased << VersionByteShift;
|
|
||||||
|
|
||||||
return new Guid(guid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default clock sequence
|
||||||
|
/// </summary>
|
||||||
|
public static byte[] DefaultClockSequence { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Default node
|
||||||
|
/// </summary>
|
||||||
|
public static byte[] DefaultNode { get; set; }
|
||||||
|
|
||||||
|
/// <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();
|
||||||
|
|
||||||
|
// reverse the version
|
||||||
|
bytes[VersionByte] &= VersionByteMask;
|
||||||
|
bytes[VersionByte] |= (byte)GuidVersion.TimeBased >> VersionByteShift;
|
||||||
|
|
||||||
|
var timestampBytes = new byte[8];
|
||||||
|
Array.Copy(bytes, TimestampByte, timestampBytes, 0, 8);
|
||||||
|
|
||||||
|
var timestamp = BitConverter.ToInt64(timestampBytes, 0);
|
||||||
|
var ticks = timestamp + GregorianCalendarStart.Ticks;
|
||||||
|
|
||||||
|
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();
|
||||||
|
var node = $"{x:X}";
|
||||||
|
var defaultNode = Encoding.UTF8.GetBytes(node.ToCharArray(), 0, 6);
|
||||||
|
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)
|
||||||
|
throw new ArgumentNullException(nameof(clockSequence));
|
||||||
|
|
||||||
|
if (node == null)
|
||||||
|
throw new ArgumentNullException(nameof(node));
|
||||||
|
|
||||||
|
if (clockSequence.Length != 2)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(clockSequence), "The clockSequence must be 2 bytes.");
|
||||||
|
|
||||||
|
if (node.Length != 6)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(node), "The node must be 6 bytes.");
|
||||||
|
|
||||||
|
var ticks = (dateTime - GregorianCalendarStart).Ticks;
|
||||||
|
var guid = new byte[ByteArraySize];
|
||||||
|
var timestamp = BitConverter.GetBytes(ticks);
|
||||||
|
|
||||||
|
// copy node
|
||||||
|
Array.Copy(node, 0, guid, NodeByte, Math.Min(6, node.Length));
|
||||||
|
|
||||||
|
// copy clock sequence
|
||||||
|
Array.Copy(clockSequence, 0, guid, GuidClockSequenceByte, Math.Min(2, clockSequence.Length));
|
||||||
|
|
||||||
|
// copy timestamp
|
||||||
|
Array.Copy(timestamp, 0, guid, TimestampByte, Math.Min(8, timestamp.Length));
|
||||||
|
|
||||||
|
// set the variant
|
||||||
|
guid[VariantByte] &= VariantByteMask;
|
||||||
|
guid[VariantByte] |= VariantByteShift;
|
||||||
|
|
||||||
|
// set the version
|
||||||
|
guid[VersionByte] &= VersionByteMask;
|
||||||
|
guid[VersionByte] |= (byte)GuidVersion.TimeBased << VersionByteShift;
|
||||||
|
|
||||||
|
return new Guid(guid);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,25 +1,53 @@
|
||||||
namespace FCS.Lib.Utility
|
// ***********************************************************************
|
||||||
|
// Assembly : FCS.Lib.Utility
|
||||||
|
// Author : fhdk
|
||||||
|
// Created : 2022 12 17 13:33
|
||||||
|
//
|
||||||
|
// Last Modified By: fhdk
|
||||||
|
// Last Modified On : 2023 03 14 09:16
|
||||||
|
// ***********************************************************************
|
||||||
|
// <copyright file="GuidVersion.cs" company="FCS">
|
||||||
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as
|
||||||
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
|
// License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
|
// </copyright>
|
||||||
|
// <summary></summary>
|
||||||
|
// ***********************************************************************
|
||||||
|
|
||||||
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Guid Version Enum
|
||||||
|
/// </summary>
|
||||||
|
public enum GuidVersion
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Guid Version Enum
|
/// Time
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public enum GuidVersion
|
TimeBased = 0x01,
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Time
|
/// Reserved
|
||||||
/// </summary>
|
/// </summary>
|
||||||
TimeBased = 0x01,
|
Reserved = 0x02,
|
||||||
/// <summary>
|
|
||||||
/// Reserved
|
/// <summary>
|
||||||
/// </summary>
|
/// Name
|
||||||
Reserved = 0x02,
|
/// </summary>
|
||||||
/// <summary>
|
NameBased = 0x03,
|
||||||
/// Name
|
|
||||||
/// </summary>
|
/// <summary>
|
||||||
NameBased = 0x03,
|
/// Random
|
||||||
/// <summary>
|
/// </summary>
|
||||||
/// Random
|
Random = 0x04
|
||||||
/// </summary>
|
|
||||||
Random = 0x04
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,25 +1,25 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 03-10-2015
|
// Created : 2023 01 23 07:31
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="IAsyncReadonlyRepo.cs" company="FCS-TECH">
|
// <copyright file="IAsyncReadonlyRepo.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2023-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
@ -30,67 +30,66 @@ using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface IRepositoryAsync
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity">The type of the t entity.</typeparam>
|
||||||
|
public interface IAsyncReadonlyRepo<TEntity> where TEntity : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface IRepositoryAsync
|
/// Queryable
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TEntity">The type of the t entity.</typeparam>
|
/// <returns>IQueryable<TEntity>.</returns>
|
||||||
public interface IAsyncReadonlyRepo<TEntity> where TEntity : class
|
IQueryable<TEntity> All();
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Queryable
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>IQueryable<TEntity>.</returns>
|
|
||||||
IQueryable<TEntity> All();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All items asynchronous.
|
/// All items asynchronous.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">The predicate.</param>
|
/// <param name="predicate">The predicate.</param>
|
||||||
/// <returns>Task<IList<TEntity>>.</returns>
|
/// <returns>Task<IList<TEntity>>.</returns>
|
||||||
Task<IList<TEntity>> AllAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<IList<TEntity>> AllAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Any item asynchronous.
|
/// Any item asynchronous.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">The predicate.</param>
|
/// <param name="predicate">The predicate.</param>
|
||||||
/// <returns>Task<System.Boolean>.</returns>
|
/// <returns>Task<System.Boolean>.</returns>
|
||||||
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Finds the asynchronous.
|
/// Finds the asynchronous.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">The predicate.</param>
|
/// <param name="predicate">The predicate.</param>
|
||||||
/// <returns>Task<TEntity>.</returns>
|
/// <returns>Task<TEntity>.</returns>
|
||||||
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Firsts the asynchronous.
|
/// Firsts the asynchronous.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">The predicate.</param>
|
/// <param name="predicate">The predicate.</param>
|
||||||
/// <returns>Task<TEntity>.</returns>
|
/// <returns>Task<TEntity>.</returns>
|
||||||
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Firsts the or default asynchronous.
|
/// Firsts the or default asynchronous.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">The predicate.</param>
|
/// <param name="predicate">The predicate.</param>
|
||||||
/// <returns>Task<TEntity>.</returns>
|
/// <returns>Task<TEntity>.</returns>
|
||||||
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Anies the specified predicate.
|
/// Anies the specified predicate.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">The predicate.</param>
|
/// <param name="predicate">The predicate.</param>
|
||||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||||
bool Any(Expression<Func<TEntity, bool>> predicate);
|
bool Any(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the by identifier.
|
/// Gets the by identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">The identifier.</param>
|
/// <param name="id">The identifier.</param>
|
||||||
/// <returns>TEntity.</returns>
|
/// <returns>TEntity.</returns>
|
||||||
TEntity GetById(string id);
|
TEntity GetById(string id);
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,61 +1,60 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 05-13-2020
|
// Created : 2022 12 17 13:33
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="IRepository.cs" company="FCS-TECH">
|
// <copyright file="IRepository.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface IRepository
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <typeparam name="TKey">The type of the TKey.</typeparam>
|
||||||
|
public interface IRepository<T, in TKey> where T : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface IRepository
|
/// Gets the specified identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <param name="id">The identifier.</param>
|
||||||
/// <typeparam name="TKey">The type of the TKey.</typeparam>
|
/// <returns>T.</returns>
|
||||||
public interface IRepository<T, in TKey> where T : class
|
T GetById(TKey id);
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the specified identifier.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="id">The identifier.</param>
|
|
||||||
/// <returns>T.</returns>
|
|
||||||
T GetById(TKey id);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the specified entity.
|
/// Creates the specified entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
void Create(T entity);
|
void Create(T entity);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates the specified entity.
|
/// Updates the specified entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
void Update(T entity);
|
void Update(T entity);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deletes the specified identifier.
|
/// Deletes the specified identifier.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">The identifier.</param>
|
/// <param name="id">The identifier.</param>
|
||||||
void Delete(TKey id);
|
void Delete(TKey id);
|
||||||
}
|
|
||||||
}
|
}
|
197
IRepositoryEx.cs
197
IRepositoryEx.cs
|
@ -1,25 +1,25 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 03-10-2015
|
// Created : 2022 12 17 13:33
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="IRepositoryEx.cs" company="FCS-TECH">
|
// <copyright file="IRepositoryEx.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
@ -29,99 +29,98 @@ using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface IRepositoryEx
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity">The type of the t entity</typeparam>
|
||||||
|
public interface IRepositoryEx<TEntity> where TEntity : class
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Interface IRepositoryEx
|
/// Get all entities synchronous
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TEntity">The type of the t entity</typeparam>
|
/// <param name="predicate">Predicate</param>
|
||||||
public interface IRepositoryEx<TEntity> where TEntity : class
|
/// <returns>Task<System.Boolean></returns>
|
||||||
{
|
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
/// <summary>
|
|
||||||
/// Get all entities synchronous
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="predicate">Predicate</param>
|
|
||||||
/// <returns>Task<System.Boolean></returns>
|
|
||||||
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find matching entity asynchronous
|
/// Find matching entity asynchronous
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">Predicate</param>
|
/// <param name="predicate">Predicate</param>
|
||||||
/// <returns>Task<TEntity></returns>
|
/// <returns>Task<TEntity></returns>
|
||||||
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find first matching entity asynchronous
|
/// Find first matching entity asynchronous
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">Predicate</param>
|
/// <param name="predicate">Predicate</param>
|
||||||
/// <returns>Task<TEntity></returns>
|
/// <returns>Task<TEntity></returns>
|
||||||
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get first entity matching query or default entity asynchronous
|
/// Get first entity matching query or default entity asynchronous
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">Predicate</param>
|
/// <param name="predicate">Predicate</param>
|
||||||
/// <returns>Task<TEntity></returns>
|
/// <returns>Task<TEntity></returns>
|
||||||
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
|
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add an entity
|
/// Add an entity
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
void Add(TEntity entity);
|
void Add(TEntity entity);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Attach the entity
|
/// Attach the entity
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
void Attach(TEntity entity);
|
void Attach(TEntity entity);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete the entity
|
/// Delete the entity
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
void Delete(TEntity entity);
|
void Delete(TEntity entity);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Anies the specified predicate.
|
/// Anies the specified predicate.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">The predicate.</param>
|
/// <param name="predicate">The predicate.</param>
|
||||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||||
bool Any(Expression<Func<TEntity, bool>> predicate);
|
bool Any(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get entity by id
|
/// Get entity by id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">The identifier.</param>
|
/// <param name="id">The identifier.</param>
|
||||||
/// <returns>TEntity</returns>
|
/// <returns>TEntity</returns>
|
||||||
TEntity GetById(string id);
|
TEntity GetById(string id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find first entity matching query
|
/// Find first entity matching query
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">Predicate</param>
|
/// <param name="predicate">Predicate</param>
|
||||||
/// <returns>TEntity</returns>
|
/// <returns>TEntity</returns>
|
||||||
TEntity First(Expression<Func<TEntity, bool>> predicate);
|
TEntity First(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find first matching entity or default entity
|
/// Find first matching entity or default entity
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">Predicate</param>
|
/// <param name="predicate">Predicate</param>
|
||||||
/// <returns>TEntity</returns>
|
/// <returns>TEntity</returns>
|
||||||
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
|
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Find all matching entities matching query
|
/// Find all matching entities matching query
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="predicate">Predicate</param>
|
/// <param name="predicate">Predicate</param>
|
||||||
/// <returns>IQueryable<TEntity></returns>
|
/// <returns>IQueryable<TEntity></returns>
|
||||||
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
|
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get all entities
|
/// Get all entities
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>IQueryable<TEntity></returns>
|
/// <returns>IQueryable<TEntity></returns>
|
||||||
IQueryable<TEntity> All();
|
IQueryable<TEntity> All();
|
||||||
}
|
|
||||||
}
|
}
|
928
Mogrify.cs
928
Mogrify.cs
|
@ -1,25 +1,25 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 27-08-2016
|
// Created : 2023 03 02 09:52
|
||||||
//
|
//
|
||||||
// Last Modified By : Frede H.
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="Mogrify.cs" company="FCS-TECH">
|
// <copyright file="Mogrify.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2023-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
@ -33,446 +33,462 @@ using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Mogrify between units
|
||||||
|
/// </summary>
|
||||||
|
public static class Mogrify
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Mogrify between units
|
/// Remove everything but digits and country code
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Mogrify
|
/// <param name="phone"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string SanitizePhone(string phone)
|
||||||
{
|
{
|
||||||
/// <summary>
|
if (string.IsNullOrWhiteSpace(phone))
|
||||||
/// Get month from timestamp
|
return "";
|
||||||
/// </summary>
|
phone = phone.Replace("+45", "").Replace("+46", "").Replace("+47", "");
|
||||||
/// <param name="timeStamp"></param>
|
var regexObj = new Regex(@"[^\d]");
|
||||||
/// <returns>integer</returns>
|
return regexObj.Replace(phone, "");
|
||||||
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>
|
|
||||||
/// return iso8601 string from timestamp
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="timestamp"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string TimestampToIso8601(long timestamp)
|
|
||||||
{
|
|
||||||
return DateTimeIso8601(TimeStampToDateTime(timestamp));
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// return date as ISO
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="date"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static string DateTimeIso8601(DateTime date)
|
|
||||||
{
|
|
||||||
return date.ToString("o",CultureInfo.InvariantCulture);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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);
|
|
||||||
var dt2 = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59);
|
|
||||||
return new Dictionary<string, long>{
|
|
||||||
{ "lower", DateTimeToTimeStamp(dt1) },
|
|
||||||
{ "upper", DateTimeToTimeStamp(dt2) }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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);
|
|
||||||
if (!result)
|
|
||||||
return 0;
|
|
||||||
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>
|
|
||||||
/// get timestamp from current date time
|
|
||||||
/// </summary>
|
|
||||||
/// <returns><see cref="long"/></returns>
|
|
||||||
public static long CurrentDateTimeToTimeStamp()
|
|
||||||
{
|
|
||||||
return Convert.ToUInt32(DateTimeToTimeStamp(DateTime.Now));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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);
|
|
||||||
var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
||||||
|
|
||||||
if (dateTime >= bigDate)
|
|
||||||
return Convert.ToInt64((bigDate - nixDate).TotalSeconds) +
|
|
||||||
Convert.ToInt64((dateTime - bigDate).TotalSeconds);
|
|
||||||
|
|
||||||
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>
|
|
||||||
/// 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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;
|
|
||||||
if (list == null) return empty;
|
|
||||||
var enumerator = (IEnumerator) list.GetType().GetMethod("GetEnumerator")?.Invoke(list, null);
|
|
||||||
while (enumerator != null && enumerator.MoveNext())
|
|
||||||
if (enumerator.Current != null)
|
|
||||||
empty = string.Concat(empty, enumerator.Current.ToString(), delimiter);
|
|
||||||
|
|
||||||
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))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var flag = false;
|
|
||||||
var upper = value.ToUpperInvariant();
|
|
||||||
if (string.Compare(upper, "true", StringComparison.OrdinalIgnoreCase) == 0)
|
|
||||||
{
|
|
||||||
flag = true;
|
|
||||||
}
|
|
||||||
else if (string.CompareOrdinal(upper, "false") == 0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
else if (string.CompareOrdinal(upper, "1") == 0)
|
|
||||||
{
|
|
||||||
flag = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
return
|
|
||||||
!decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number,
|
|
||||||
CultureInfo.InvariantCulture, out var num)
|
|
||||||
? (decimal?) null
|
|
||||||
: 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));
|
|
||||||
if (string.IsNullOrEmpty(delimiter)) throw new ArgumentNullException(nameof(delimiter));
|
|
||||||
|
|
||||||
var ts = new List<T>();
|
|
||||||
var strArrays = value.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
foreach (var str in strArrays)
|
|
||||||
{
|
|
||||||
var o = typeof(T).FullName;
|
|
||||||
if (o == null) continue;
|
|
||||||
var upperInvariant = o.ToUpperInvariant();
|
|
||||||
if (string.CompareOrdinal(upperInvariant, "system.string") == 0)
|
|
||||||
{
|
|
||||||
ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
|
|
||||||
}
|
|
||||||
else if (string.CompareOrdinal(upperInvariant, "system.int32") == 0)
|
|
||||||
{
|
|
||||||
ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
|
|
||||||
}
|
|
||||||
else if (string.CompareOrdinal(upperInvariant, "system.guid") == 0)
|
|
||||||
{
|
|
||||||
var guid = new Guid(str);
|
|
||||||
ts.Add((T) Convert.ChangeType(guid, typeof(T), CultureInfo.InvariantCulture));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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;
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// return iso8601 string from timestamp
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="timestamp"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string TimestampToIso8601(long timestamp)
|
||||||
|
{
|
||||||
|
return DateTimeIso8601(TimeStampToDateTime(timestamp));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// return date as ISO
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="date"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string DateTimeIso8601(DateTime date)
|
||||||
|
{
|
||||||
|
return date.ToString("o", CultureInfo.InvariantCulture);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
var dt2 = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59);
|
||||||
|
return new Dictionary<string, long>
|
||||||
|
{
|
||||||
|
{ "lower", DateTimeToTimeStamp(dt1) },
|
||||||
|
{ "upper", DateTimeToTimeStamp(dt2) }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
if (!result)
|
||||||
|
return 0;
|
||||||
|
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>
|
||||||
|
/// get timestamp from current date time
|
||||||
|
/// </summary>
|
||||||
|
/// <returns><see cref="long"/></returns>
|
||||||
|
public static long CurrentDateTimeToTimeStamp()
|
||||||
|
{
|
||||||
|
return Convert.ToUInt32(DateTimeToTimeStamp(DateTime.Now));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
var nixDate = new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
||||||
|
|
||||||
|
if (dateTime >= bigDate)
|
||||||
|
return Convert.ToInt64((bigDate - nixDate).TotalSeconds) +
|
||||||
|
Convert.ToInt64((dateTime - bigDate).TotalSeconds);
|
||||||
|
|
||||||
|
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>
|
||||||
|
/// 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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;
|
||||||
|
if (list == null) return empty;
|
||||||
|
var enumerator = (IEnumerator)list.GetType().GetMethod("GetEnumerator")?.Invoke(list, null);
|
||||||
|
while (enumerator != null && enumerator.MoveNext())
|
||||||
|
if (enumerator.Current != null)
|
||||||
|
empty = string.Concat(empty, enumerator.Current.ToString(), delimiter);
|
||||||
|
|
||||||
|
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))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var flag = false;
|
||||||
|
var upper = value.ToUpperInvariant();
|
||||||
|
if (string.Compare(upper, "true", StringComparison.OrdinalIgnoreCase) == 0)
|
||||||
|
{
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
else if (string.CompareOrdinal(upper, "false") == 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
else if (string.CompareOrdinal(upper, "1") == 0)
|
||||||
|
{
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
return
|
||||||
|
!decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number,
|
||||||
|
CultureInfo.InvariantCulture, out var num)
|
||||||
|
? null
|
||||||
|
: 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));
|
||||||
|
if (string.IsNullOrEmpty(delimiter)) throw new ArgumentNullException(nameof(delimiter));
|
||||||
|
|
||||||
|
var ts = new List<T>();
|
||||||
|
var strArrays = value.Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
foreach (var str in strArrays)
|
||||||
|
{
|
||||||
|
var o = typeof(T).FullName;
|
||||||
|
if (o == null) continue;
|
||||||
|
var upperInvariant = o.ToUpperInvariant();
|
||||||
|
if (string.CompareOrdinal(upperInvariant, "system.string") == 0)
|
||||||
|
{
|
||||||
|
ts.Add((T)Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
else if (string.CompareOrdinal(upperInvariant, "system.int32") == 0)
|
||||||
|
{
|
||||||
|
ts.Add((T)Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
else if (string.CompareOrdinal(upperInvariant, "system.guid") == 0)
|
||||||
|
{
|
||||||
|
var guid = new Guid(str);
|
||||||
|
ts.Add((T)Convert.ChangeType(guid, typeof(T), CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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;
|
||||||
|
//}
|
||||||
}
|
}
|
|
@ -17,5 +17,5 @@ using System.Runtime.InteropServices;
|
||||||
[assembly: ComVisible(false)]
|
[assembly: ComVisible(false)]
|
||||||
[assembly: Guid("8D850197-78DB-4D16-A91F-E5BB6E8880A7")]
|
[assembly: Guid("8D850197-78DB-4D16-A91F-E5BB6E8880A7")]
|
||||||
|
|
||||||
[assembly: AssemblyVersion("1.0.23023.0820")]
|
[assembly: AssemblyVersion("1.0.23077.1334")]
|
||||||
[assembly: AssemblyFileVersion("1.0.23023.0820")]
|
[assembly: AssemblyFileVersion("1.0.23077.1334")]
|
151
QueryHelper.cs
151
QueryHelper.cs
|
@ -1,25 +1,25 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 01-01-2022
|
// Created : 2022 12 17 13:33
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="QueryHelper.cs" company="Frede Hundewadt">
|
// <copyright file="QueryHelper.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
@ -29,70 +29,69 @@ using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class QueryHelper.
|
||||||
|
/// </summary>
|
||||||
|
public static class QueryHelper
|
||||||
{
|
{
|
||||||
|
// https://stackoverflow.com/a/45761590
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class QueryHelper.
|
/// OrderBy
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class QueryHelper
|
/// <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)
|
||||||
{
|
{
|
||||||
// https://stackoverflow.com/a/45761590
|
var entityType = typeof(TModel);
|
||||||
|
var p = entityType.GetProperty(name);
|
||||||
|
var m = typeof(QueryHelper)
|
||||||
|
.GetMethod("OrderByProperty")
|
||||||
|
?.MakeGenericMethod(entityType, p.PropertyType);
|
||||||
|
|
||||||
/// <summary>
|
return (IQueryable<TModel>)m.Invoke(null, new object[] { q, p, desc });
|
||||||
/// 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 });
|
/// <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);
|
||||||
|
|
||||||
/// <summary>
|
return (IQueryable<TModel>)m.Invoke(null, new object[] { q, p, desc });
|
||||||
/// 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 });
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// OrderByProperty
|
/// OrderByProperty
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="q"></param>
|
/// <param name="q"></param>
|
||||||
/// <param name="p"></param>
|
/// <param name="p"></param>
|
||||||
/// <param name="desc"></param>
|
/// <param name="desc"></param>
|
||||||
/// <typeparam name="TModel"></typeparam>
|
/// <typeparam name="TModel"></typeparam>
|
||||||
/// <typeparam name="TRet"></typeparam>
|
/// <typeparam name="TRet"></typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IQueryable<TModel> OrderByProperty<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p, bool desc)
|
public static IQueryable<TModel> OrderByProperty<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p, bool desc)
|
||||||
{
|
{
|
||||||
var pe = Expression.Parameter(typeof(TModel));
|
var pe = Expression.Parameter(typeof(TModel));
|
||||||
Expression se = Expression.Convert(Expression.Property(pe, p), typeof(object));
|
Expression se = Expression.Convert(Expression.Property(pe, p), typeof(object));
|
||||||
var exp = Expression.Lambda<Func<TModel, TRet>>(se, pe);
|
var exp = Expression.Lambda<Func<TModel, TRet>>(se, pe);
|
||||||
|
|
||||||
return desc ? q.OrderByDescending(exp) : q.OrderBy(exp);
|
|
||||||
}
|
|
||||||
|
|
||||||
}}
|
return desc ? q.OrderByDescending(exp) : q.OrderBy(exp);
|
||||||
|
}
|
||||||
|
}
|
725
Squid.cs
725
Squid.cs
|
@ -1,395 +1,394 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 2020-07-01
|
// Created : 2022 12 17 13:33
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 02-24-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="Squid.cs" company="FCS-TECH">
|
// <copyright file="Squid.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the Affero GNU General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// Affero GNU General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the Affero GNU General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary>Derived from https:github.com/csharpvitamins/CSharpVitamins.ShortGuid</summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A wrapper for handling URL-safe Base64 encoded globally unique identifiers (GUID).
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>Special characters are replaced (/, +) or removed (==).
|
||||||
|
/// Derived from https:github.com/csharpvitamins/CSharpVitamins.ShortGuid</remarks>
|
||||||
|
[DebuggerDisplay("{" + nameof(Value) + "}")]
|
||||||
|
public readonly struct Squid : IEquatable<Squid>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A wrapper for handling URL-safe Base64 encoded globally unique identifiers (GUID).
|
/// A read-only object of the Squid struct.
|
||||||
|
/// Value is guaranteed to be all zeroes.
|
||||||
|
/// Equivalent to <see cref="Guid.Empty" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>Special characters are replaced (/, +) or removed (==).
|
public static readonly Squid Empty = new(Guid.Empty);
|
||||||
/// Derived from https:github.com/csharpvitamins/CSharpVitamins.ShortGuid</remarks>
|
|
||||||
[DebuggerDisplay("{" + nameof(Value) + "}")]
|
/// <summary>
|
||||||
public readonly struct Squid : IEquatable<Squid>
|
/// Creates a new Squid from a Squid encoded string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">A valid Squid encodd string.</param>
|
||||||
|
public Squid(string value)
|
||||||
{
|
{
|
||||||
/// <summary>
|
Value = value;
|
||||||
/// A read-only object of the Squid struct.
|
Guid = DecodeSquid(value);
|
||||||
/// Value is guaranteed to be all zeroes.
|
}
|
||||||
/// Equivalent to <see cref="Guid.Empty" />.
|
|
||||||
/// </summary>
|
|
||||||
public static readonly Squid Empty = new(Guid.Empty);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new Squid from a Squid encoded string.
|
/// Creates a new Squid with the given <see cref="System.Guid" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">A valid Squid encodd string.</param>
|
/// <param name="obj">A valid System.Guid object.</param>
|
||||||
public Squid(string value)
|
public Squid(Guid obj)
|
||||||
{
|
{
|
||||||
Value = value;
|
Value = EncodeGuid(obj);
|
||||||
Guid = DecodeSquid(value);
|
Guid = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new Squid with the given <see cref="System.Guid" />.
|
/// Gets the underlying <see cref="System.Guid" /> for the encoded Squid.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj">A valid System.Guid object.</param>
|
/// <value>The unique identifier.</value>
|
||||||
public Squid(Guid obj)
|
|
||||||
{
|
|
||||||
Value = EncodeGuid(obj);
|
|
||||||
Guid = obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the underlying <see cref="System.Guid" /> for the encoded Squid.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The unique identifier.</value>
|
|
||||||
#pragma warning disable CA1720 // Identifier contains type name
|
#pragma warning disable CA1720 // Identifier contains type name
|
||||||
public Guid Guid { get; }
|
public Guid Guid { get; }
|
||||||
#pragma warning restore CA1720 // Identifier contains type name
|
#pragma warning restore CA1720 // Identifier contains type name
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The encoded string value of the <see cref="Guid" />
|
/// The encoded string value of the <see cref="Guid" />
|
||||||
/// as an URL-safe Base64 string.
|
/// as an URL-safe Base64 string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The value.</value>
|
/// <value>The value.</value>
|
||||||
public string Value { get; }
|
public string Value { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the encoded URL-safe Base64 string.
|
/// Returns the encoded URL-safe Base64 string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>A <see cref="string" /> that represents this instance.</returns>
|
/// <returns>A <see cref="string" /> that represents this instance.</returns>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a value indicating whether this object and a specified object represent the same type and value.
|
||||||
|
/// Compares for equality against other string, Guid and Squid types.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">A Systerm.String, System.Guid or Squid object</param>
|
||||||
|
/// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
return obj is Squid other && Equals(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Equality comparison
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">A valid Squid object</param>
|
||||||
|
/// <returns>A boolean indicating equality.</returns>
|
||||||
|
public bool Equals(Squid obj)
|
||||||
|
{
|
||||||
|
return Guid.Equals(obj.Guid) && Value == obj.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the hash code for the underlying <see cref="System.Guid" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
unchecked
|
||||||
{
|
{
|
||||||
return Value;
|
return (Guid.GetHashCode() * 397) ^ (Value != null ? Value.GetHashCode() : 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a value indicating whether this object and a specified object represent the same type and value.
|
/// Initialises a new object of the Squid using <see cref="Guid.NewGuid()" />.
|
||||||
/// Compares for equality against other string, Guid and Squid types.
|
/// </summary>
|
||||||
/// </summary>
|
/// <returns>New Squid object</returns>
|
||||||
/// <param name="obj">A Systerm.String, System.Guid or Squid object</param>
|
public static Squid NewGuid()
|
||||||
/// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
|
{
|
||||||
public override bool Equals(object obj)
|
return new Squid(Guid.NewGuid());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encode string as a new Squid encoded string.
|
||||||
|
/// The encoding is similar to Base64 with
|
||||||
|
/// non-URL safe characters replaced, and padding removed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">A valid <see cref="System.Guid" />.Tostring().</param>
|
||||||
|
/// <returns>A 22 character URL-safe Base64 string.</returns>
|
||||||
|
public static string EncodeString(string value)
|
||||||
|
{
|
||||||
|
var guid = new Guid(value);
|
||||||
|
return EncodeGuid(guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Encode a <see cref="System.Guid" /> object to Squid.
|
||||||
|
/// The encoding is similar to Base64 with
|
||||||
|
/// non-URL safe characters replaced, and padding removed.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">A valid <see cref="System.Guid" /> object.</param>
|
||||||
|
/// <returns>A 22 character URL-safe Base64 string.</returns>
|
||||||
|
public static string EncodeGuid(Guid obj)
|
||||||
|
{
|
||||||
|
var encoded = Convert.ToBase64String(obj.ToByteArray());
|
||||||
|
encoded = encoded
|
||||||
|
.Replace("/", "_")
|
||||||
|
.Replace("+", "-");
|
||||||
|
return encoded.Substring(0, 22);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decode Squid string to a <see cref="System.Guid" />.
|
||||||
|
/// See also <seealso cref="TryDecode(string, out System.Guid)" /> or
|
||||||
|
/// <seealso cref="TryParse(string, out System.Guid)" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">A valid Squid encoded string.</param>
|
||||||
|
/// <returns>A new <see cref="System.Guid" /> object from the parsed string.</returns>
|
||||||
|
public static Guid DecodeSquid(string value)
|
||||||
|
{
|
||||||
|
if (value == null) return Empty;
|
||||||
|
value = value
|
||||||
|
.Replace("_", "/")
|
||||||
|
.Replace("-", "+");
|
||||||
|
|
||||||
|
var blob = Convert.FromBase64String(value + "==");
|
||||||
|
return new Guid(blob);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Squid to Guid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj">A valid Squid object.</param>
|
||||||
|
/// <returns>System.Guid object.</returns>
|
||||||
|
public static Guid FromSquid(Squid obj)
|
||||||
|
{
|
||||||
|
return obj.Guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// String to Squid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">String value to convert</param>
|
||||||
|
/// <returns>A Squid object.</returns>
|
||||||
|
public static Squid FromString(string value)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(value))
|
||||||
|
return Empty;
|
||||||
|
return TryParse(value, out Squid obj) ? obj : Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decodes the given value to a <see cref="System.Guid" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The Squid encoded string to decode.</param>
|
||||||
|
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
|
||||||
|
/// <returns>A boolean indicating if the decode was successful.</returns>
|
||||||
|
public static bool TryDecode(string value, out Guid obj)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return obj is Squid other && Equals(other);
|
// Decode as Squid
|
||||||
|
obj = DecodeSquid(value);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Equality comparison
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="obj">A valid Squid object</param>
|
|
||||||
/// <returns>A boolean indicating equality.</returns>
|
|
||||||
public bool Equals(Squid obj)
|
|
||||||
{
|
|
||||||
return Guid.Equals(obj.Guid) && Value == obj.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Returns the hash code for the underlying <see cref="System.Guid" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
unchecked
|
|
||||||
{
|
|
||||||
return (Guid.GetHashCode() * 397) ^ (Value != null ? Value.GetHashCode() : 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initialises a new object of the Squid using <see cref="Guid.NewGuid()" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>New Squid object</returns>
|
|
||||||
public static Squid NewGuid()
|
|
||||||
{
|
|
||||||
return new(Guid.NewGuid());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Encode string as a new Squid encoded string.
|
|
||||||
/// The encoding is similar to Base64 with
|
|
||||||
/// non-URL safe characters replaced, and padding removed.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">A valid <see cref="System.Guid" />.Tostring().</param>
|
|
||||||
/// <returns>A 22 character URL-safe Base64 string.</returns>
|
|
||||||
public static string EncodeString(string value)
|
|
||||||
{
|
|
||||||
var guid = new Guid(value);
|
|
||||||
return EncodeGuid(guid);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Encode a <see cref="System.Guid" /> object to Squid.
|
|
||||||
/// The encoding is similar to Base64 with
|
|
||||||
/// non-URL safe characters replaced, and padding removed.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="obj">A valid <see cref="System.Guid" /> object.</param>
|
|
||||||
/// <returns>A 22 character URL-safe Base64 string.</returns>
|
|
||||||
public static string EncodeGuid(Guid obj)
|
|
||||||
{
|
|
||||||
var encoded = Convert.ToBase64String(obj.ToByteArray());
|
|
||||||
encoded = encoded
|
|
||||||
.Replace("/", "_")
|
|
||||||
.Replace("+", "-");
|
|
||||||
return encoded.Substring(0, 22);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Decode Squid string to a <see cref="System.Guid" />.
|
|
||||||
/// See also <seealso cref="TryDecode(string, out System.Guid)" /> or
|
|
||||||
/// <seealso cref="TryParse(string, out System.Guid)" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">A valid Squid encoded string.</param>
|
|
||||||
/// <returns>A new <see cref="System.Guid" /> object from the parsed string.</returns>
|
|
||||||
public static Guid DecodeSquid(string value)
|
|
||||||
{
|
|
||||||
if (value == null) return Empty;
|
|
||||||
value = value
|
|
||||||
.Replace("_", "/")
|
|
||||||
.Replace("-", "+");
|
|
||||||
|
|
||||||
var blob = Convert.FromBase64String(value + "==");
|
|
||||||
return new Guid(blob);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Squid to Guid.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="obj">A valid Squid object.</param>
|
|
||||||
/// <returns>System.Guid object.</returns>
|
|
||||||
public static Guid FromSquid(Squid obj)
|
|
||||||
{
|
|
||||||
return obj.Guid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// String to Squid.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">String value to convert</param>
|
|
||||||
/// <returns>A Squid object.</returns>
|
|
||||||
public static Squid FromString(string value)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
return Empty;
|
|
||||||
return TryParse(value, out Squid obj) ? obj : Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Decodes the given value to a <see cref="System.Guid" />.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The Squid encoded string to decode.</param>
|
|
||||||
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
|
|
||||||
/// <returns>A boolean indicating if the decode was successful.</returns>
|
|
||||||
public static bool TryDecode(string value, out Guid obj)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Decode as Squid
|
|
||||||
obj = DecodeSquid(value);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#pragma warning disable CA1031 // Do not catch general exception types
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
#pragma warning restore CA1031 // Do not catch general exception types
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
{
|
|
||||||
// Return empty Guid
|
|
||||||
obj = Guid.Empty;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tries to parse the given string value and
|
|
||||||
/// outputs the <see cref="Squid" /> object.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
|
|
||||||
/// <param name="obj">A new <see cref="Squid" /> object from the parsed string.</param>
|
|
||||||
/// <returns>A boolean indicating if the parse was successful.</returns>
|
|
||||||
public static bool TryParse(string value, out Squid obj)
|
|
||||||
{
|
{
|
||||||
// Parse as Squid string.
|
// Return empty Guid
|
||||||
if (TryDecode(value, out var oGuid))
|
|
||||||
{
|
|
||||||
obj = oGuid;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse as Guid string.
|
|
||||||
if (Guid.TryParse(value, out oGuid))
|
|
||||||
{
|
|
||||||
obj = oGuid;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
obj = Empty;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tries to parse the string value and
|
|
||||||
/// outputs the underlying <see cref="System.Guid" /> object.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
|
|
||||||
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
|
|
||||||
/// <returns>A boolean indicating if the parse was successful.</returns>
|
|
||||||
public static bool TryParse(string value, out Guid obj)
|
|
||||||
{
|
|
||||||
// Try a Squid string.
|
|
||||||
if (TryDecode(value, out obj))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// Try a Guid string.
|
|
||||||
if (Guid.TryParse(value, out obj))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
obj = Guid.Empty;
|
obj = Guid.Empty;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Operators
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if both Squid objects have the same
|
|
||||||
/// underlying <see cref="System.Guid" /> value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">The x.</param>
|
|
||||||
/// <param name="y">The y.</param>
|
|
||||||
/// <returns>The result of the operator.</returns>
|
|
||||||
public static bool operator ==(Squid x, Squid y)
|
|
||||||
{
|
|
||||||
return x.Guid == y.Guid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if both objects have the same
|
|
||||||
/// underlying <see cref="System.Guid" /> value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">The x.</param>
|
|
||||||
/// <param name="y">The y.</param>
|
|
||||||
/// <returns>The result of the operator.</returns>
|
|
||||||
public static bool operator ==(Squid x, Guid y)
|
|
||||||
{
|
|
||||||
return x.Guid == y;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if both objects have the same
|
|
||||||
/// underlying <see cref="System.Guid" /> value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">The x.</param>
|
|
||||||
/// <param name="y">The y.</param>
|
|
||||||
/// <returns>The result of the operator.</returns>
|
|
||||||
public static bool operator ==(Guid x, Squid y)
|
|
||||||
{
|
|
||||||
return y == x; // NB: order of arguments
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if both Squid objects do not have the same
|
|
||||||
/// underlying <see cref="System.Guid" /> value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">The x.</param>
|
|
||||||
/// <param name="y">The y.</param>
|
|
||||||
/// <returns>The result of the operator.</returns>
|
|
||||||
public static bool operator !=(Squid x, Squid y)
|
|
||||||
{
|
|
||||||
return !(x == y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if both objects do not have the same
|
|
||||||
/// underlying <see cref="System.Guid" /> value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">The x.</param>
|
|
||||||
/// <param name="y">The y.</param>
|
|
||||||
/// <returns>The result of the operator.</returns>
|
|
||||||
public static bool operator !=(Squid x, Guid y)
|
|
||||||
{
|
|
||||||
return !(x == y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Determines if both objects do not have the same
|
|
||||||
/// underlying <see cref="System.Guid" /> value.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="x">The x.</param>
|
|
||||||
/// <param name="y">The y.</param>
|
|
||||||
/// <returns>The result of the operator.</returns>
|
|
||||||
public static bool operator !=(Guid x, Squid y)
|
|
||||||
{
|
|
||||||
return !(x == y);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Implicitly converts the Squid to
|
|
||||||
/// its string equivalent.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="oSquid">The o squid.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static implicit operator string(Squid oSquid)
|
|
||||||
{
|
|
||||||
return oSquid.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Implicitly converts the Squid to
|
|
||||||
/// its <see cref="System.Guid" /> equivalent.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="oSquid">The o squid.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static implicit operator Guid(Squid oSquid)
|
|
||||||
{
|
|
||||||
return oSquid.Guid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Implicitly converts the string to a Squid.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="value">The value.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static implicit operator Squid(string value)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
return Empty;
|
|
||||||
|
|
||||||
return TryParse(value, out Squid oSquid) ? oSquid : Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Implicitly converts the <see cref="System.Guid" /> to a Squid.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="oGuid">The o unique identifier.</param>
|
|
||||||
/// <returns>The result of the conversion.</returns>
|
|
||||||
public static implicit operator Squid(Guid oGuid)
|
|
||||||
{
|
|
||||||
return oGuid == Guid.Empty ? Empty : new Squid(oGuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to parse the given string value and
|
||||||
|
/// outputs the <see cref="Squid" /> object.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
|
||||||
|
/// <param name="obj">A new <see cref="Squid" /> object from the parsed string.</param>
|
||||||
|
/// <returns>A boolean indicating if the parse was successful.</returns>
|
||||||
|
public static bool TryParse(string value, out Squid obj)
|
||||||
|
{
|
||||||
|
// Parse as Squid string.
|
||||||
|
if (TryDecode(value, out var oGuid))
|
||||||
|
{
|
||||||
|
obj = oGuid;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse as Guid string.
|
||||||
|
if (Guid.TryParse(value, out oGuid))
|
||||||
|
{
|
||||||
|
obj = oGuid;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = Empty;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to parse the string value and
|
||||||
|
/// outputs the underlying <see cref="System.Guid" /> object.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
|
||||||
|
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
|
||||||
|
/// <returns>A boolean indicating if the parse was successful.</returns>
|
||||||
|
public static bool TryParse(string value, out Guid obj)
|
||||||
|
{
|
||||||
|
// Try a Squid string.
|
||||||
|
if (TryDecode(value, out obj))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Try a Guid string.
|
||||||
|
if (Guid.TryParse(value, out obj))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
obj = Guid.Empty;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Operators
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if both Squid objects have the same
|
||||||
|
/// underlying <see cref="System.Guid" /> value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">The x.</param>
|
||||||
|
/// <param name="y">The y.</param>
|
||||||
|
/// <returns>The result of the operator.</returns>
|
||||||
|
public static bool operator ==(Squid x, Squid y)
|
||||||
|
{
|
||||||
|
return x.Guid == y.Guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if both objects have the same
|
||||||
|
/// underlying <see cref="System.Guid" /> value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">The x.</param>
|
||||||
|
/// <param name="y">The y.</param>
|
||||||
|
/// <returns>The result of the operator.</returns>
|
||||||
|
public static bool operator ==(Squid x, Guid y)
|
||||||
|
{
|
||||||
|
return x.Guid == y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if both objects have the same
|
||||||
|
/// underlying <see cref="System.Guid" /> value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">The x.</param>
|
||||||
|
/// <param name="y">The y.</param>
|
||||||
|
/// <returns>The result of the operator.</returns>
|
||||||
|
public static bool operator ==(Guid x, Squid y)
|
||||||
|
{
|
||||||
|
return y == x; // NB: order of arguments
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if both Squid objects do not have the same
|
||||||
|
/// underlying <see cref="System.Guid" /> value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">The x.</param>
|
||||||
|
/// <param name="y">The y.</param>
|
||||||
|
/// <returns>The result of the operator.</returns>
|
||||||
|
public static bool operator !=(Squid x, Squid y)
|
||||||
|
{
|
||||||
|
return !(x == y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if both objects do not have the same
|
||||||
|
/// underlying <see cref="System.Guid" /> value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">The x.</param>
|
||||||
|
/// <param name="y">The y.</param>
|
||||||
|
/// <returns>The result of the operator.</returns>
|
||||||
|
public static bool operator !=(Squid x, Guid y)
|
||||||
|
{
|
||||||
|
return !(x == y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Determines if both objects do not have the same
|
||||||
|
/// underlying <see cref="System.Guid" /> value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">The x.</param>
|
||||||
|
/// <param name="y">The y.</param>
|
||||||
|
/// <returns>The result of the operator.</returns>
|
||||||
|
public static bool operator !=(Guid x, Squid y)
|
||||||
|
{
|
||||||
|
return !(x == y);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Implicitly converts the Squid to
|
||||||
|
/// its string equivalent.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oSquid">The o squid.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator string(Squid oSquid)
|
||||||
|
{
|
||||||
|
return oSquid.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Implicitly converts the Squid to
|
||||||
|
/// its <see cref="System.Guid" /> equivalent.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oSquid">The o squid.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator Guid(Squid oSquid)
|
||||||
|
{
|
||||||
|
return oSquid.Guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Implicitly converts the string to a Squid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">The value.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator Squid(string value)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(value))
|
||||||
|
return Empty;
|
||||||
|
|
||||||
|
return TryParse(value, out Squid oSquid) ? oSquid : Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Implicitly converts the <see cref="System.Guid" /> to a Squid.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oGuid">The o unique identifier.</param>
|
||||||
|
/// <returns>The result of the conversion.</returns>
|
||||||
|
public static implicit operator Squid(Guid oGuid)
|
||||||
|
{
|
||||||
|
return oGuid == Guid.Empty ? Empty : new Squid(oGuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
117
StringOptions.cs
117
StringOptions.cs
|
@ -1,76 +1,75 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 2020-09-09
|
// Created : 2022 12 17 13:33
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 03-14-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="StringOptions.cs" company="FCS-TECH">
|
// <copyright file="StringOptions.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2022-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
namespace FCS.Lib.Utility;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class StringOptions.
|
||||||
|
/// </summary>
|
||||||
|
public class StringOptions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class StringOptions.
|
/// Gets or sets the required length of a string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class StringOptions
|
/// <value>The length of the required.</value>
|
||||||
{
|
public int RequiredLength { get; set; }
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the required length of a string
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The length of the required.</value>
|
|
||||||
public int RequiredLength { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether to [require non letter or digit].
|
/// Gets or sets a value indicating whether to [require non letter or digit].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value><c>true</c> if [require non letter or digit]; otherwise, <c>false</c>.</value>
|
/// <value><c>true</c> if [require non letter or digit]; otherwise, <c>false</c>.</value>
|
||||||
public bool RequireNonLetterOrDigit { get; set; }
|
public bool RequireNonLetterOrDigit { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether to [require digit].
|
/// Gets or sets a value indicating whether to [require digit].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value><c>true</c> if [require digit]; otherwise, <c>false</c>.</value>
|
/// <value><c>true</c> if [require digit]; otherwise, <c>false</c>.</value>
|
||||||
public bool RequireDigit { get; set; }
|
public bool RequireDigit { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether to [require lowercase].
|
/// Gets or sets a value indicating whether to [require lowercase].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value><c>true</c> if [require lowercase]; otherwise, <c>false</c>.</value>
|
/// <value><c>true</c> if [require lowercase]; otherwise, <c>false</c>.</value>
|
||||||
public bool RequireLowercase { get; set; }
|
public bool RequireLowercase { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether to [require uppercase].
|
/// Gets or sets a value indicating whether to [require uppercase].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value><c>true</c> if [require uppercase]; otherwise, <c>false</c>.</value>
|
/// <value><c>true</c> if [require uppercase]; otherwise, <c>false</c>.</value>
|
||||||
public bool RequireUppercase { get; set; }
|
public bool RequireUppercase { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the required unique chars.
|
/// Gets or sets the required unique chars.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The required unique chars.</value>
|
/// <value>The required unique chars.</value>
|
||||||
public int RequiredUniqueChars { get; set; }
|
public int RequiredUniqueChars { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether to [require non alphanumeric].
|
/// Gets or sets a value indicating whether to [require non alphanumeric].
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value><c>true</c> if [require non alphanumeric]; otherwise, <c>false</c>.</value>
|
/// <value><c>true</c> if [require non alphanumeric]; otherwise, <c>false</c>.</value>
|
||||||
public bool RequireNonAlphanumeric { get; set; }
|
public bool RequireNonAlphanumeric { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,362 +1,358 @@
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// Assembly : FCS.Lib.Utility
|
// Assembly : FCS.Lib.Utility
|
||||||
// Author : FH
|
// Author : fhdk
|
||||||
// Created : 03-30-2022
|
// Created : 2023 03 09 17:42
|
||||||
//
|
//
|
||||||
// Last Modified By : FH
|
// Last Modified By: fhdk
|
||||||
// Last Modified On : 04-19-2022
|
// Last Modified On : 2023 03 14 09:16
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
// <copyright file="VatFormatValidator.cs" company="FCS-TECH">
|
// <copyright file="VatFormatValidator.cs" company="FCS">
|
||||||
// Copyright (C) 2022 FCS Frede's Computer Services.
|
// Copyright (C) 2023-2023 FCS Frede's Computer Services.
|
||||||
// This program is free software: you can redistribute it and/or modify
|
// This program is free software: you can redistribute it and/or modify
|
||||||
// it under the terms of the GNU Affero General Public License as
|
// it under the terms of the GNU Affero General Public License as
|
||||||
// published by the Free Software Foundation, either version 3 of the
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
// License, or (at your option) any later version.
|
// License, or (at your option) any later version.
|
||||||
//
|
//
|
||||||
// This program is distributed in the hope that it will be useful,
|
// This program is distributed in the hope that it will be useful,
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
// GNU Affero General Public License for more details.
|
// GNU Affero General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see [https://www.gnu.org/licenses]
|
// along with this program. If not, see [https://www.gnu.org/licenses]
|
||||||
// </copyright>
|
// </copyright>
|
||||||
// <summary></summary>
|
// <summary></summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace FCS.Lib.Utility
|
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/
|
||||||
|
|
||||||
|
|
||||||
|
//https://www.bolagsverket.se/apierochoppnadata.2531.html
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vat format validator
|
/// Check vat number format
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class VatFormatValidator
|
/// <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)
|
||||||
{
|
{
|
||||||
// https://ec.europa.eu/taxation_customs/vies/faqvies.do#item_11
|
if (string.IsNullOrWhiteSpace(vatNumber))
|
||||||
// https://ec.europa.eu/taxation_customs/vies/
|
return false;
|
||||||
|
|
||||||
|
var sanitizedVat = SanitizeVatNumber(vatNumber);
|
||||||
|
|
||||||
//https://www.bolagsverket.se/apierochoppnadata.2531.html
|
return countryCode.ToUpperInvariant() switch
|
||||||
|
|
||||||
/// <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)
|
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(vatNumber))
|
"DK" => ValidateDkVat(sanitizedVat),
|
||||||
return false;
|
"NO" => ValidateNoOrg(sanitizedVat),
|
||||||
|
"SE" => ValidateSeOrg(sanitizedVat),
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
var sanitizedVat = SanitizeVatNumber(vatNumber);
|
/// <summary>
|
||||||
|
/// sanitize vat number
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="vatNumber"></param>
|
||||||
|
/// <returns>sanitized string</returns>
|
||||||
|
public static string SanitizeVatNumber(string vatNumber)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(vatNumber))
|
||||||
|
return "";
|
||||||
|
// remove anything but digits
|
||||||
|
var regexObj = new Regex(@"[^\d]");
|
||||||
|
return regexObj.Replace(vatNumber, "");
|
||||||
|
}
|
||||||
|
|
||||||
return countryCode.ToUpperInvariant() switch
|
private static bool ValidateDkVat(string vatNumber)
|
||||||
{
|
{
|
||||||
"DK" => ValidateDkVat(sanitizedVat),
|
// https://wiki.scn.sap.com/wiki/display/CRM/Denmark
|
||||||
"NO" => ValidateNoOrg(sanitizedVat),
|
// 8 digits 0 to 9
|
||||||
"SE" => ValidateSeOrg(sanitizedVat),
|
// C1..C7
|
||||||
_ => false
|
// C8 check-digit MOD11
|
||||||
};
|
// C1 > 0
|
||||||
}
|
// R = (2*C1 + 7*C2 + 6*C3 + 5*C4 + 4*C5 + 3*C6 + 2*C7 + C8)
|
||||||
|
if (vatNumber.Length == 8 && long.TryParse(vatNumber, out _))
|
||||||
|
return ValidateMod11(vatNumber);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
private static bool ValidateNoOrg(string vatNumber)
|
||||||
/// sanitize vat number
|
{
|
||||||
/// </summary>
|
// https://wiki.scn.sap.com/wiki/display/CRM/Norway
|
||||||
/// <param name="vatNumber"></param>
|
// 12 digits
|
||||||
/// <returns>sanitized string</returns>
|
// C1..C8 random 0 to 9
|
||||||
public static string SanitizeVatNumber(string vatNumber)
|
// C9 check-digit MOD11
|
||||||
|
// C10 C11 C12 chars == MVA
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(vatNumber))
|
if (vatNumber.Length == 9 && long.TryParse(vatNumber, out _))
|
||||||
return "";
|
return ValidateMod11(vatNumber);
|
||||||
// remove anything but digits
|
|
||||||
var regexObj = new Regex(@"[^\d]");
|
|
||||||
return regexObj.Replace(vatNumber, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool ValidateDkVat(string vatNumber)
|
|
||||||
{
|
|
||||||
// https://wiki.scn.sap.com/wiki/display/CRM/Denmark
|
|
||||||
// 8 digits 0 to 9
|
|
||||||
// C1..C7
|
|
||||||
// C8 check-digit MOD11
|
|
||||||
// C1 > 0
|
|
||||||
// R = (2*C1 + 7*C2 + 6*C3 + 5*C4 + 4*C5 + 3*C6 + 2*C7 + C8)
|
|
||||||
if(vatNumber.Length == 8 && long.TryParse(vatNumber, out _))
|
|
||||||
return ValidateMod11(vatNumber);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
private static bool ValidateNoOrg(string vatNumber)
|
|
||||||
{
|
{
|
||||||
// https://wiki.scn.sap.com/wiki/display/CRM/Norway
|
return false;
|
||||||
// 12 digits
|
|
||||||
// C1..C8 random 0 to 9
|
|
||||||
// C9 check-digit MOD11
|
|
||||||
// C10 C11 C12 chars == MVA
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (vatNumber.Length == 9 && long.TryParse(vatNumber, out _))
|
|
||||||
return ValidateMod11(vatNumber);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool ValidateSeOrg(string orgNumber)
|
|
||||||
{
|
|
||||||
// https://wiki.scn.sap.com/wiki/display/CRM/Sweden
|
|
||||||
// 12 digits 0 to 9
|
|
||||||
// C10 = (10 – (18 + 5 + 1 + 8 + 4)MOD10 10) MOD10
|
|
||||||
// R = S1 + S3 + S5 + S7 + S9
|
|
||||||
// Si = int(Ci/5) + (Ci*2)MOD10)
|
|
||||||
// https://www.skatteverket.se/skatter/mervardesskattmoms/momsregistreringsnummer.4.18e1b10334ebe8bc80002649.html
|
|
||||||
// EU MOMS => C11 C12 == 01 (De två sista siffrorna är alltid 01)
|
|
||||||
// C11 C12 is not used inside Sweden
|
|
||||||
// C1 is type of org and C2 to C9 is org number
|
|
||||||
// C10 is check digit
|
|
||||||
|
|
||||||
var orgToCheck = orgNumber;
|
|
||||||
if (long.Parse(orgToCheck) == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
switch (orgToCheck.Length)
|
|
||||||
{
|
|
||||||
// personal vat se
|
|
||||||
case 6:
|
|
||||||
return ValidateFormatSeExt(orgToCheck);
|
|
||||||
|
|
||||||
case < 10:
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// strip EU extension `01`
|
|
||||||
case 12:
|
|
||||||
orgNumber = orgNumber.Substring(0, 10);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
var c10 = C10(orgToCheck);
|
|
||||||
|
|
||||||
// compare calculated org number with incoming org number
|
|
||||||
return $"{orgToCheck.Substring(0, 9)}{c10}" == orgNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int C10(string vatToCheck)
|
|
||||||
{
|
|
||||||
// check digit calculation
|
|
||||||
var r = new[] { 0, 2, 4, 6, 8 }
|
|
||||||
.Sum(m => (int)char.GetNumericValue(vatToCheck[m]) / 5 +
|
|
||||||
(int)char.GetNumericValue(vatToCheck[m]) * 2 % 10);
|
|
||||||
var c1 = new[] { 1, 3, 5, 7 }.Sum(m => (int)char.GetNumericValue(vatToCheck[m]));
|
|
||||||
var c10 = (10 - (r + c1) % 10) % 10;
|
|
||||||
return c10;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool ValidateFormatSeExt(string ssn)
|
|
||||||
{
|
|
||||||
// Swedish personally held companies uses SSN number
|
|
||||||
// a relaxed validation is required as only first 6 digits is supplied
|
|
||||||
// birthday format e.g. 991231
|
|
||||||
|
|
||||||
if (ssn.Length is not 6 or 10 || int.Parse(ssn) == 0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var y = int.Parse(ssn.Substring(0,2));
|
|
||||||
var m = int.Parse(ssn.Substring(2,2));
|
|
||||||
var d = int.Parse(ssn.Substring(4,2));
|
|
||||||
// this calculation is only valid within 21st century
|
|
||||||
var leap = y % 4 == 0; // 2000 was a leap year;
|
|
||||||
// day
|
|
||||||
if(d is < 1 or > 31)
|
|
||||||
return false;
|
|
||||||
// month
|
|
||||||
switch (m)
|
|
||||||
{
|
|
||||||
// feb
|
|
||||||
case 2:
|
|
||||||
{
|
|
||||||
if (leap)
|
|
||||||
return d <= 29;
|
|
||||||
return d <= 28;
|
|
||||||
}
|
|
||||||
// apr, jun, sep, nov
|
|
||||||
case 4 or 6 or 9 or 11:
|
|
||||||
return d <= 30;
|
|
||||||
// jan, mar, may, july, aug, oct, dec
|
|
||||||
case 1 or 3 or 5 or 7 or 8 or 10 or 12:
|
|
||||||
return true;
|
|
||||||
// does not exist
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool ValidateMod11(string number)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (long.Parse(number) == 0)
|
|
||||||
return false;
|
|
||||||
var sum = 0;
|
|
||||||
for (int i = number.Length - 1, multiplier = 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
// Console.WriteLine($"char: {number[i]} multiplier: {multiplier}");
|
|
||||||
sum += (int)char.GetNumericValue(number[i]) * multiplier;
|
|
||||||
if (++multiplier > 7) multiplier = 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sum % 11 == 0;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
///// <summary>
|
|
||||||
/////
|
|
||||||
///// </summary>
|
|
||||||
///// <param name="ssn"></param>
|
|
||||||
///// <returns></returns>
|
|
||||||
//public static string FakeVatSsnSe(string ssn)
|
|
||||||
//{
|
|
||||||
// var fake = ssn.PadRight(9, '8');
|
|
||||||
// var c10 = SeGenerateCheckDigit(fake);
|
|
||||||
// return $"{fake}{c10}";
|
|
||||||
//}
|
|
||||||
|
|
||||||
///// <summary>
|
|
||||||
/////
|
|
||||||
///// </summary>
|
|
||||||
///// <param name="org"></param>
|
|
||||||
///// <returns></returns>
|
|
||||||
//public static bool OrgIsPrivate(string org)
|
|
||||||
//{
|
|
||||||
// var orgType = new List<string>() { };
|
|
||||||
|
|
||||||
// return ValidateFormatSeExt(org.Substring(0, 5));
|
|
||||||
//}
|
|
||||||
|
|
||||||
///// <summary>
|
|
||||||
/////
|
|
||||||
///// </summary>
|
|
||||||
///// <param name="vatNumber"></param>
|
|
||||||
///// <returns></returns>
|
|
||||||
//public static bool CheckLuhn(string vatNumber)
|
|
||||||
//{
|
|
||||||
// // https://www.geeksforgeeks.org/luhn-algorithm/
|
|
||||||
|
|
||||||
// var nDigits = vatNumber.Length;
|
|
||||||
// var nSum = 0;
|
|
||||||
// var isSecond = false;
|
|
||||||
// for (var i = nDigits - 1; i >= 0; i--)
|
|
||||||
// {
|
|
||||||
// var d = (int)char.GetNumericValue(vatNumber[i]) - '0';
|
|
||||||
// if (isSecond)
|
|
||||||
// d *= 2;
|
|
||||||
// // We add two digits to handle
|
|
||||||
// // cases that make two digits
|
|
||||||
// // after doubling
|
|
||||||
// nSum += d / 10;
|
|
||||||
// nSum += d % 10;
|
|
||||||
|
|
||||||
// isSecond = !isSecond;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nSum % 10 == 0;
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
//private static bool ValidateMod10(string number)
|
|
||||||
//{
|
|
||||||
// if (long.Parse(number) == 0)
|
|
||||||
// return false;
|
|
||||||
|
|
||||||
// var nDigits = number.Length;
|
|
||||||
// var nSum = 0;
|
|
||||||
// var isSecond = false;
|
|
||||||
// for (var i = nDigits - 1; i >= 0; i--)
|
|
||||||
// {
|
|
||||||
// var d = (int)char.GetNumericValue(number[i]) - '0';
|
|
||||||
// if (isSecond)
|
|
||||||
// d *= 2;
|
|
||||||
// nSum += d / 10;
|
|
||||||
// nSum += d % 10;
|
|
||||||
// isSecond = !isSecond;
|
|
||||||
// }
|
|
||||||
// return nSum % 10 == 0;
|
|
||||||
//}
|
|
||||||
|
|
||||||
//private static string GetMod10CheckDigit(string number)
|
|
||||||
//{
|
|
||||||
// var sum = 0;
|
|
||||||
// var alt = true;
|
|
||||||
// var digits = number.ToCharArray();
|
|
||||||
// for (var i = digits.Length - 1; i >= 0; i--)
|
|
||||||
// {
|
|
||||||
// var curDigit = digits[i] - 48;
|
|
||||||
// if (alt)
|
|
||||||
// {
|
|
||||||
// curDigit *= 2;
|
|
||||||
// if (curDigit > 9)
|
|
||||||
// curDigit -= 9;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// sum += curDigit;
|
|
||||||
// alt = !alt;
|
|
||||||
// }
|
|
||||||
// return sum % 10 == 0 ? "0" : (10 - sum % 10).ToString();
|
|
||||||
//}
|
|
||||||
|
|
||||||
//private string AddMod11CheckDigit(string number)
|
|
||||||
//{
|
|
||||||
// return number + GetMod11CheckDigit(number);
|
|
||||||
//}
|
|
||||||
|
|
||||||
//private static string GetMod11CheckDigit(string number)
|
|
||||||
//{
|
|
||||||
// var sum = 0;
|
|
||||||
// for (int i = number.Length - 1, multiplier = 2; i >= 0; i--)
|
|
||||||
// {
|
|
||||||
// sum += (int)char.GetNumericValue(number[i]) * multiplier;
|
|
||||||
// if (++multiplier > 7) multiplier = 2;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// var modulo = sum % 11;
|
|
||||||
// return modulo is 0 or 1 ? "0" : (11 - modulo).ToString();
|
|
||||||
//}
|
|
||||||
|
|
||||||
//private static bool CheckLuhn(string vatNumber)
|
|
||||||
//{
|
|
||||||
// // https://www.geeksforgeeks.org/luhn-algorithm/
|
|
||||||
|
|
||||||
// var nDigits = vatNumber.Length;
|
|
||||||
// var nSum = 0;
|
|
||||||
// var isSecond = false;
|
|
||||||
// for (var i = nDigits - 1; i >= 0; i--)
|
|
||||||
// {
|
|
||||||
// var d = (int)char.GetNumericValue(vatNumber[i]) - '0';
|
|
||||||
// if (isSecond)
|
|
||||||
// d *= 2;
|
|
||||||
// // We add two digits to handle
|
|
||||||
// // cases that make two digits
|
|
||||||
// // after doubling
|
|
||||||
// nSum += d / 10;
|
|
||||||
// nSum += d % 10;
|
|
||||||
|
|
||||||
// isSecond = !isSecond;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nSum % 10 == 0;
|
|
||||||
//}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool ValidateSeOrg(string orgNumber)
|
||||||
|
{
|
||||||
|
// https://wiki.scn.sap.com/wiki/display/CRM/Sweden
|
||||||
|
// 12 digits 0 to 9
|
||||||
|
// C10 = (10 – (18 + 5 + 1 + 8 + 4)MOD10 10) MOD10
|
||||||
|
// R = S1 + S3 + S5 + S7 + S9
|
||||||
|
// Si = int(Ci/5) + (Ci*2)MOD10)
|
||||||
|
// https://www.skatteverket.se/skatter/mervardesskattmoms/momsregistreringsnummer.4.18e1b10334ebe8bc80002649.html
|
||||||
|
// EU MOMS => C11 C12 == 01 (De två sista siffrorna är alltid 01)
|
||||||
|
// C11 C12 is not used inside Sweden
|
||||||
|
// C1 is type of org and C2 to C9 is org number
|
||||||
|
// C10 is check digit
|
||||||
|
|
||||||
|
var orgToCheck = orgNumber;
|
||||||
|
if (!long.TryParse(orgToCheck, out _))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
switch (orgToCheck.Length)
|
||||||
|
{
|
||||||
|
// personal vat se
|
||||||
|
case 6:
|
||||||
|
return ValidateFormatSeExt(orgToCheck);
|
||||||
|
|
||||||
|
case < 10:
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// strip EU extension `01`
|
||||||
|
case 12:
|
||||||
|
orgNumber = orgNumber.Substring(0, 10);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var c10 = C10(orgToCheck);
|
||||||
|
|
||||||
|
// compare calculated org number with incoming org number
|
||||||
|
return $"{orgToCheck.Substring(0, 9)}{c10}" == orgNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int C10(string vatToCheck)
|
||||||
|
{
|
||||||
|
// check digit calculation
|
||||||
|
var r = new[] { 0, 2, 4, 6, 8 }
|
||||||
|
.Sum(m => (int)char.GetNumericValue(vatToCheck[m]) / 5 +
|
||||||
|
(int)char.GetNumericValue(vatToCheck[m]) * 2 % 10);
|
||||||
|
var c1 = new[] { 1, 3, 5, 7 }.Sum(m => (int)char.GetNumericValue(vatToCheck[m]));
|
||||||
|
var c10 = (10 - (r + c1) % 10) % 10;
|
||||||
|
return c10;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool ValidateFormatSeExt(string ssn)
|
||||||
|
{
|
||||||
|
// Swedish personally held companies uses SSN number
|
||||||
|
// a relaxed validation is required as only first 6 digits is supplied
|
||||||
|
// birthday format e.g. 991231
|
||||||
|
|
||||||
|
if (ssn.Length is not 6 or 10 || int.Parse(ssn) == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var y = int.Parse(ssn.Substring(0, 2));
|
||||||
|
var m = int.Parse(ssn.Substring(2, 2));
|
||||||
|
var d = int.Parse(ssn.Substring(4, 2));
|
||||||
|
// this calculation is only valid within 21st century
|
||||||
|
var leap = y % 4 == 0; // 2000 was a leap year;
|
||||||
|
// day
|
||||||
|
if (d is < 1 or > 31)
|
||||||
|
return false;
|
||||||
|
// month
|
||||||
|
switch (m)
|
||||||
|
{
|
||||||
|
// feb
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
if (leap)
|
||||||
|
return d <= 29;
|
||||||
|
return d <= 28;
|
||||||
|
}
|
||||||
|
// apr, jun, sep, nov
|
||||||
|
case 4 or 6 or 9 or 11:
|
||||||
|
return d <= 30;
|
||||||
|
// jan, mar, may, july, aug, oct, dec
|
||||||
|
case 1 or 3 or 5 or 7 or 8 or 10 or 12:
|
||||||
|
return true;
|
||||||
|
// does not exist
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool ValidateMod11(string number)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (long.Parse(number) == 0)
|
||||||
|
return false;
|
||||||
|
var sum = 0;
|
||||||
|
for (int i = number.Length - 1, multiplier = 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
// Console.WriteLine($"char: {number[i]} multiplier: {multiplier}");
|
||||||
|
sum += (int)char.GetNumericValue(number[i]) * multiplier;
|
||||||
|
if (++multiplier > 7) multiplier = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum % 11 == 0;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///// <summary>
|
||||||
|
/////
|
||||||
|
///// </summary>
|
||||||
|
///// <param name="ssn"></param>
|
||||||
|
///// <returns></returns>
|
||||||
|
//public static string FakeVatSsnSe(string ssn)
|
||||||
|
//{
|
||||||
|
// var fake = ssn.PadRight(9, '8');
|
||||||
|
// var c10 = SeGenerateCheckDigit(fake);
|
||||||
|
// return $"{fake}{c10}";
|
||||||
|
//}
|
||||||
|
|
||||||
|
///// <summary>
|
||||||
|
/////
|
||||||
|
///// </summary>
|
||||||
|
///// <param name="org"></param>
|
||||||
|
///// <returns></returns>
|
||||||
|
//public static bool OrgIsPrivate(string org)
|
||||||
|
//{
|
||||||
|
// var orgType = new List<string>() { };
|
||||||
|
|
||||||
|
// return ValidateFormatSeExt(org.Substring(0, 5));
|
||||||
|
//}
|
||||||
|
|
||||||
|
///// <summary>
|
||||||
|
/////
|
||||||
|
///// </summary>
|
||||||
|
///// <param name="vatNumber"></param>
|
||||||
|
///// <returns></returns>
|
||||||
|
//public static bool CheckLuhn(string vatNumber)
|
||||||
|
//{
|
||||||
|
// // https://www.geeksforgeeks.org/luhn-algorithm/
|
||||||
|
|
||||||
|
// var nDigits = vatNumber.Length;
|
||||||
|
// var nSum = 0;
|
||||||
|
// var isSecond = false;
|
||||||
|
// for (var i = nDigits - 1; i >= 0; i--)
|
||||||
|
// {
|
||||||
|
// var d = (int)char.GetNumericValue(vatNumber[i]) - '0';
|
||||||
|
// if (isSecond)
|
||||||
|
// d *= 2;
|
||||||
|
// // We add two digits to handle
|
||||||
|
// // cases that make two digits
|
||||||
|
// // after doubling
|
||||||
|
// nSum += d / 10;
|
||||||
|
// nSum += d % 10;
|
||||||
|
|
||||||
|
// isSecond = !isSecond;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return nSum % 10 == 0;
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
//private static bool ValidateMod10(string number)
|
||||||
|
//{
|
||||||
|
// if (long.Parse(number) == 0)
|
||||||
|
// return false;
|
||||||
|
|
||||||
|
// var nDigits = number.Length;
|
||||||
|
// var nSum = 0;
|
||||||
|
// var isSecond = false;
|
||||||
|
// for (var i = nDigits - 1; i >= 0; i--)
|
||||||
|
// {
|
||||||
|
// var d = (int)char.GetNumericValue(number[i]) - '0';
|
||||||
|
// if (isSecond)
|
||||||
|
// d *= 2;
|
||||||
|
// nSum += d / 10;
|
||||||
|
// nSum += d % 10;
|
||||||
|
// isSecond = !isSecond;
|
||||||
|
// }
|
||||||
|
// return nSum % 10 == 0;
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private static string GetMod10CheckDigit(string number)
|
||||||
|
//{
|
||||||
|
// var sum = 0;
|
||||||
|
// var alt = true;
|
||||||
|
// var digits = number.ToCharArray();
|
||||||
|
// for (var i = digits.Length - 1; i >= 0; i--)
|
||||||
|
// {
|
||||||
|
// var curDigit = digits[i] - 48;
|
||||||
|
// if (alt)
|
||||||
|
// {
|
||||||
|
// curDigit *= 2;
|
||||||
|
// if (curDigit > 9)
|
||||||
|
// curDigit -= 9;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// sum += curDigit;
|
||||||
|
// alt = !alt;
|
||||||
|
// }
|
||||||
|
// return sum % 10 == 0 ? "0" : (10 - sum % 10).ToString();
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private string AddMod11CheckDigit(string number)
|
||||||
|
//{
|
||||||
|
// return number + GetMod11CheckDigit(number);
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private static string GetMod11CheckDigit(string number)
|
||||||
|
//{
|
||||||
|
// var sum = 0;
|
||||||
|
// for (int i = number.Length - 1, multiplier = 2; i >= 0; i--)
|
||||||
|
// {
|
||||||
|
// sum += (int)char.GetNumericValue(number[i]) * multiplier;
|
||||||
|
// if (++multiplier > 7) multiplier = 2;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// var modulo = sum % 11;
|
||||||
|
// return modulo is 0 or 1 ? "0" : (11 - modulo).ToString();
|
||||||
|
//}
|
||||||
|
|
||||||
|
//private static bool CheckLuhn(string vatNumber)
|
||||||
|
//{
|
||||||
|
// // https://www.geeksforgeeks.org/luhn-algorithm/
|
||||||
|
|
||||||
|
// var nDigits = vatNumber.Length;
|
||||||
|
// var nSum = 0;
|
||||||
|
// var isSecond = false;
|
||||||
|
// for (var i = nDigits - 1; i >= 0; i--)
|
||||||
|
// {
|
||||||
|
// var d = (int)char.GetNumericValue(vatNumber[i]) - '0';
|
||||||
|
// if (isSecond)
|
||||||
|
// d *= 2;
|
||||||
|
// // We add two digits to handle
|
||||||
|
// // cases that make two digits
|
||||||
|
// // after doubling
|
||||||
|
// nSum += d / 10;
|
||||||
|
// nSum += d % 10;
|
||||||
|
|
||||||
|
// isSecond = !isSecond;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return nSum % 10 == 0;
|
||||||
|
//}
|
||||||
}
|
}
|
Loading…
Reference in a new issue