Skip to content

Commit

Permalink
Allow global setting of ForceNumericStrictMode (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
yellis committed Apr 23, 2014
1 parent 1e576d5 commit 5438e46
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
25 changes: 25 additions & 0 deletions EllisWeb.Gematria.Tests/Calculator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace EllisWeb.Gematria.Tests
[TestFixture]
public class Calculator_Tests
{
[SetUp]
public void Setup()
{
Calculator.ForceNumericStrictMode = false;
}

[Test]
[ExpectedException(typeof(ArgumentNullException))]
Expand Down Expand Up @@ -91,6 +96,26 @@ public void GetNumericGematriaValue_IllegalOrderOfDigits_ThrowsFormatExceptionIn
Assert.AreEqual(expectedNonStrict, Calculator.GetNumericGematriaValue(pattern));
}

[TestCase("אי", 1010)]
[TestCase("יצ", 10090)]
[TestCase("תכמ", 420040)]
[TestCase("קאי", 101010)]
public void GetNumericGematriaValue_SetForceNumericStrictModeGlobally_StrictModeApplied_OverrideWorks(string pattern, long expectedNonStrict)
{
Calculator.ForceNumericStrictMode = true;
bool wasFormatException = false;
try
{
Calculator.GetNumericGematriaValue(pattern);
}
catch (FormatException)
{
wasFormatException = true;
}
Assert.IsTrue(wasFormatException);
Assert.AreEqual(expectedNonStrict, Calculator.GetNumericGematriaValue(pattern, isStrictMode: false));
}

[TestCase("רחצ", 298)]
public void GetNumericGematriaValue_StrictMode_IllegalOrderOfDigits_NoErrorWhenOnExceptionsList(string pattern, long expected)
{
Expand Down
26 changes: 20 additions & 6 deletions EllisWeb.Gematria/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace EllisWeb.Gematria
/// </summary>
public static class Calculator
{
static Calculator()
{
ForceNumericStrictMode = false; // set the default
}

/// <summary>
/// Calculates the gematria value for all Hebrew letters in the given string.
/// Ignores all characters that are not Hebrew letters.
Expand All @@ -36,6 +41,11 @@ public static long GetGematriaValue(string sourceString, GematriaType gematriaTy
return value;
}

/// <summary>
/// Should strict mode always be used when calculating numeric values (defaults to false). When set to true,
/// </summary>
public static bool ForceNumericStrictMode { get; set; }

private static readonly Dictionary<string, int> KnownNumericValues = new Dictionary<string, int>
{
{"רחצ", 298}
Expand All @@ -49,19 +59,23 @@ public static long GetGematriaValue(string sourceString, GematriaType gematriaTy
/// </summary>
/// <param name="sourceString">The string to evaluate</param>
/// <param name="gematriaType"><see cref="T:EllisWeb.Gematria.GematriaType"/> to use for calculation (defaults to Absolute)</param>
/// <param name="isStrictMode">Should the numeric gematria be evaluated with Strict Mode turned on. Defaults to false. When true
/// This will throw a <see cref="FormatException"/> whenever the numbers at the end of the string that are under 100 (ק)
/// are not included in descending numeric order, and do not appear on the exceptions list.</param>
/// <param name="isStrictMode">
/// Should the numeric gematria be evaluated with Strict Mode turned on. Defaults to the global setting
/// defined in <see cref="ForceNumericStrictMode"/>. When true this will throw a <see cref="FormatException"/> whenever the numbers at
/// the end of the string that are under 100 (ק) are not included in descending numeric order, and do not appear on the exceptions list.
/// </param>
/// <returns>Number equal to the numeric gematria value of the string provided</returns>
/// <remarks>
/// This function will infer the division between thousands-groupings of the number by using the following rule:
/// Evaluate characters one at a time. It is expected that gematria values within a thousands-grouping will always be the same or equal to the previous value.
/// If a value is encountered that is greater than the previous value, it signifies the start of a new thousands-grouping.
/// </remarks>
public static long GetNumericGematriaValue(string sourceString, GematriaType gematriaType = GematriaType.Absolute, bool isStrictMode = false)
public static long GetNumericGematriaValue(string sourceString, GematriaType gematriaType = GematriaType.Absolute, bool? isStrictMode = null)
{
sourceString = sourceString.Trim();
if (isStrictMode && KnownNumericValues.ContainsKey(sourceString))

bool currentStrictMode = isStrictMode ?? ForceNumericStrictMode;
if (currentStrictMode && KnownNumericValues.ContainsKey(sourceString))
{
return KnownNumericValues[sourceString];
}
Expand Down Expand Up @@ -104,7 +118,7 @@ public static long GetNumericGematriaValue(string sourceString, GematriaType gem
foreach (List<int> numberStack in numberStacks)
{
long stackSum = numberStack.Sum();
if (isStrictMode)
if (currentStrictMode)
{
numberStack.Reverse(); // need to reverse the current stack, in order to preserve the order of items being evaluated
foreach (var number in numberStack)
Expand Down

0 comments on commit 5438e46

Please sign in to comment.