Skip to content

Commit

Permalink
Merge pull request #271 from justin-edwards/humanize-collections-readme
Browse files Browse the repository at this point in the history
update docs to include collection humanization
  • Loading branch information
MehdiK committed May 21, 2014
2 parents 77a0feb + ce63903 commit 1d5710c
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Humanizer meets all your .NET needs for manipulating and displaying strings, enu
- [Dehumanize Enums](#dehumanize-enums)
- [Humanize DateTime](#humanize-datetime)
- [Humanize TimeSpan](#humanize-timespan)
- [Humanize Collections](#humanize-collections)
- [Inflector methods](#inflector-methods)
- [Pluralize](#pluralize)
- [Singularize](#singularize)
Expand Down Expand Up @@ -308,6 +309,43 @@ TimeSpan.FromMilliseconds(2).Humanize() => "2 milisekundy"
TimeSpan.FromMilliseconds(5).Humanize() => "5 milisekúnd"
```

###<a id="humanize-collections">Humanize Collections</a>

You can call `Humanize` on any `IEnumerable` to get a nicely formatted string representing the objects in the collection. By default `ToString()` will be called on each item to get its representation but a formatting function may be passed to `Humanize` instead. Additionally, a default separator is provided("and" in English), but a different separator may be passed into `Humanize`.

For instance:

```C#
class SomeClass
{
public string SomeString;
public int SomeInt;
public override string ToString()
{
return "Specific String";
}
}

string FormatSomeClass(SomeClass sc)
{
return string.Format("SomeObject #{0} - {1}", sc.SomeInt, sc.SomeString);
}

var collection = new List<SomeClass>
{
new SomeClass { SomeInt = 1, SomeString = "One" },
new SomeClass { SomeInt = 2, SomeString = "Two" },
new SomeClass { SomeInt = 3, SomeString = "Three" }
};

collection.Humanize() // "Specific String, Specific String, and Specific String"
collection.Humanize("or") //"Specific String, Specific String, or Specific String"
collection.Humanize(FormatSomeClass) //"SomeObject #1 - One, SomeObject #2 - Two, and SomeObject #3 - Three"
collection.Humanize(sc => sc.SomeInt.Ordinalize(), "or") //"1st, 2nd, or 3rd"
```

You can provide your own collection formatter by implementing `ICollectionFormatter` and registering it with `Configurator.CollectionFormatters`

###<a id="inflector-methods">Inflector methods</a>
There are also a few inflector methods:

Expand Down

0 comments on commit 1d5710c

Please sign in to comment.