Skip to content

Commit

Permalink
Merge pull request #517 from ronnieoverby/dev
Browse files Browse the repository at this point in the history
allow formatted byte size when humanizing ByteRate
  • Loading branch information
Oren Novotny committed Jan 30, 2016
2 parents c6f92e8 + 564712d commit 99500a7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,13 @@ text = size.Per(measurementInterval).Humanize(TimeUnit.Hour);
// 35.15625 GB/hour
```

You can specify a format for the bytes part of the humanized output:

```
19854651984.Bytes().Per(1.Seconds()).Humanize("#.##");
// 18.49 GB/s
```

##<a id="mix-this-into-your-framework-to-simplify-your-life">Mix this into your framework to simplify your life</a>
This is just a baseline and you can use this to simplify your day to day job. For example, in Asp.Net MVC we keep chucking `Display` attribute on ViewModel properties so `HtmlHelper` can generate correct labels for us; but, just like enums, in vast majority of cases we just need a space between the words in property name - so why not use `"string".Humanize` for that?!

Expand Down
18 changes: 17 additions & 1 deletion src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ public void TimeUnitTests(long megabytes, double measurementIntervalSeconds, Tim

var rate = size.Per(measurementInterval);
var text = rate.Humanize(displayInterval);


Assert.Equal(expectedValue, text);
}

[Theory]
[InlineData(19854651984, 1, TimeUnit.Second, null, "18.4910856038332 GB/s")]
[InlineData(19854651984, 1, TimeUnit.Second, "#.##", "18.49 GB/s")]
public void FormattedTimeUnitTests(long bytes, int measurementIntervalSeconds, TimeUnit displayInterval, string format, string expectedValue)
{
var size = ByteSize.FromBytes(bytes);
var measurementInterval = TimeSpan.FromSeconds(measurementIntervalSeconds);
var rate = size.Per(measurementInterval);
var text = rate.Humanize(format, displayInterval);

Assert.Equal(expectedValue, text);
}



[Theory]
[InlineData(TimeUnit.Millisecond)]
[InlineData(TimeUnit.Day)]
Expand All @@ -61,5 +76,6 @@ public void ThowsOnUnsupportedData(TimeUnit units)
dummyRate.Humanize(units);
});
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Humanizer.Bytes
public System.TimeSpan Interval { get; }
public Humanizer.Bytes.ByteSize Size { get; }
public string Humanize(Humanizer.Localisation.TimeUnit timeUnit = 1) { }
public string Humanize(string format, Humanizer.Localisation.TimeUnit timeUnit = 1) { }
}
public struct ByteSize : System.IComparable, System.IComparable<Humanizer.Bytes.ByteSize>, System.IEquatable<Humanizer.Bytes.ByteSize>
{
Expand Down
14 changes: 13 additions & 1 deletion src/Humanizer/Bytes/ByteRate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public ByteRate(ByteSize size, TimeSpan interval)
/// <param name="timeUnit">Unit of time to calculate rate for (defaults is per second)</param>
/// <returns></returns>
public string Humanize(TimeUnit timeUnit = TimeUnit.Second)
{
return Humanize(null, timeUnit);
}

/// <summary>
/// Calculate rate for the quantity of bytes and interval defined by this instance
/// </summary>
/// <param name="timeUnit">Unit of time to calculate rate for (defaults is per second)</param>
/// <param name="format">The string format to use for the number of bytes</param>
/// <returns></returns>
public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second)
{
TimeSpan displayInterval;
string displayUnit;
Expand All @@ -60,7 +71,8 @@ public string Humanize(TimeUnit timeUnit = TimeUnit.Second)
else
throw new NotSupportedException("timeUnit must be Second, Minute, or Hour");

return (new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds)).Humanize() + '/' + displayUnit;
return new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds)
.Humanize(format) + '/' + displayUnit;
}
}
}

0 comments on commit 99500a7

Please sign in to comment.