Manipulating and displaying in C#
Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities.
What Is Humanizer?
Humanizer is a library that helps you get human-friendly string and human string to your code
The word "Humanize" evokes, for me, a feeling of making something simpler, more accessible, more useful, more fluent, more flexible.
This below is features of Humanizer
1. Transform 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"
// acronyms are left intact
"HTML".Humanize() => "HTML"
"Can_return_title_Case".Humanize(LetterCasing.Title) => "Can Return Title Case"
"CanReturnLowerCase".Humanize(LetterCasing.LowerCase) => "can return lower case"
And transform string
"some title".Pascalize(); //SomeTitle
"some title".Camelize(); //someTitle
"some title".Underscore(); //some_title
"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
2. Truncate string
"Long text to truncate".Truncate(10, Truncator.FixedLength); //Long text…
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords); //Long text…
3. Make enum readable
public enum EnumUnderTest
{
[Description(Custom description)]
MemberWithDescriptionAttribute,
MemberWithoutDescriptionAttribute,
}
//Custom description
EnumUnderTest.MemberWithDescriptionAttribute.Humanize();
//Member without description attribute
EnumUnderTest.MemberWithoutDescriptionAttribute.Humanize();
4. DateTime to text
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); //Set language to English
DateTime.UtcNow.AddHours(-30).Humanize(); // Yesterday
DateTime.UtcNow.AddHours(-60).Humanize(); // 2 days ago
DateTime.UtcNow.AddHours(2).Humanize(); // In 2 hours
DateTime.UtcNow.AddDays(1).Humanize(); // Tomorrow
Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN"); //Set language to Vietnamese
DateTime.UtcNow.AddHours(-30).Humanize(); // Hôm qua
DateTime.UtcNow.AddHours(-60).Humanize(); // Cách đây 2 ngày
DateTime.UtcNow.AddHours(2).Humanize(); // 2 giờ nữa
DateTime.UtcNow.AddDays(1).Humanize(); // Ngày mai
5. TimeSpan to text
TimeSpan.FromMilliseconds(1).Humanize() => "1 millisecond"
TimeSpan.FromMilliseconds(2).Humanize() => "2 milliseconds"
TimeSpan.FromDays(1).Humanize() => "1 day"
TimeSpan.FromDays(16).Humanize() => "2 weeks"
TimeSpan.FromDays(1).Humanize(precision:2) => "1 day" // no difference when there is only one unit in the provided TimeSpan
TimeSpan.FromDays(16).Humanize(2) => "2 weeks, 2 days"
// the same TimeSpan value with different precision returns different results
TimeSpan.FromMilliseconds(1299630020).Humanize() => "2 weeks"
TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"
TimeSpan.FromMilliseconds(1299630020).Humanize(4) => "2 weeks, 1 day, 1 hour, 30 seconds"
TimeSpan.FromMilliseconds(1299630020).Humanize(5) => "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds"
6. Number to text
1.ToWords(); //one
10.ToWords(); //ten
11.ToWords(); //eleven
122.ToWords(); //one hundred and twenty-two
3501.ToWords(); //three thousand five hundred and one
7. File size to text
var fileSize = (10).Kilobytes();
fileSize.Bits = 81920
fileSize.Bytes = 10240
fileSize.Kilobytes = 10
// Calculate file size
var total = (10).Gigabytes() + (512).Megabytes() - (2.5).Gigabytes();
// Transform to text
7.Bits().ToString(); // 7 b
8.Bits().ToString(); // 1 B
(.5).Kilobytes().Humanize(); // 512 B
(1000).Kilobytes().ToString(); // 1000 KB
(1024).Kilobytes().Humanize(); // 1 MB
// Text to file size (ignore case)
ByteSize.Parse("1.55 mB");
ByteSize.Parse("1.55 mb");
ByteSize.Parse("1.55 GB");
ByteSize.Parse("1.55 gB");
ByteSize.Parse("1.55 gb");
ByteSize.Parse("1.55 TB");
8. Quantity (fantastic)
"men".ToQuantity(2) => "2 men"
"process".ToQuantity(2) => "2 processes"
"process".ToQuantity(1) => "1 process"
"processes".ToQuantity(1) => "1 process"
"case".ToQuantity(0) => "0 cases"
"case".ToQuantity(1) => "1 case"
Even as words!
"case".ToQuantity(5, ShowQuantityAs.Words) => "five cases"
And quantity to text
3501.ToWords() => "three thousand five hundred and one"
121.ToOrdinalWords() => "hundred and twenty first"
8.ToRoman() => "VIII"
It's so fantastic and beautiful.
An Important NuGet Note
PM> Install-Package Humanizer
Summary
I'm just scratching the surface of Humanizer, going through it's Getting Started. There are also samples where you can plug it into your own frameworks or deep within ASP.NET MVC and humanize enums, type names, and properties as a way to keep your code DRY.