在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Humanizr/Humanizer开源软件地址(OpenSource Url):https://github.com/Humanizr/Humanizer开源编程语言(OpenSource Language):C# 100.0%开源软件介绍(OpenSource Introduction):Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities. It is part of the .NET Foundation, and operates under their code of conduct. It is licensed under the MIT (an OSI approved license). Table of contents
InstallYou can install Humanizer as a nuget package: English only: All languages: Humanizer is a .NET Standard Class Library with support for .NET Standard 1.0+ (.Net 4.5+, UWP, Xamarin, and .NET Core). Also Humanizer symbols are source indexed with SourceLink and are included in the package so you can step through Humanizer code while debugging your code. For pre-release builds, Azure Artifacts feed is available where you can pull down CI packages from the latest codebase. The feed URL is: Specify Languages (Optional)New in Humanizer 2.0 is the option to choose which localization packages you wish to use. You choose which packages
based on what NuGet package(s) you install. By default, the main Here are the options:
The detailed explanation for how this works is in the comments here. FeaturesHumanize String
"PascalCaseInputStringIsTurnedIntoSentence".Humanize() => "Pascal case input string is turned into sentence"
"Underscored_input_string_is_turned_into_sentence".Humanize() => "Underscored input string is turned into sentence"
"Underscored_input_String_is_turned_INTO_sentence".Humanize() => "Underscored input String is turned INTO sentence" Note that a string that contains only upper case letters, and consists only of one word, is always treated as an acronym (regardless of its length). To guarantee that any arbitrary string will always be humanized you must use a transform (see // acronyms are left intact
"HTML".Humanize() => "HTML"
// any unbroken upper case string is treated as an acronym
"HUMANIZER".Humanize() => "HUMANIZER"
"HUMANIZER".Transform(To.LowerCase, To.TitleCase) => "Humanizer" You may also specify the desired letter casing: "CanReturnTitleCase".Humanize(LetterCasing.Title) => "Can Return Title Case"
"Can_return_title_Case".Humanize(LetterCasing.Title) => "Can Return Title Case"
"CanReturnLowerCase".Humanize(LetterCasing.LowerCase) => "can return lower case"
"CanHumanizeIntoUpperCase".Humanize(LetterCasing.AllCaps) => "CAN HUMANIZE INTO UPPER CASE"
Dehumanize StringMuch like you can humanize a computer friendly into human friendly string you can dehumanize a human friendly string into a computer friendly one: "Pascal case input string is turned into sentence".Dehumanize() => "PascalCaseInputStringIsTurnedIntoSentence" Transform StringThere is a string Transform(this string input, params IStringTransformer[] transformers) And there are some out of the box implementations of "Sentence casing".Transform(To.LowerCase) => "sentence casing"
"Sentence casing".Transform(To.SentenceCase) => "Sentence casing"
"Sentence casing".Transform(To.TitleCase) => "Sentence Casing"
"Sentence casing".Transform(To.UpperCase) => "SENTENCE CASING"
The benefit of using Truncate StringYou can truncate a "Long text to truncate".Truncate(10) => "Long text…" By default the "Long text to truncate".Truncate(10, "---") => "Long te---" The default truncation strategy, "Long text to truncate".Truncate(10, Truncator.FixedLength) => "Long text…"
"Long text to truncate".Truncate(10, "---", Truncator.FixedLength) => "Long te---"
"Long text to truncate".Truncate(6, Truncator.FixedNumberOfCharacters) => "Long t…"
"Long text to truncate".Truncate(6, "---", Truncator.FixedNumberOfCharacters) => "Lon---"
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords) => "Long text…"
"Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords) => "Long text---" Note that you can also use create your own truncator by implementing the There is also an option to choose whether to truncate the string from the beginning ( "Long text to truncate".Truncate(10, Truncator.FixedLength, TruncateFrom.Left) => "… truncate"
"Long text to truncate".Truncate(10, "---", Truncator.FixedLength, TruncateFrom.Left) => "---runcate"
"Long text to truncate".Truncate(10, Truncator.FixedNumberOfCharacters, TruncateFrom.Left) => "…o truncate"
"Long text to truncate".Truncate(16, "---", Truncator.FixedNumberOfCharacters, TruncateFrom.Left) => "---ext to truncate"
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords, TruncateFrom.Left) => "…to truncate"
"Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords, TruncateFrom.Left) => "---to truncate" Format StringYou can format a "To be formatted -> {0}/{1}.".FormatWith(1, "A") => "To be formatted -> 1/A." This is an extension method based on You also can specify the culture to use explicitly as the first parameter for the "{0:N2}".FormatWith(new CultureInfo("ru-RU"), 6666.66) => "6 666,66" If a culture is not specified, current thread's current culture is used. Humanize EnumsCalling public enum EnumUnderTest
{
[Description("Custom description")]
MemberWithDescriptionAttribute,
MemberWithoutDescriptionAttribute,
ALLCAPITALS
} You will get: // DescriptionAttribute is honored
EnumUnderTest.MemberWithDescriptionAttribute.Humanize() => "Custom description"
// In the absence of Description attribute string.Humanizer kicks in
EnumUnderTest.MemberWithoutDescriptionAttribute.Humanize() => "Member without description attribute"
// Of course you can still apply letter casing
EnumUnderTest.MemberWithoutDescriptionAttribute.Humanize().Transform(To.TitleCase) => "Member Without Description Attribute" You are not limited to You can even configure the name of the property of attibute to use as description.
If you need to provide localised descriptions you can use public enum EnumUnderTest
{
[Display(Description = "EnumUnderTest_Member", ResourceType = typeof(Project.Resources))]
Member
} You will get: EnumUnderTest.Member.Humanize() => "content" // from Project.Resources found under "EnumUnderTest_Member" resource key Hopefully this will help avoid littering enums with unnecessary attributes! Dehumanize EnumsDehumanizes a string into the Enum it was originally Humanized from! The API looks like: public static TTargetEnum DehumanizeTo<TTargetEnum>(this string input) And the usage is: "Member without description attribute".DehumanizeTo<EnumUnderTest>() => EnumUnderTest.MemberWithoutDescriptionAttribute And just like the Humanize API it honors the There is also a non-generic counterpart for when the original Enum is not known at compile time: public static Enum DehumanizeTo(this string input, Type targetEnum, NoMatch onNoMatch = NoMatch.ThrowsException) which can be used like: "Member without description attribute".DehumanizeTo(typeof(EnumUnderTest)) => EnumUnderTest.MemberWithoutDescriptionAttribute By default both methods throw a Humanize DateTimeYou can DateTime.UtcNow.AddHours(-30).Humanize() => "yesterday"
DateTime.UtcNow.AddHours(-2).Humanize() => "2 hours ago"
DateTime.UtcNow.AddHours(30).Humanize() => "tomorrow"
DateTime.UtcNow.AddHours(2).Humanize() => "2 hours from now"
DateTimeOffset.UtcNow.AddHours(1).Humanize() => "an hour from now" Humanizer supports both local and UTC dates as well as dates with offset ( public static string Humanize(this DateTime input, bool utcDate = true, DateTime? dateToCompareAgainst = null, CultureInfo culture = null)
public static string Humanize(this DateTimeOffset input, DateTimeOffset? dateToCompareAgainst = null, CultureInfo culture = null) Many localizations are available for this method. Here is a few examples: // In ar culture
DateTime.UtcNow.AddDays(-1).Humanize() => "أمس"
DateTime.UtcNow.AddDays(-2).Humanize() => "منذ يومين"
DateTime.UtcNow.AddDays(-3).Humanize() => "منذ 3 أيام"
DateTime.UtcNow.AddDays(-11).Humanize() => "منذ 11 يوم"
// In ru-RU culture
DateTime.UtcNow.AddMinutes(-1).Humanize() => "минуту назад"
DateTime.UtcNow.AddMinutes(-2).Humanize() => "2 минуты назад"
DateTime.UtcNow.AddMinutes(-10).Humanize() => "10 минут назад"
DateTime.UtcNow.AddMinutes(-21).Humanize() => "21 минуту назад"
DateTime.UtcNow.AddMinutes(-22).Humanize() => "22 минуты назад"
DateTime.UtcNow.AddMinutes(-40).Humanize() => "40 минут назад" There are two strategies for Configurator.DateTimeHumanizeStrategy = new PrecisionDateTimeHumanizeStrategy(precision: .75);
Configurator.DateTimeOffsetHumanizeStrategy = new PrecisionDateTimeOffsetHumanizeStrategy(precision: .75); // configure when humanizing DateTimeOffset The default precision is set to .75 but you can pass your desired precision too. With precision set to 0.75: 44 seconds => 44 seconds ago/from now
45 seconds => one minute ago/from now
104 seconds => one minute ago/from now
105 seconds => two minutes ago/from now
25 days => a month ago/from now No dehumanization for dates as Humanize TimeSpanYou can call TimeSpan.FromMilliseconds(1).Humanize() => "1 millisecond"
TimeSpan.FromMilliseconds(2).Humanize() => "2 milliseconds"
TimeSpan.FromDays(1).Humanize() => "1 day"
TimeSpan.FromDays(16).Humanize() => "2 weeks" There is an optional TimeSpan.FromDays(1).Humanize(
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论