Code Monkey home page Code Monkey logo

humanizer's People

Contributors

adamchester avatar ahmedshuhel avatar akamud avatar aktheknight avatar aloisdg avatar ammahir avatar borzoo avatar clairernovotny avatar dependabot-preview[bot] avatar dependabot[bot] avatar gabrielweyer avatar hangy avatar hazzik avatar hmemcpy avatar i3arnon avatar igorkulman avatar justin-edwards avatar kristinn-stefansson avatar lauren-rutledge avatar louis-z avatar mehdik avatar mexx avatar neilboyd avatar simoncropp avatar strobelt avatar thunsaker avatar vgrigoriu avatar vnsharanin avatar wahidshalaly avatar wcsanders1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

humanizer's Issues

"One year ago" or "Last year"

In Date.Humanize we have "yesterday" for a day ago and "one year ago" for "last year". How would everyone feel about changing it to "last year"? It is more humanized and is more consistent with the "yesterday" logic!

Getting only needed resources on nuget install

@hmemcpy raised a valid concern:

how can I prevent from copying all the language-specific resource to the output dir with Humanizer? Don't want 'em :)

NuGet supports two localization approach: single & satellite localized packages. Humanizer currently uses single localized package & I don't think it's possible to achieve this in this mode.

I think it's a good idea to switch to satellite packages even though it's a breaking change for this and other benefits.

To Do:

  • Create a nupsec for each locale, currently 39
  • Create script to update versions correctly on resource packages
  • Create script to push all packages to NuGet from the MyGet feed

Localisation failing in Windows Store apps

As far as I can tell all Localisation methods are failing in Windows Store apps. The root causes appears to be the loading for the resource strings.

All calls to DateTime.Humanize and TimeSpan.Humanize are throwing an ArgumentNullException in String.Format.

In the below example the result is null rather than the resource string.

var resource = Humanizer.Localisation.Resources.GetResource(Humanizer.Localisation.ResourceKeys.TimeSpanHumanize.MultipleDays);

Should be able to call Singularize and Pluralize on singular and plural words respectively

Currently calling Singularize on a singular word returns null or an incorrect value (e.g. "Process".Singularize() => "Proces". Same goes for Pluralize on a plural word (e.g. "men".Pluralize() => "mens")

Ideally we need to implement IsPlural and/or IsSingular extension methods. This way we can fix this issue using the methods and the users of the framework can use the new methods to check a word without having to call Singularize or Pluralize on it.

One-off error in unit tests

I am trying to add Slovak localization, that requires a separate Formatter. I have written unit tests but when I run them, some of them are one-off.

Consider

        [Theory]
        [InlineData(1, "o hodinu")]
        [InlineData(2, "o 2 hodiny")]
        [InlineData(3, "o 3 hodiny")]
        [InlineData(4, "o 4 hodiny")]
        [InlineData(5, "o 5 hodín")]
        [InlineData(6, "o 6 hodín")]
        [InlineData(10, "o 10 hodín")]
        public void NHoursFromNow(int number, string expected)
        {
            var humanize = DateTime.UtcNow.AddHours(number).Humanize();
            Assert.Equal(expected, humanize);
        }

When I run the tests, I get "o 9 hodín" for the last case testing "o 10 hodín". I guess the Humanize method got called with 9 instead of 10.

Am I missing something in writting the tests?

Refactor formatters (if possible)

Copied @waheedsayed's thought over from #70:

I'm thinking to do the following

  1. No need for two methods to show either xxxAgo() or xxxFromNow(). One method with a flag (isFuture) is enough & method should use the proper format from resources.
  2. No need for two methods to show either Singlexxx() or Multiplexxx(int count). Again, one method with input (with default = 1) is enough
  3. Not sure about this yet, but I'm thinking we should also refactor the DefaultFormatter

Split it into two formatters: DateTimeFormatter & TimeSpanFormatter
Shorten names by removing prefixes DateHumaniz_xxx & TimeSpanHumanize_xxx

Add `ToQuantity` extension method

"request".ToQuantity(0) => "0 requests"
"request".ToQuantity(1) => "1 request"
"request".ToQuantity(2) => "2 requests"

Heaps of useful discussion around this issue and the method naming can be found at #25

Add non-generic EnumDehumanizeExtensions.DehumanizeTo

I could use a non-generic EnumDehumanizeExtensions.DehumanizeTo for when the enum type isn't known at compile time.

I wrote my own which I use in my HumanizedEnumConverter:
https://rapiddevbookcode.codeplex.com/SourceControl/latest#LLBL Pro v4.1/AW.Helper/TypeConverters/HumanizedEnumConverter.cs

e.g.

    private static Enum DehumanizeTo(string input, Type enumType)
    {
      var values = Enum.GetValues(enumType);
      foreach (var value in values)
      {
        var enumValue = value as Enum;
        if (enumValue == null)
          return null;
        if (string.Equals(enumValue.Humanize(), input, StringComparison.OrdinalIgnoreCase))
          return enumValue;
      }
      return null;
    }

DateTime Humanize

When i do DateTime.Now.AddDays(-1).Humanize() i get result as "23 hours ago" and not as yesterday which i have expected.
Am i doing anything wrong?
Also DateTime.Now.AddDays(1).Humanize() gives me result as not yet.

ToWords - Hebrew language

In Hebrew, there are both masculine and feminine forms of numbers, e.g. 1 can be either אחת ("achat", feminine) or אחד ("echad", masculine), depending on the preceding or following word.

I wonder how to address this issue in implementing ToWords() Hebrew extension? An additional overload, accepting this sounds reasonable, but not all languages have this...

Ideas?

Dehumanize DateTime with reference date?

The documentation says dehumanization of dates is not reversible, but is that only because a phrase like "yesterday" is relative to some unknown reference point?

What if the API included a method like "yesterday".DehumanizeRelativeTo(DateTime.UtcNow) that calculated "yesterday" from the perspective of the current time? It would be really cool to be able to turn the string "set an alarm for tomorrow at two thirty" into a command + datetime-relative-to-now using the Humanize library (rather than rolling your own). I assume this idea has been visited before, were there any specific challenges that can't be solved with an explicit reference date?

Should fix and extend the pluralization rules (WAS: Consider using built in .NET for underlying pluralization)

The built in .NET PluralizationService has more work done in correctly pluralizing most of English. Please consider using this as the service behind the Pluralize feature of Humanizer.

http://referencesource.microsoft.com/#System.Data.Entity.Design/Entity/Design/PluralizationService/EnglishPluralizationService.cs

I realize it is in an assembly you probably do not want to require as a dependency, but perhaps some notes can be taken from it.

Licensing, Copyright

Great work with this utility library - very useful. A few things to keep you out of hot water that I might suggest is:

  • Add a copyright to the copy/pasted classes, right in the top. You've renamespaced Inflector (as an example) which is generally a bad idea as it breaks copyright - but it's also widely done and it seems to be a floating library anyway.
  • Add the license for Inflector to your LICENSE file. When people download and use Inflector, they're downloading someone else's copyrighted work - you need to be more upfront about this rather than popping InflectorLicense.txt into the codebase. I might suggest doing this for any licensed code you've included in Humanizer.

Just a few thoughts :).

TimeSpan's should show the time breakdown

I would expect this:

var timespan = TimeSpan.FromDays(2) + TimeSpan.FromHours(3) + TimeSpan.FromMinutes(5);

Assert.That(timespan.Humanize(), Is.EqualTo("2 days, 3 hours, 5 minutes"));

But it seems to come back with 2 days currently.

ToQuantity ShowQuantityAs.Words fails on Arabic culture

It gives صفر cases instead of zero cases and واحد case instead of one case ?

There is 15 other test fail, namely ToOrdinalWords and DateHumanizeTests.

ToOrdinalWords صفرth is wrong and does not exist in Arabic ?

Only happen on my computer because my windows region and language is set to Arabic.

Add code attribution to Roman feature

Jesse Slicer commented on Scott Hanselman's post about Humanizer saying Roman feature is a copy of his work and the contributor failed to acknowledge that in the code, which is not nice. Need to fix this ASAP.

Some suggestions to humanizer

Hello,

I tested Humanizer on an ASP.NET MVC CMS project and I have a few suggestions:

1 - Add a HumanizerConfiguration for startup
This would be used to define, for example, custom translations.
The translations could be use a Fluent API.
This would be more flexible then having Resource files built in Humanizer.

2 - Add the option for enum humanizer to allow a different attribute than Description.
Again, this could be applied in HumanizerConfiguration.

3 - Add extensions points to humanizer ...
I could create an extension to do some type of conversion ...
This way people could start to create humanizer "plugins" and share.

What do you think? Just some ideas from my short experience with Humanizer.

Thank You,
Miguel

Make Configurator.FormatterFactories public

I suggest to make Configurator.FormatterFactories public or add methods to add and remove factories from configuration. It can be useful if someone wants to use custom formatter which is, for example, not ready to be included to a library, etc.

Make DateTimeHumanize tests consistent across all locales

DateTimeHumanize tests are created by contributors and over the time the coding style has shifted and now the old code are inconsistent with the rest of the tests. Test method names, testing patterns etc should be made consistent.

Humanizer can/should support truncating a string to a certain length whilst adding ellipsis

What do you guys/gals think about the following:

var longPieceOfText = "This is some text that's too large to display into a column in a table (HTML), but should present nicely when truncated to a certain length";
Assert.That(longPieceOfText.TrimTo(10), Is.EqualTo("This is s…")); 

Possible variations include trimming to the last word

Assert.That(longPieceOfText.TrimTo(10,Option.RespectWords), Is.EqualTo("This is some …")); 

Trimming to the last word but respecting the length as absolute maximum

Assert.That(longPieceOfText.TrimTo(10,Option.RespectWords | ,Option.ConsiderLengthAbsolute), Is.EqualTo("This is …")); 

See also https://github.com/HubSpot/humanize#truncate

If it's desirable I might start hacking on a PR this week...

Elevator pitch for the framework?

Humanizer started out with three very simple methods all named Humanize. So I described it as "a framework that turns your normal strings, type names, enums and DateTime into a human friendly format".

Things have changed a lot since and I am looking for a better elevator pitch that tells the story nicely and is less than 10 sentences.

Your help is much appreciated :)

ToQuantity without displaying the number

Provide a mechanism to optionally omit the number from the ToQuantity result. For example, "case".ToQuantity(5, QuantityOptions.OmitNumber) would be "cases", where OmitNumber would be a Flags enum.

Support future dates as "from now"

Looks like dates are all in the past.

How about future dates - should use the same breakdowns but be "from now" instead.

Also, maybe +/- some threshold, there could be a "now"

Percentage extensions

Hello,

I got an idea to make percentage extensions

for example :

public static decimal PercentageOf(this double number, int percent) {
            return (decimal)(number * percent / 100);
        }

Introduce an interface for the clock

Last night I have also randomly failing tests which used DateHumanize.Verify.
I had no time to look deeper, but the problem seems be that Humanize uses DateTime.UtcNow to determine current time.
Maybe we should add an interface for the clock, so we get reliable time in tests.
This approach also followed by the NodaTime library.

Add precision to DateTime.Humanize

e.g. (55 days).Humanize(precision:5) => "2 months" (instead of one month)

We also need to apply a sensible default based on each unit; e.g.

  • 5 for days so 55 days reports as 2 months
  • 2 for months so 22 months reports as 2 years

Also perhaps these defaults should be lower for singular units; e.g. 25 days shouldn't report as one month; but 29 days perhaps could report as one month while 55 days reports as 2 months! A bit unsure about this as it might get a bit confusing.

Either way this is a breaking change and as such set for V2.

Numbers to positions

It would be handy to have an option to do something like

1 -> First
2 -> Second
3 -> Third

Hopefully theres not something already to do this, couldnt see anything.

Could not load 'System.Core, Version 2.0.5.0'

Hi,

I'm observing the above exception running Humanizer 1.11.3.0 on a production box with IIS6/.Net 4 / ASP.Net MVC 3.

Found a similar issue reported with AutoMapper (AutoMapper/AutoMapper#383) and wondering whether it could be the same root cause.

Complete stack trace follows;

[FileLoadException: Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)]
Humanizer.TimeSpanHumanizeExtensions.Humanize(TimeSpan timeSpan, Int32 precision) +0
BHP.ROS.Statusboard.DAL.Ampla.Dtos.DowntimeDto.get_DurationText() +177

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +0
System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +152
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +393
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +38
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35
System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +763
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +1692
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +146
System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +182
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +1647
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +146
System.Web.Script.Serialization.JavaScriptSerializer.SerializeDictionary(IDictionary o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +663
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +1576
System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +146
System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat) +117
System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context) +280
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) +784900 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +265
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
System.Web.Mvc.Controller.ExecuteCore() +159
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.