Skip to content

Commit

Permalink
Updated public API. Started implenting Humanizr#526. Fixed some grammar.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBarka committed Jan 2, 2019
1 parent 11bd9fd commit 9ba6fc1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
Expand Up @@ -412,8 +412,8 @@ namespace Humanizer
public class static MetricNumeralExtensions
{
public static double FromMetric(this string input) { }
public static string ToMetric(this int input, bool hasSpace = False, bool useSymbol = True, System.Nullable<int> decimals = null) { }
public static string ToMetric(this double input, bool hasSpace = False, bool useSymbol = True, System.Nullable<int> decimals = null) { }
public static string ToMetric(this int input, bool hasSpace = False, bool useSymbol = True, System.Nullable<int> decimals = null, System.Nullable<char> largestPrefix = null) { }
public static string ToMetric(this double input, bool hasSpace = False, bool useSymbol = True, System.Nullable<int> decimals = null, System.Nullable<char> largestPrefix = null) { }
}
public class NoMatchFoundException : System.Exception
{
Expand Down
44 changes: 35 additions & 9 deletions src/Humanizer/MetricNumeralExtensions.cs
Expand Up @@ -101,17 +101,19 @@ public static double FromMetric(this string input)
/// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
/// <param name="useSymbol">True will use symbol instead of name</param>
/// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
/// <param name="largestPrefix">If not null it is the largest prefix used in result.</param>
/// <example>
/// <code>
/// 1000.ToMetric() => "1k"
/// 123.ToMetric() => "123"
/// 1E-1.ToMetric() => "100m"
/// 1_000_000.ToMetric(largestPrefix = 'k') => "1000k"
/// </code>
/// </example>
/// <returns>A valid Metric representation</returns>
public static string ToMetric(this int input, bool hasSpace = false, bool useSymbol = true, int? decimals = null)
public static string ToMetric(this int input, bool hasSpace = false, bool useSymbol = true, int? decimals = null, char? largestPrefix = null)
{
return ((double)input).ToMetric(hasSpace, useSymbol, decimals);
return ((double)input).ToMetric(hasSpace, useSymbol, decimals, largestPrefix);
}

/// <summary>
Expand All @@ -125,15 +127,17 @@ public static string ToMetric(this int input, bool hasSpace = false, bool useSym
/// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
/// <param name="useSymbol">True will use symbol instead of name</param>
/// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
/// <param name="largestPrefix">If not null it is the largest prefix used in result.</param>
/// <example>
/// <code>
/// 1000d.ToMetric() => "1k"
/// 123d.ToMetric() => "123"
/// 1E-1.ToMetric() => "100m"
/// 1_000_000.ToMetric(largestPrefix = 'k') => "1000k"
/// </code>
/// </example>
/// <returns>A valid Metric representation</returns>
public static string ToMetric(this double input, bool hasSpace = false, bool useSymbol = true, int? decimals = null)
public static string ToMetric(this double input, bool hasSpace = false, bool useSymbol = true, int? decimals = null, char? largestPrefix = null)
{
if (input.Equals(0))
{
Expand All @@ -145,7 +149,7 @@ public static string ToMetric(this double input, bool hasSpace = false, bool use
throw new ArgumentOutOfRangeException(nameof(input));
}

return BuildRepresentation(input, hasSpace, useSymbol, decimals);
return BuildRepresentation(input, hasSpace, useSymbol, decimals, largestPrefix);
}

/// <summary>
Expand Down Expand Up @@ -217,13 +221,14 @@ private static string ReplaceNameBySymbol(string input)
/// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
/// <param name="useSymbol">True will use symbol instead of name</param>
/// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
/// <param name="largestPrefix">If not null it is the largest prefix used in result.</param>
/// <returns>A number in a Metric representation</returns>
private static string BuildRepresentation(double input, bool hasSpace, bool useSymbol, int? decimals)
private static string BuildRepresentation(double input, bool hasSpace, bool useSymbol, int? decimals, char? largestPrefix = null)
{
var exponent = (int)Math.Floor(Math.Log10(Math.Abs(input)) / 3);
return exponent.Equals(0)
? input.ToString()
: BuildMetricRepresentation(input, exponent, hasSpace, useSymbol, decimals);
: BuildMetricRepresentation(input, exponent, hasSpace, useSymbol, decimals, largestPrefix);
}

/// <summary>
Expand All @@ -234,9 +239,13 @@ private static string BuildRepresentation(double input, bool hasSpace, bool useS
/// <param name="hasSpace">True will split the number and the symbol with a whitespace.</param>
/// <param name="useSymbol">True will use symbol instead of name</param>
/// <param name="decimals">If not null it is the numbers of decimals to round the number to</param>
/// <param name="largestPrefix">If not null it is the largest prefix used in result.</param>
/// <returns>A number in a Metric representation</returns>
private static string BuildMetricRepresentation(double input, int exponent, bool hasSpace, bool useSymbol, int? decimals)
private static string BuildMetricRepresentation(double input, int exponent, bool hasSpace, bool useSymbol, int? decimals, char? largestPrefix = null)
{
if (largestPrefix != null)
exponent = LimitExponent(exponent, (char)largestPrefix);

var number = input * Math.Pow(1000, -exponent);
if (decimals.HasValue)
{
Expand All @@ -251,6 +260,18 @@ private static string BuildMetricRepresentation(double input, int exponent, bool
+ GetUnit(symbol, useSymbol);
}

// TODO docs
private static int LimitExponent(int exponent, char largestPrefix)
{
// TODO largestPrefix = largestPrefix.Trim(); When changed to string

if (!(Symbols[0].Contains(largestPrefix) || Symbols[1].Contains(largestPrefix)))
throw new ArgumentException("Empty or invalid Metric prefix character.", nameof(largestPrefix)); // TODO change to "string".

return exponent; // TODO
}


/// <summary>
/// Get the unit from a symbol of from the symbol's name.
/// </summary>
Expand All @@ -265,7 +286,7 @@ private static string GetUnit(char symbol, bool useSymbol)
/// <summary>
/// Check if a Metric representation is out of the valid range.
/// </summary>
/// <param name="input">A Metric representation who might be out of the valid range.</param>
/// <param name="input">A Metric representation that may be out of the valid range.</param>
/// <returns>True if input is out of the valid range.</returns>
private static bool IsOutOfRange(this double input)
{
Expand All @@ -283,7 +304,7 @@ private static bool IsOutOfRange(this double input)
/// <remarks>
/// ToDo: Performance: Use (string input, out number) to escape the double use of Parse()
/// </remarks>
/// <param name="input">A string who might contain a invalid Metric representation.</param>
/// <param name="input">A string that may contain an invalid Metric representation.</param>
/// <returns>True if input is not a valid Metric representation.</returns>
private static bool IsInvalidMetricNumeral(this string input)
{
Expand All @@ -292,5 +313,10 @@ private static bool IsInvalidMetricNumeral(this string input)
var isSymbol = Symbols[0].Contains(last) || Symbols[1].Contains(last);
return !double.TryParse(isSymbol ? input.Remove(index) : input, out var number);
}

private static bool IsInvalidMetricPrefix(this string input)
{
return false; //TODO
}
}
}

0 comments on commit 9ba6fc1

Please sign in to comment.