From bde7e70ab251cb1c53ec3cf4c3510c654de73e63 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Wed, 22 Jan 2014 16:09:53 +0330 Subject: [PATCH 1/6] Renamed TimeSpanHumanizeTests files/class for consistency --- src/Humanizer.Tests/Humanizer.Tests.csproj | 8 ++++---- .../ar/{TimeSpanTests.cs => TimeSpanHumanizeTests.cs} | 5 ++--- .../es/{TimeSpanTests.cs => TimeSpanHumanizeTests.cs} | 4 ++-- .../pt-BR/{TimeSpanTests.cs => TimeSpanHumanizeTests.cs} | 4 ++-- ...umanizeExtensionsTests.cs => TimeSpanHumanizeTests.cs} | 4 ++-- 5 files changed, 12 insertions(+), 13 deletions(-) rename src/Humanizer.Tests/Localisation/ar/{TimeSpanTests.cs => TimeSpanHumanizeTests.cs} (93%) rename src/Humanizer.Tests/Localisation/es/{TimeSpanTests.cs => TimeSpanHumanizeTests.cs} (93%) rename src/Humanizer.Tests/Localisation/pt-BR/{TimeSpanTests.cs => TimeSpanHumanizeTests.cs} (94%) rename src/Humanizer.Tests/{TimeSpanHumanizeExtensionsTests.cs => TimeSpanHumanizeTests.cs} (95%) diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index ae9db0420..340e09b32 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -72,12 +72,12 @@ - + - + - + @@ -100,7 +100,7 @@ - + diff --git a/src/Humanizer.Tests/Localisation/ar/TimeSpanTests.cs b/src/Humanizer.Tests/Localisation/ar/TimeSpanHumanizeTests.cs similarity index 93% rename from src/Humanizer.Tests/Localisation/ar/TimeSpanTests.cs rename to src/Humanizer.Tests/Localisation/ar/TimeSpanHumanizeTests.cs index 09e9eb7c5..1b54942fa 100644 --- a/src/Humanizer.Tests/Localisation/ar/TimeSpanTests.cs +++ b/src/Humanizer.Tests/Localisation/ar/TimeSpanHumanizeTests.cs @@ -1,13 +1,12 @@ using System; -using Humanizer.Tests; using Xunit; using Xunit.Extensions; namespace Humanizer.Tests.Localisation.ar { - public class TimeSpanHumanizeExtensionsTests : AmbientCulture + public class TimeSpanHumanizeTests : AmbientCulture { - public TimeSpanHumanizeExtensionsTests() : base("ar") { } + public TimeSpanHumanizeTests() : base("ar") { } [Theory] [InlineData(7, "أسبوع واحد")] diff --git a/src/Humanizer.Tests/Localisation/es/TimeSpanTests.cs b/src/Humanizer.Tests/Localisation/es/TimeSpanHumanizeTests.cs similarity index 93% rename from src/Humanizer.Tests/Localisation/es/TimeSpanTests.cs rename to src/Humanizer.Tests/Localisation/es/TimeSpanHumanizeTests.cs index 13fdbc58c..171a61572 100644 --- a/src/Humanizer.Tests/Localisation/es/TimeSpanTests.cs +++ b/src/Humanizer.Tests/Localisation/es/TimeSpanHumanizeTests.cs @@ -3,9 +3,9 @@ namespace Humanizer.Tests.Localisation.es { - public class TimeSpanHumanizeExtensionsTests : AmbientCulture + public class TimeSpanHumanizeTests : AmbientCulture { - public TimeSpanHumanizeExtensionsTests() : base("es-ES") { } + public TimeSpanHumanizeTests() : base("es-ES") { } [Fact] public void TwoWeeks() diff --git a/src/Humanizer.Tests/Localisation/pt-BR/TimeSpanTests.cs b/src/Humanizer.Tests/Localisation/pt-BR/TimeSpanHumanizeTests.cs similarity index 94% rename from src/Humanizer.Tests/Localisation/pt-BR/TimeSpanTests.cs rename to src/Humanizer.Tests/Localisation/pt-BR/TimeSpanHumanizeTests.cs index c41cb67f0..fec50b57e 100644 --- a/src/Humanizer.Tests/Localisation/pt-BR/TimeSpanTests.cs +++ b/src/Humanizer.Tests/Localisation/pt-BR/TimeSpanHumanizeTests.cs @@ -3,9 +3,9 @@ namespace Humanizer.Tests.Localisation.ptBR { - public class TimeSpanHumanizeExtensionsTests : AmbientCulture + public class TimeSpanHumanizeTests : AmbientCulture { - public TimeSpanHumanizeExtensionsTests() : base("pt-BR") { } + public TimeSpanHumanizeTests() : base("pt-BR") { } [Fact] public void TwoWeeks() diff --git a/src/Humanizer.Tests/TimeSpanHumanizeExtensionsTests.cs b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs similarity index 95% rename from src/Humanizer.Tests/TimeSpanHumanizeExtensionsTests.cs rename to src/Humanizer.Tests/TimeSpanHumanizeTests.cs index 4ad659607..7f875bb74 100644 --- a/src/Humanizer.Tests/TimeSpanHumanizeExtensionsTests.cs +++ b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs @@ -3,9 +3,9 @@ namespace Humanizer.Tests { - public class TimeSpanHumanizeExtensionsTests : AmbientCulture + public class TimeSpanHumanizeTests : AmbientCulture { - public TimeSpanHumanizeExtensionsTests() : base("en-US") { } + public TimeSpanHumanizeTests() : base("en-US") { } [Fact] public void TwoWeeks() From 7605bb568a6a256eabc5fa4dede322197ba8fef1 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Wed, 22 Jan 2014 16:22:36 +0330 Subject: [PATCH 2/6] Changed TimeSpan tests to Theories --- src/Humanizer.Tests/TimeSpanHumanizeTests.cs | 118 ++++++------------- 1 file changed, 38 insertions(+), 80 deletions(-) diff --git a/src/Humanizer.Tests/TimeSpanHumanizeTests.cs b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs index 7f875bb74..4d08b1685 100644 --- a/src/Humanizer.Tests/TimeSpanHumanizeTests.cs +++ b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs @@ -1,5 +1,6 @@ using System; using Xunit; +using Xunit.Extensions; namespace Humanizer.Tests { @@ -7,108 +8,65 @@ public class TimeSpanHumanizeTests : AmbientCulture { public TimeSpanHumanizeTests() : base("en-US") { } - [Fact] - public void TwoWeeks() + [Theory] + [InlineData(14, "2 weeks")] + [InlineData(7, "1 week")] + public void Weeks(int days, string expected) { - var twoWeeks = TimeSpan.FromDays(14); + var twoWeeks = TimeSpan.FromDays(days); var actual = twoWeeks.Humanize(); - Assert.Equal("2 weeks", actual); + Assert.Equal(expected, actual); } - [Fact] - public void OneWeek() + [Theory] + [InlineData(6, "6 days")] + [InlineData(2, "2 days")] + [InlineData(1, "1 day")] + public void Days(int days, string expected) { - var oneWeek = TimeSpan.FromDays(7); - var actual = oneWeek.Humanize(); - Assert.Equal("1 week", actual); - } - - [Fact] - public void SixDays() - { - var sixDays = TimeSpan.FromDays(6); + var sixDays = TimeSpan.FromDays(days); var actual = sixDays.Humanize(); - Assert.Equal("6 days", actual); + Assert.Equal(expected, actual); } - [Fact] - public void TwoDays() + [Theory] + [InlineData(2, "2 hours")] + [InlineData(1, "1 hour")] + public void Hours(int hours, string expected) { - var twoDays = TimeSpan.FromDays(2); - var actual = twoDays.Humanize(); - Assert.Equal("2 days", actual); - } - - [Fact] - public void OneDay() - { - var oneDay = TimeSpan.FromDays(1); - var actual = oneDay.Humanize(); - Assert.Equal("1 day", actual); - } - - [Fact] - public void TwoHours() - { - var twoHours = TimeSpan.FromHours(2); + var twoHours = TimeSpan.FromHours(hours); var actual = twoHours.Humanize(); - Assert.Equal("2 hours", actual); - } - - [Fact] - public void OneHour() - { - var oneHour = TimeSpan.FromHours(1); - var actual = oneHour.Humanize(); - Assert.Equal("1 hour", actual); + Assert.Equal(expected, actual); } - [Fact] - public void TwoMinutes() + [Theory] + [InlineData(2, "2 minutes")] + [InlineData(1, "1 minute")] + public void Minutes(int minutes, string expected) { - var twoMinutes = TimeSpan.FromMinutes(2); + var twoMinutes = TimeSpan.FromMinutes(minutes); var actual = twoMinutes.Humanize(); - Assert.Equal("2 minutes", actual); + Assert.Equal(expected, actual); } - [Fact] - public void OneMinute() + [Theory] + [InlineData(2, "2 seconds")] + [InlineData(1, "1 second")] + public void Seconds(int seconds, string expected) { - var oneMinute = TimeSpan.FromMinutes(1); - var actual = oneMinute.Humanize(); - Assert.Equal("1 minute", actual); - } - - [Fact] - public void TwoSeconds() - { - var twoSeconds = TimeSpan.FromSeconds(2); + var twoSeconds = TimeSpan.FromSeconds(seconds); var actual = twoSeconds.Humanize(); - Assert.Equal("2 seconds", actual); + Assert.Equal(expected, actual); } - [Fact] - public void OneSecond() + [Theory] + [InlineData(2, "2 milliseconds")] + [InlineData(1, "1 millisecond")] + public void Milliseconds(int ms, string expected) { - var oneSecond = TimeSpan.FromSeconds(1); - var actual = oneSecond.Humanize(); - Assert.Equal("1 second", actual); - } - - [Fact] - public void TwoMilliseconds() - { - var twoMilliseconds = TimeSpan.FromMilliseconds(2); + var twoMilliseconds = TimeSpan.FromMilliseconds(ms); var actual = twoMilliseconds.Humanize(); - Assert.Equal("2 milliseconds", actual); - } - - [Fact] - public void OneMillisecond() - { - var oneMillisecond = TimeSpan.FromMilliseconds(1); - var actual = oneMillisecond.Humanize(); - Assert.Equal("1 millisecond", actual); + Assert.Equal(expected, actual); } [Fact] From 0d08fe817c2dc95b3c5ea51a3f1f96f8c5250b2e Mon Sep 17 00:00:00 2001 From: MehdiK Date: Wed, 22 Jan 2014 20:49:34 +0330 Subject: [PATCH 3/6] Added precision to TimeSpan Humanize - #29 #56 --- ...provalTest.approve_public_api.approved.txt | 28 ++++++++- src/Humanizer.Tests/TimeSpanHumanizeTests.cs | 58 ++++++++++++++---- src/Humanizer/TimeSpanHumanizeExtensions.cs | 61 ++++++++++++++++--- 3 files changed, 124 insertions(+), 23 deletions(-) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index 6b8c5aa0a..95bf3779e 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -58,9 +58,10 @@ public class InflectorExtensions { public string Camelize(string input) { } public string Dasherize(string underscoredWord) { } + public string Hyphenate(string underscoredWord) { } public string Pascalize(string input) { } - public string Pluralize(string word) { } - public string Singularize(string word) { } + public string Pluralize(string word, Humanizer.Plurality plurality) { } + public string Singularize(string word, Humanizer.Plurality plurality) { } public string Titleize(string input) { } public string Underscore(string input) { } } @@ -183,6 +184,14 @@ public class OrdinalizeExtensions public string Ordinalize(int number) { } } +public enum Plurality +{ + CouldBeEither, + Plural, + Singular, + value__, +} + public class PrepositionsExtensions { public System.DateTime At(System.DateTime date, int hour, int min, int second, int millisecond) { } @@ -191,6 +200,14 @@ public class PrepositionsExtensions public System.DateTime In(System.DateTime date, int year) { } } +public enum ShowQuantityAs +{ + None, + Numeric, + value__, + Words, +} + public class StringDehumanizeExtensions { public string Dehumanize(string input) { } @@ -204,7 +221,7 @@ public class StringHumanizeExtensions public class TimeSpanHumanizeExtensions { - public string Humanize(System.TimeSpan timeSpan) { } + public string Humanize(System.TimeSpan timeSpan, int precesion) { } } public class To @@ -214,4 +231,9 @@ public class To public Humanizer.IStringTransformer TitleCase { get; } public Humanizer.IStringTransformer UpperCase { get; } public string Transform(string input, Humanizer.IStringTransformer[] transformers) { } +} + +public class ToQuantityExtensions +{ + public string ToQuantity(string input, int quantity, Humanizer.ShowQuantityAs showQuantityAs) { } } \ No newline at end of file diff --git a/src/Humanizer.Tests/TimeSpanHumanizeTests.cs b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs index 4d08b1685..e386c4fca 100644 --- a/src/Humanizer.Tests/TimeSpanHumanizeTests.cs +++ b/src/Humanizer.Tests/TimeSpanHumanizeTests.cs @@ -13,8 +13,7 @@ public TimeSpanHumanizeTests() : base("en-US") { } [InlineData(7, "1 week")] public void Weeks(int days, string expected) { - var twoWeeks = TimeSpan.FromDays(days); - var actual = twoWeeks.Humanize(); + var actual = TimeSpan.FromDays(days).Humanize(); Assert.Equal(expected, actual); } @@ -24,8 +23,7 @@ public void Weeks(int days, string expected) [InlineData(1, "1 day")] public void Days(int days, string expected) { - var sixDays = TimeSpan.FromDays(days); - var actual = sixDays.Humanize(); + var actual = TimeSpan.FromDays(days).Humanize(); Assert.Equal(expected, actual); } @@ -34,8 +32,7 @@ public void Days(int days, string expected) [InlineData(1, "1 hour")] public void Hours(int hours, string expected) { - var twoHours = TimeSpan.FromHours(hours); - var actual = twoHours.Humanize(); + var actual = TimeSpan.FromHours(hours).Humanize(); Assert.Equal(expected, actual); } @@ -44,28 +41,65 @@ public void Hours(int hours, string expected) [InlineData(1, "1 minute")] public void Minutes(int minutes, string expected) { - var twoMinutes = TimeSpan.FromMinutes(minutes); - var actual = twoMinutes.Humanize(); + var actual = TimeSpan.FromMinutes(minutes).Humanize(); Assert.Equal(expected, actual); } [Theory] + [InlineData(135, "2 minutes")] + [InlineData(60, "1 minute")] [InlineData(2, "2 seconds")] [InlineData(1, "1 second")] public void Seconds(int seconds, string expected) { - var twoSeconds = TimeSpan.FromSeconds(seconds); - var actual = twoSeconds.Humanize(); + var actual = TimeSpan.FromSeconds(seconds).Humanize(); Assert.Equal(expected, actual); } [Theory] + [InlineData(2500, "2 seconds")] + [InlineData(1400, "1 second")] [InlineData(2, "2 milliseconds")] [InlineData(1, "1 millisecond")] public void Milliseconds(int ms, string expected) { - var twoMilliseconds = TimeSpan.FromMilliseconds(ms); - var actual = twoMilliseconds.Humanize(); + var actual = TimeSpan.FromMilliseconds(ms).Humanize(); + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(0, 3, "no time")] + [InlineData(0, 2, "no time")] + [InlineData(10, 2, "10 milliseconds")] + [InlineData(1400, 2, "1 second, 400 milliseconds")] + [InlineData(2500, 2, "2 seconds, 500 milliseconds")] + [InlineData(120000, 2, "2 minutes")] + [InlineData(62000, 2, "1 minute, 2 seconds")] + [InlineData(62020, 2, "1 minute, 2 seconds")] + [InlineData(62020, 3, "1 minute, 2 seconds, 20 milliseconds")] + [InlineData(3600020, 4, "1 hour, 20 milliseconds")] + [InlineData(3600020, 3, "1 hour, 20 milliseconds")] + [InlineData(3600020, 2, "1 hour, 20 milliseconds")] + [InlineData(3600020, 1, "1 hour")] + [InlineData(3603001, 2, "1 hour, 3 seconds")] + [InlineData(3603001, 3, "1 hour, 3 seconds, 1 millisecond")] + [InlineData(86400000, 3, "1 day")] + [InlineData(86400000, 2, "1 day")] + [InlineData(86400000, 1, "1 day")] + [InlineData(86401000, 1, "1 day")] + [InlineData(86401000, 2, "1 day, 1 second")] + [InlineData(86401200, 2, "1 day, 1 second")] + [InlineData(86401200, 3, "1 day, 1 second, 200 milliseconds")] + [InlineData(1296000000, 1, "2 weeks")] + [InlineData(1296000000, 2, "2 weeks, 1 day")] + [InlineData(1299600000, 2, "2 weeks, 1 day")] + [InlineData(1299600000, 3, "2 weeks, 1 day, 1 hour")] + [InlineData(1299630020, 3, "2 weeks, 1 day, 1 hour")] + [InlineData(1299630020, 4, "2 weeks, 1 day, 1 hour, 30 seconds")] + [InlineData(1299630020, 5, "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds")] + public void TimeSpanWithPrecesion(int milliseconds, int precesion, string expected) + { + var actual = TimeSpan.FromMilliseconds(milliseconds).Humanize(precesion); Assert.Equal(expected, actual); } diff --git a/src/Humanizer/TimeSpanHumanizeExtensions.cs b/src/Humanizer/TimeSpanHumanizeExtensions.cs index 81acce9e7..15800e8d1 100644 --- a/src/Humanizer/TimeSpanHumanizeExtensions.cs +++ b/src/Humanizer/TimeSpanHumanizeExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using Humanizer.Configuration; using Humanizer.Localisation; @@ -15,16 +16,60 @@ public static class TimeSpanHumanizeExtensions /// Turns a TimeSpan into a human readable form. E.g. 1 day. /// /// + /// The maximum number of time units to return /// - /// - /// This method chooses the largest part of the TimeSpan (Day, Hour, Minute, Second, - /// Millisecond) and returns only that part. - /// - public static string Humanize(this TimeSpan timeSpan) + public static string Humanize(this TimeSpan timeSpan, int precision = 1) { - return FormatParameters - .Select(format => TryFormat(format, timeSpan)) - .FirstOrDefault(result => result != null); + var result = new StringBuilder(); + for (int i = 0; i < precision; i++) + { + var timePart = FormatParameters + .Select(format => TryFormat(format, timeSpan)) + .FirstOrDefault(part => part != null); + + if (result.Length > 0) + result.Append(", "); + + result.Append(timePart); + + timeSpan = DeduceTheLargestUnit(timeSpan); + if (timeSpan == TimeSpan.Zero) + return result.ToString(); + } + + return result.ToString(); + } + + static TimeSpan DeduceTheLargestUnit(TimeSpan timeSpan) + { + return timeSpan - LargestUnit(timeSpan); + } + + static TimeSpan LargestUnit(TimeSpan timeSpan) + { + var days = timeSpan.Days; + if (days >= 7) + return TimeSpan.FromDays((days/7) * 7); + if (days >= 1) + return TimeSpan.FromDays(days); + + var hours = timeSpan.Hours; + if (hours >= 1) + return TimeSpan.FromHours(hours); + + var minutes = timeSpan.Minutes; + if (minutes >= 1) + return TimeSpan.FromMinutes(minutes); + + var seconds = timeSpan.Seconds; + if (seconds >= 1) + return TimeSpan.FromSeconds(seconds); + + var milliseconds = timeSpan.Milliseconds; + if (milliseconds >= 1) + return TimeSpan.FromMilliseconds(milliseconds); + + return TimeSpan.Zero; } /// From 4b91a6f5b8d9fecf94793771888e8365c8c9d551 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Wed, 22 Jan 2014 23:36:49 +0330 Subject: [PATCH 4/6] refactored TimeSpan Humanize --- src/Humanizer/Humanizer.csproj | 1 - .../Localisation/TimeSpanPropertyFormat.cs | 33 ---------------- src/Humanizer/TimeSpanHumanizeExtensions.cs | 38 ++++++++++++++++--- 3 files changed, 33 insertions(+), 39 deletions(-) delete mode 100644 src/Humanizer/Localisation/TimeSpanPropertyFormat.cs diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index 934f38d18..fc500887c 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -111,7 +111,6 @@ - diff --git a/src/Humanizer/Localisation/TimeSpanPropertyFormat.cs b/src/Humanizer/Localisation/TimeSpanPropertyFormat.cs deleted file mode 100644 index 481b8c846..000000000 --- a/src/Humanizer/Localisation/TimeSpanPropertyFormat.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; - -namespace Humanizer.Localisation -{ - /// - /// Stores a single mapping of a part of the time span (Day, Hour etc.) to its associated - /// formatter method for Zero, Single, Multiple. - /// - class TimeSpanPropertyFormat - { - public TimeSpanPropertyFormat( - Func propertySelector, - Func single, - Func multiple) - { - PropertySelector = propertySelector; - Single = single; - Multiple = multiple; - Zero = () => null; - } - - public TimeSpanPropertyFormat(Func propertySelector, Func zeroFunc) - { - PropertySelector = propertySelector; - Zero = zeroFunc; - } - - public Func PropertySelector { get; private set; } - public Func Single { get; private set; } - public Func Multiple { get; private set; } - public Func Zero { get; private set; } - } -} \ No newline at end of file diff --git a/src/Humanizer/TimeSpanHumanizeExtensions.cs b/src/Humanizer/TimeSpanHumanizeExtensions.cs index 15800e8d1..c8fa145c5 100644 --- a/src/Humanizer/TimeSpanHumanizeExtensions.cs +++ b/src/Humanizer/TimeSpanHumanizeExtensions.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Text; using Humanizer.Configuration; -using Humanizer.Localisation; namespace Humanizer { @@ -16,7 +15,7 @@ public static class TimeSpanHumanizeExtensions /// Turns a TimeSpan into a human readable form. E.g. 1 day. /// /// - /// The maximum number of time units to return + /// The maximum number of time units to return. Defaulted is 1 which means the largest unit is returned /// public static string Humanize(this TimeSpan timeSpan, int precision = 1) { @@ -32,15 +31,15 @@ public static string Humanize(this TimeSpan timeSpan, int precision = 1) result.Append(timePart); - timeSpan = DeduceTheLargestUnit(timeSpan); + timeSpan = TakeOutTheLargestUnit(timeSpan); if (timeSpan == TimeSpan.Zero) - return result.ToString(); + break; } return result.ToString(); } - static TimeSpan DeduceTheLargestUnit(TimeSpan timeSpan) + static TimeSpan TakeOutTheLargestUnit(TimeSpan timeSpan) { return timeSpan - LargestUnit(timeSpan); } @@ -135,5 +134,34 @@ private static string TryFormat( return propertyFormat.Multiple(value); } } + + /// + /// Stores a single mapping of a part of the time span (Day, Hour etc.) to its associated + /// formatter method for Zero, Single, Multiple. + /// + class TimeSpanPropertyFormat + { + public TimeSpanPropertyFormat( + Func propertySelector, + Func single, + Func multiple) + { + PropertySelector = propertySelector; + Single = single; + Multiple = multiple; + Zero = () => null; + } + + public TimeSpanPropertyFormat(Func propertySelector, Func zeroFunc) + { + PropertySelector = propertySelector; + Zero = zeroFunc; + } + + public Func PropertySelector { get; private set; } + public Func Single { get; private set; } + public Func Multiple { get; private set; } + public Func Zero { get; private set; } + } } } \ No newline at end of file From 51587626f5ccef1e4b84a2f39881869f43e205b8 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Wed, 22 Jan 2014 23:43:21 +0330 Subject: [PATCH 5/6] Added TimeSpan precision to readme --- readme.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 97fbd7068..b74130cdc 100644 --- a/readme.md +++ b/readme.md @@ -130,7 +130,22 @@ You can call `Humanizes` on a `TimeSpan` to a get human friendly representation TimeSpan.FromMilliseconds(1).Humanize() => "1 millisecond" TimeSpan.FromMilliseconds(2).Humanize() => "2 milliseconds" TimeSpan.FromDays(1).Humanize() => "1 day" -TimeSpan.FromDays(14).Humanize() => "2 weeks" +TimeSpan.FromDays(16).Humanize() => "2 weeks" +``` + +There is an optional `precision` parameter for `TimeSpan.Humanize` which allows you to specify the precision of the returned value. +The default value of `precision` is 1 which means only the largest time unit is returned like you saw in `TimeSpan.FromDays(16).Humanize()`. +Here is a few examples of specifying precision: + +```C# +TimeSpan.FromDays(1).Humanize(precision:2) => "1 day" // no difference when there is only on unit in the provided TimeSpan +TimeSpan.FromDays(16).Humanize(2) => "2 weeks, 2 days" + +// the same TimeSpan value with different precision returns different results +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" +TimeSpan.FromMilliseconds(1299630020).Humanize(5) => "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds" ``` ###Inflector methods From 4bf786d6ce96bd272a46a8d510afd64a52e94299 Mon Sep 17 00:00:00 2001 From: MehdiK Date: Thu, 23 Jan 2014 09:39:09 +0330 Subject: [PATCH 6/6] Fixed typo - precision --- .../PublicApiApprovalTest.approve_public_api.approved.txt | 2 +- src/Humanizer.Tests/Humanizer.Tests.csproj | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index 95bf3779e..4176ebe5d 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -221,7 +221,7 @@ public class StringHumanizeExtensions public class TimeSpanHumanizeExtensions { - public string Humanize(System.TimeSpan timeSpan, int precesion) { } + public string Humanize(System.TimeSpan timeSpan, int precision) { } } public class To diff --git a/src/Humanizer.Tests/Humanizer.Tests.csproj b/src/Humanizer.Tests/Humanizer.Tests.csproj index 340e09b32..0d49e8c6f 100644 --- a/src/Humanizer.Tests/Humanizer.Tests.csproj +++ b/src/Humanizer.Tests/Humanizer.Tests.csproj @@ -115,6 +115,9 @@ Humanizer + + +