Cpo Design

Humanizer example

Pavel Svarc Monday, February 8, 2016 5:42 AM

Humanizer  an opensource project that provides real benefits to the developer.


Humanizer is a Portable Class Library with support for .Net 4.5+, Windows Phone 8, Win Store, and ASP.NET 5 (CoreCLR) applications. Also Humanizer symbols are source indexed with GitLink and are included in the package so you can step through Humanizer code while debugging your code.


 I have copied couple examples from the github site to illustrate the usage in my examples below


"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"
"HUMANIZER".Transform(To.LowerCase, To.TitleCase) => "Humanizer" 
"Long text to truncate".Truncate(10, "---") => "Long te---"
"Long text to truncate".Truncate(2, Truncator.FixedNumberOfWords) => "Long text…"


Now my favourite Enums to user friendly text

enum EnumUnderTest { [Description("Custom description")] MemberWithDescriptionAttribute, MemberWithoutDescriptionAttribute, ALLCAPITALS }


This can be used as


// 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"

Example of using resources


public enum EnumUnderTest
    [Display(Description = "EnumUnderTest_Member", ResourceType = typeof(Project.Resources))]

which produces

EnumUnderTest.Member.Humanize() => "content" // from Project.Resources found under "EnumUnderTest_Member" resource key

Related posts

Currenlty there are no related articles.