Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@
CollectionAssert.AreEqual(new List<string> { "hello", "hello world", "world" }, result);
}

[TestMethod]
[DataRow("hello", "hell", "say hello to everyone in the room")]
[DataRow("abc", "ab", "axxxxxxbxxxxxxc")]
[DataRow("readme", "read", "docs/ReleaseNotes/readme.md")]
[DataRow("config", "conf", "src/app/settings/config.json")]
public void RankPutsARealMatchAboveAHigherScoringNearMiss(string filter, string nearMiss, string match)
{
List<string> result = [.. TextFilter.Rank([nearMiss, match], filter)];
CollectionAssert.AreEqual(new List<string> { match, nearMiss }, result);

Check warning on line 338 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaDkkPY0AduN7hzMaD46&open=AaDkkPY0AduN7hzMaD46&pullRequest=124
}

[TestMethod]
public void RankWithKeySelectorPutsARealMatchAboveAHigherScoringNearMiss()
{
List<(int Id, string Text)> items = [(1, "hell"), (2, "say hello to everyone in the room")];
List<(int Id, string Text)> result = [.. TextFilter.Rank(items, item => item.Text, "hello")];
CollectionAssert.AreEqual(new List<(int, string)> { (2, "say hello to everyone in the room"), (1, "hell") }, result);

Check warning on line 346 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaDkkPY0AduN7hzMaD45&open=AaDkkPY0AduN7hzMaD45&pullRequest=124
}

[TestMethod]
public void RankEmptyStringsReturnsEmpty()
{
Expand Down
5 changes: 3 additions & 2 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(1);

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "SYSLIB1045:Convert to 'GeneratedRegexAttribute'.", Justification = "Not available in older frameworks")]
private static Regex RegexMatchAnything() => new(".*", RegexOptions.Compiled);

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

/// <summary>
/// Gets a hint for the specified filter type.
Expand Down Expand Up @@ -201,7 +201,7 @@
/// <param name="keySelector">A function to extract the string key from an item.</param>
/// <param name="fuzzyFilter">The fuzzy filter pattern.</param>
/// <returns>The collection of items sorted by their match score.</returns>
/// <remarks>Uses fuzzy matching to rank the items by their match score.</remarks>
/// <remarks>Uses fuzzy matching to rank the items by their match score. Items that match the pattern always sort above items that don't, because a short near-miss can outscore a real match inside a longer string.</remarks>
public static IEnumerable<TItem> Rank<TItem>(IEnumerable<TItem> items, Func<TItem, string> keySelector, string fuzzyFilter)
{
Ensure.NotNull(items);
Expand All @@ -213,7 +213,8 @@
bool isMatch = IsMatch(keySelector(item), fuzzyFilter, out int score, TextFilterType.Fuzzy);
return (item, isMatch, score);
})
.OrderByDescending(t => t.score)
.OrderByDescending(t => t.isMatch)
.ThenByDescending(t => t.score)
.Select(t => t.item);
}

Expand Down Expand Up @@ -276,7 +277,7 @@

return ExcludedTokenPrefixes.Contains(prefix)
? TextFilterTokenType.Excluded
: RequiredTokenPrefixes.Contains(prefix)

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.
? TextFilterTokenType.Required
: TextFilterTokenType.Optional;
})
Expand Down
Loading