nuget.server包_本周的NuGet软件包:Humanizer使.NET数据类型更人性化

nuget.server包

nuget.server包

The .NET BCL (Base Class Library) moves too slow, IMHO. That's why NuGet and NuGet packages are so nice. It's a joy to find a "rightly sized" library like NodaTime, for example. It's a better date and time API for .NET. Microsoft should just use it, it's lovely.

.NET BCL(基类库)移动太慢,恕我直言。 这就是为什么NuGet和NuGet软件包如此好。 例如,找到像NodaTime这样的“大小合适”的库很高兴。 这是.NET更好的日期和时间API。 Microsoft应该只使用它,它很可爱。

Another nicely-sized Open Source library, and the focus for today's blog post, is Humanizer from http://humanizr.net. They say "Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities" and it does just that.

另一个大小合适的开放源代码库(也是当今博客文章的重点)是http://humanizr.net的Humanizer 。 他们说:“ Humanizer满足了您.NET对操纵和显示字符串,枚举,日期,时间,时间跨度,数字和数量的所有需求”,它就是这样做的。

It's a lovely joy of a library and you should check it out. Just make yourself a console app now and install-package Humanizer, and explore.

这是一个图书馆的快乐,您应该检查一下。 只需立即使自己成为控制台应用并安装软件包Humanizer,然后进行探索。

Note: Be sure to check out all the NuGet packages of the week! There's more!

注意:请务必查看本周所有的Nu​​Get软件包! 还有更多!

The word "Humanize" evokes, for me, a feeling of making something simpler, more accessible, more useful, more fluent, more flexible.

对我来说,“人性化”一词使人感到一种使事情变得更简单,更易用,更有用,更流畅,更灵活的感觉。

Here's some great examples of problems that Humanizer solves.

这是Humanizer解决的一些很好的例子。

First, Humanzer has a LOT of extension methods to the string type. If you've got a funky string, it will turn it into as close to a normally-cased sentence as possible. This is even more useful if you're using BDD frameworks like BDDfy.

首先,Humanzer对字符串类型有很多扩展方法。 如果您有一个时髦的字符串,它将把它变成尽可能接近正常情况下的句子。 如果您使用的是BDDfy之类的BDD框架,这将更加有用。

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

There's dozens of great string methods, but it's the DateTime extension methods that really shine. These will feel familiar to Rails developers and Twitter users.

有很多很棒的字符串方法,但是真正发挥作用的是DateTime扩展方法。 这些将使Rails开发人员和Twitter用户感到熟悉。

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"

TimeSpan.FromMilliseconds(1299630020).Humanize() => "2 weeks"
TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"

Pluralization libraries are always fun, as I've blogged before and Humanizer doesn't disappoint. You can pluralize and singularlize strings, but the "To Quantity" methods are fantastic, doing exactly what you'd expect, even if you do something odd:

多元化的库总是很有趣,正如我之前写过的,而Humanizer并不令人失望。 您可以对字符串进行复数和单数化处理,但是“ To Quantity”方法非常出色,即使您执行了一些奇怪的操作,也可以完全按照您的期望进行操作:

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

The Number to Words family of methods go even further:

Number to Words系列方法甚至更进一步:

3501.ToWords() => "three thousand five hundred and one"
121.ToOrdinalWords() => "hundred and twenty first"
8.ToRoman() => "VIII"

We've all written a truncate method to take a long string and add "..." at the end.

我们都编写了一个截断方法,以获取长字符串并在末尾添加“ ...”。

"Long text to truncate".Truncate(6, Truncator.FixedNumberOfCharacters) => "Long t…"
"Long text to truncate".Truncate(6, "---", Truncator.FixedNumberOfCharacters) => "Lon---"

I'm just scratching the surface of Humanizer, going through it's Getting Started. There's 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.

我只是在拖弄Humanizer的表面,经历了入门。 也有一些示例,您可以将其插入自己的框架中或深入ASP.NET MVC中,并使枚举,类型名称和属性人性化,以使代码保持DRY状态。

Humanizer is even starting to support localization to non-English languages, and helping with the localization of Humanizer is a GREAT OPPORTUNITY if you're looking to get into Open Source as well as learning Git and GitHub Flow.

Humanizer甚至开始支持对非英语语言的本地化,如果您希望进入开源以及学习Git和GitHub Flow ,那么帮助Humanizer本地化将是一个巨大的机会。

I like open source libraries like this that look like the code we've all written before...except tested and complete. ;) We've all written utilities like this, except as one-of functions. I look forward to NOT writing methods like this in the future, and instead using libraries like Humanizer. I fully plan to use this library in my next project (a startup I'm working on.)

我喜欢这样的开源库,它们看起来像我们之前编写的代码……经过测试和完善。 ;)我们已经编写了所有类似这样的实用程序,只是其中一种功能。 我期待将来不再编写此类方法,而希望使用Humanizer之类的库。 我完全计划在下一个项目(我正在从事的初创公司)中使用此库。

Go take a look at Humanizer and thank the author on Twitter while you're at it!

快来看看Humanizer感谢在Twitter上的作者

翻译自: https://www.hanselman.com/blog/nuget-package-of-the-week-humanizer-makes-net-data-types-more-human

nuget.server包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值