mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-06-20 01:28:15 +08:00
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
943d151048 | |||
09352ac5ea |
@ -1,4 +1,8 @@
|
||||
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve">
|
||||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/ReSpeller/ReSpellerEnabled/@EntryValue">False</s:Boolean>
|
||||
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
|
||||
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
|
||||
|
38
README.md
38
README.md
@ -1,27 +1,27 @@
|
||||
# ns-ext
|
||||
|
||||
[中](README.zh-CN.md) | **En**
|
||||
The **ns-ext** is a .NET extension function library, containing the following types of extension modules:
|
||||
|
||||
|
||||
| Features | File name |
|
||||
| -------- | ---------------------------------- |
|
||||
| Byte type extension | ByteExtensions.cs |
|
||||
| Character Type Extensions | CharExtensions.cs |
|
||||
| Date Type Extensions | DateTimeExtensions.cs |
|
||||
| Database command type extension | DbCommandExtensions.cs |
|
||||
| Decimal Number Type extension | DecimalExtensions.cs |
|
||||
| Enumable type extension | EnumerableExtensions.cs |
|
||||
| Enumeration type extension | EnumExtensions.cs |
|
||||
| General type extension | GenericExtensions.cs |
|
||||
| Integer type extension | IntExtensions.cs |
|
||||
| Features | File name |
|
||||
|------------------------------------------|------------------------------------|
|
||||
| Byte type extension | ByteExtensions.cs |
|
||||
| Character Type Extensions | CharExtensions.cs |
|
||||
| Date Type Extensions | DateTimeExtensions.cs |
|
||||
| Database command type extension | DbCommandExtensions.cs |
|
||||
| Decimal Number Type extension | DecimalExtensions.cs |
|
||||
| Enumable type extension | EnumerableExtensions.cs |
|
||||
| Enumeration type extension | EnumExtensions.cs |
|
||||
| General type extension | GenericExtensions.cs |
|
||||
| Integer type extension | IntExtensions.cs |
|
||||
| Json Serialization option type extension | JsonSerializerOptionsExtensions.cs |
|
||||
| Log type extension | LoggerExtensions.cs |
|
||||
| Long integer extension | LongExtensions.cs |
|
||||
| Object type extension | ObjectExtensions.cs |
|
||||
| Stream type extension | StreamExtensions.cs |
|
||||
| String type extension | StringExtensions.cs |
|
||||
| Prototype type extension | TypeExtensions.cs |
|
||||
| Resource locator type extension | UriExtensions.cs |
|
||||
| Log type extension | LoggerExtensions.cs |
|
||||
| Long integer extension | LongExtensions.cs |
|
||||
| Object type extension | ObjectExtensions.cs |
|
||||
| Stream type extension | StreamExtensions.cs |
|
||||
| String type extension | StringExtensions.cs |
|
||||
| Prototype type extension | TypeExtensions.cs |
|
||||
| Resource locator type extension | UriExtensions.cs |
|
||||
|
||||
## Quick start
|
||||
|
||||
|
@ -1,13 +0,0 @@
|
||||
namespace NSExt.Constant;
|
||||
#pragma warning disable SYSLIB1045
|
||||
|
||||
// 使用 RegexGenerator 新特性会生成重复key值的xmlcomment导致出错
|
||||
internal static class Regexes
|
||||
{
|
||||
public static readonly Regex RegexHtmlTag = new("<[^>]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public static readonly Regex RegexMobile
|
||||
= new("^(\\d{3})\\d{4}(\\d{4})$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
public static readonly Regex RegexUpLetter = new("([A-Z])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
}
|
@ -42,17 +42,20 @@ public static class ByteExtensions
|
||||
/// <param name="me">me</param>
|
||||
/// <param name="upperCase">是否大写</param>
|
||||
/// <param name="splitShar">字节间分隔符</param>
|
||||
public static string String(this byte[] me, bool upperCase = true, string splitShar = null)
|
||||
/// <param name="splitInterval">分隔符跳跃字节数</param>
|
||||
public static string String(this IEnumerable<byte> me, bool upperCase = true, string splitShar = ""
|
||||
, int splitInterval = 1)
|
||||
{
|
||||
var ret = BitConverter.ToString(me);
|
||||
if (!upperCase) {
|
||||
ret = ret.ToLower(CultureInfo.InvariantCulture);
|
||||
var sb = new StringBuilder();
|
||||
var i = 0;
|
||||
foreach (var c in me.Select(x => x.ToString(upperCase ? "X2" : "x2", CultureInfo.InvariantCulture))) {
|
||||
if (i++ % splitInterval == 0) {
|
||||
sb.Append(splitShar);
|
||||
}
|
||||
|
||||
sb.Append(c);
|
||||
}
|
||||
|
||||
if (splitShar != "-") {
|
||||
ret = ret.Replace("-", splitShar ?? string.Empty);
|
||||
}
|
||||
|
||||
return ret;
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
namespace NSExt.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// DbCommandExtensions
|
||||
/// DbCommandExtensions
|
||||
/// </summary>
|
||||
public static class DbCommandExtensions
|
||||
{
|
||||
@ -15,18 +15,30 @@ public static class DbCommandExtensions
|
||||
|
||||
//应逆向替换,否则由于 多个表的过滤器问题导致替换不完整 如 @TenantId1 @TenantId10
|
||||
for (var i = me.Parameters.Count - 1; i >= 0; i--) {
|
||||
#pragma warning disable IDE0072
|
||||
sql = me.Parameters[i].DbType switch {
|
||||
#pragma warning restore IDE0072
|
||||
DbType.String or DbType.DateTime or DbType.Date or DbType.Time or DbType.DateTime2
|
||||
or DbType.DateTimeOffset or DbType.Guid or DbType.VarNumeric or DbType.AnsiStringFixedLength
|
||||
or DbType.AnsiString
|
||||
or DbType.StringFixedLength =>
|
||||
sql.Replace(me.Parameters[i].ParameterName, "'" + me.Parameters[i].Value + "'")
|
||||
, DbType.Boolean => sql.Replace(//
|
||||
me.Parameters[i].ParameterName
|
||||
, Convert.ToBoolean(me.Parameters[i].Value, CultureInfo.InvariantCulture) ? "1" : "0")
|
||||
, _ => sql.Replace(me.Parameters[i].ParameterName, me.Parameters[i].Value?.ToString())
|
||||
or DbType.AnsiString or DbType.StringFixedLength => sql.Replace( //
|
||||
me.Parameters[i].ParameterName, "'" + me.Parameters[i].Value + "'")
|
||||
, DbType.Boolean => sql.Replace( //
|
||||
me.Parameters[i].ParameterName
|
||||
, Convert.ToBoolean(me.Parameters[i].Value, CultureInfo.InvariantCulture) ? "1" : "0")
|
||||
, DbType.Binary => throw new NotImplementedException()
|
||||
, DbType.Byte => throw new NotImplementedException()
|
||||
, DbType.Currency => throw new NotImplementedException()
|
||||
, DbType.Decimal => throw new NotImplementedException()
|
||||
, DbType.Double => throw new NotImplementedException()
|
||||
, DbType.Int16 => throw new NotImplementedException()
|
||||
, DbType.Int32 => throw new NotImplementedException()
|
||||
, DbType.Int64 => throw new NotImplementedException()
|
||||
, DbType.Object => throw new NotImplementedException()
|
||||
, DbType.SByte => throw new NotImplementedException()
|
||||
, DbType.Single => throw new NotImplementedException()
|
||||
, DbType.UInt16 => throw new NotImplementedException()
|
||||
, DbType.UInt32 => throw new NotImplementedException()
|
||||
, DbType.UInt64 => throw new NotImplementedException()
|
||||
, DbType.Xml => throw new NotImplementedException()
|
||||
, _ => sql.Replace(me.Parameters[i].ParameterName, me.Parameters[i].Value?.ToString())
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -15,12 +15,14 @@ public static class JsonSerializerOptionsExtensions
|
||||
public static JsonSerializerOptions NewJsonSerializerOptions(this JsonSerializerOptions _)
|
||||
{
|
||||
return new JsonSerializerOptions {
|
||||
ReadCommentHandling = JsonCommentHandling.Skip
|
||||
, AllowTrailingCommas = true
|
||||
, DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
|
||||
, PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
, NumberHandling = JsonNumberHandling.AllowReadingFromString
|
||||
ReadCommentHandling = JsonCommentHandling.Skip
|
||||
, AllowTrailingCommas = true
|
||||
, DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
|
||||
, PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
, NumberHandling
|
||||
= JsonNumberHandling.AllowReadingFromString |
|
||||
JsonNumberHandling.WriteAsString
|
||||
, PropertyNameCaseInsensitive = true
|
||||
};
|
||||
}
|
||||
|
@ -4,14 +4,13 @@
|
||||
#pragma warning disable CA1720
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
using NSExt.Constant;
|
||||
|
||||
namespace NSExt.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// StringExtensions
|
||||
/// </summary>
|
||||
public static class StringExtensions
|
||||
public static partial class StringExtensions
|
||||
{
|
||||
private static readonly JsonSerializerOptions _defaultJsonSerializerOptions
|
||||
= default(JsonSerializerOptions).NewJsonSerializerOptions();
|
||||
@ -370,7 +369,7 @@ public static class StringExtensions
|
||||
/// <returns>掩码后的手机号</returns>
|
||||
public static string MaskMobile(this string me)
|
||||
{
|
||||
return Regexes.RegexMobile.Replace(me, "$1****$2");
|
||||
return RegexMobile().Replace(me, "$1****$2");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -445,7 +444,7 @@ public static class StringExtensions
|
||||
/// <returns>处理之后的字符串</returns>
|
||||
public static string RemoveHtmlTag(this string me)
|
||||
{
|
||||
return Regexes.RegexHtmlTag.Replace(me, string.Empty);
|
||||
return RegexHtmlTag().Replace(me, string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -474,7 +473,7 @@ public static class StringExtensions
|
||||
/// </summary>
|
||||
public static string Snakecase(this string me)
|
||||
{
|
||||
return Regexes.RegexUpLetter.Replace(me, "-$1").ToLower(CultureInfo.InvariantCulture).TrimStart('-');
|
||||
return RegexUpperCaseLetter().Replace(me, "-$1").ToLower(CultureInfo.InvariantCulture).TrimStart('-');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -508,6 +507,16 @@ public static class StringExtensions
|
||||
return ret == me ? ret : ret.TrimSpaces();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将\ux0000 、 %u0000 、 &#x0000; 编码转换成可读字符串
|
||||
/// </summary>
|
||||
public static string UnicodeDe(this string me)
|
||||
{
|
||||
const string replacement = "&#x$1;";
|
||||
return me.Contains(@"\u") ? RegexBacksLantUnicode().Replace(me, replacement).HtmlDe() :
|
||||
me.Contains(@"%u") ? RegexPercentUnicode().Replace(me, replacement).HtmlDe() : me.HtmlDe();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// url编码
|
||||
/// </summary>
|
||||
@ -542,4 +551,19 @@ public static class StringExtensions
|
||||
.Replace("-", string.Empty)
|
||||
.ToLower(CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
[GeneratedRegex("\\\\u([a-fA-F0-9]{4})")]
|
||||
private static partial Regex RegexBacksLantUnicode();
|
||||
|
||||
[GeneratedRegex("<[^>]*>")]
|
||||
private static partial Regex RegexHtmlTag();
|
||||
|
||||
[GeneratedRegex("^(\\d{3})\\d{4}(\\d{4})$")]
|
||||
private static partial Regex RegexMobile();
|
||||
|
||||
[GeneratedRegex("\\\\u([a-fA-F0-9]{4})")]
|
||||
private static partial Regex RegexPercentUnicode();
|
||||
|
||||
[GeneratedRegex("([A-Z])")]
|
||||
private static partial Regex RegexUpperCaseLetter();
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<TargetFrameworks>net7.0</TargetFrameworks>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0"/>
|
||||
<PackageReference Include="MinVer" Version="4.3.0-beta.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<Import Project="../CodeQuality.props" />
|
||||
<Import Project="../CodeQuality.props"/>
|
||||
</Project>
|
Reference in New Issue
Block a user