Skip to content

Commit

Permalink
Added StringComparison to extension to handle more specific string co…
Browse files Browse the repository at this point in the history
…mparisons. Updated all usages.

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
  • Loading branch information
WhitWaldo committed Sep 26, 2024
1 parent 78e98c8 commit e819dee
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Dapr.Jobs/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal static class StringExtensions
/// </summary>
/// <param name="value">The string value to evaluate.</param>
/// <param name="possibleValues">The possible values to look for a match within.</param>
/// <returns></returns>
public static bool EndsWithAny(this string value, IReadOnlyList<string> possibleValues) => possibleValues.Any(value.EndsWith);
/// <param name="comparisonType">The type of string comparison to perform.</param>
/// <returns>True if the value ends with any of the possible values; otherwise false.</returns>
public static bool EndsWithAny(this string value, IReadOnlyList<string> possibleValues,
StringComparison comparisonType) => possibleValues.Any(val => value.EndsWith(val, comparisonType));
}
2 changes: 1 addition & 1 deletion src/Dapr.Jobs/Models/DaprJobSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static DaprJobSchedule FromDuration(TimeSpan duration)
public bool IsPrefixedPeriodExpression =>
ExpressionValue.StartsWith('@') &&
(isEveryExpression.IsMatch(ExpressionValue) ||
ExpressionValue.EndsWithAny(acceptablePeriodValues));
ExpressionValue.EndsWithAny(acceptablePeriodValues, StringComparison.InvariantCulture));

/// <summary>
/// Reflects that the schedule represents a fixed point in time.
Expand Down
5 changes: 3 additions & 2 deletions test/Dapr.Jobs.Test/Extensions/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// limitations under the License.
// ------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Dapr.Jobs.Extensions;
using Xunit;
Expand All @@ -31,15 +32,15 @@ public void EndsWithAny_ContainsMatch()
"daily",
"midnight",
"hourly"
});
}, StringComparison.InvariantCulture);
Assert.True(result);
}

[Fact]
public void EndsWithAny_DoesNotContainMatch()
{
const string testValue = "@weekly";
var result = testValue.EndsWithAny(new List<string> { "every", "monthly", "daily", "midnight", "hourly" });
var result = testValue.EndsWithAny(new List<string> { "every", "monthly", "daily", "midnight", "hourly" }, StringComparison.InvariantCulture);
Assert.False(result);
}
}

0 comments on commit e819dee

Please sign in to comment.