Humanizer — an Exciting Library in C#
This time, I will introduce you to a pretty “cool” library. This library is called Humanizer. It has only one function: Convert strings, dates, … into words that humans can read (As the name of Humanizer means “personification”). It sounds simple, but you will be amazed at its functions.
The article only showcases, introductory, so there won’t be much code. If you are curious, you can create a new project, use the Nuget to install the Humanizer and test code. Some salient features of Humanizer.
Convert function names and variable names to meaningful strings
In programming, we often name the field, function name, variable name according to a naming convention. Humanizer allows turning these names into a meaningful 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
It is possible to transform a string into uppercase, lowercase, uppercase, first letter:
"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
Collapse the string
To display long information, we usually write a function to cut the string, then end with a … Humanizer has a built-in function that supports this. We can also cut by the number of characters or words
"Long text to truncate".Truncate(10, Truncator.FixedLength);
//Cut 10 characters
//Long text…"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords); //Cut 2 characters
//Long text…
Convert enum into a meaningful string
With some long enum, we often use Annotation to convert it into a meaningful string. Humanizer handles this very easily.
public enum EnumUnderTest
{
[Description (Custom description)]
MemberWithDescriptionAttribute,
MemberWithoutDescriptionAttribute,
}
// If DescriptionAttribute, read the word Attribute
EnumUnderTest.MemberWithDescriptionAttribute.Humanize (); // Custom description// If not, convert enum to string
EnumUnderTest.MemberWithoutDescriptionAttribute.Humanize (); // Member without description attribute
Handling DateTime
You want to do the function of calculating the time of posting comments like Facebook (1 hour ago, 2 hours ago, etc.).
Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-VN"); // Set Vietnamese language
DateTime.UtcNow.AddHours (-30) .Humanize (); // "yesterday
DateTime.UtcNow.AddHours (-60) .Humanize (); //"2 days ago
DateTime.UtcNow.AddHours (2) .Humanize (); // 2 hours
DateTime.UtcNow.AddDays (1) .Humanize (); //Tomorrow
Handling TimeSpan
TimeSpan processing function is similar, both English and Vietnamese run very well.
TimeSpan.FromMilliseconds(2).Humanize(); //2 milliseconds
TimeSpan.FromDays(1).Humanize(); //1 day
TimeSpan.FromDays(16).Humanize(); //2 weeks
// By default, the Humanize () function will give the largest unit (year-> month -> ...).// We pass more parameters to display the smaller units
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
Convert numbers into words (English only)
1.ToWords(); //one10.ToWords(); //ten11.ToWords(); //eleven122.ToWords(); //one hundred and twenty-two3501.ToWords(); //three thousand five hundred and one
Processing file size (Pretty good)
Humanizer allows processing data related to filesize (KB, MB, GB, …) easily:
var fileSize = (10).Kilobytes();fileSize.Bits => 81920fileSize.Bytes => 10240fileSize.Kilobytes => 10// We can add and subtract file size
var total = (10).Gigabytes() + (512).Megabytes() - (2.5).Gigabytes();// Display as 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// Parse backwards from the text (Not case sensitive)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");
Within the scope of this article, I just introduced some outstanding features of Humanizer. You can find out more here: https://github.com/MehdiK/Humanizer.
See you again.