-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from digirati-co-uk/feature/lvp_helper
Helper for finding LabelValuePairs by language + label
- Loading branch information
Showing
7 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/IIIF/IIIF.Tests/Presentation/V3/LabelValuePairXTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Collections.Generic; | ||
using IIIF.Presentation.V3; | ||
using IIIF.Presentation.V3.Strings; | ||
|
||
namespace IIIF.Tests.Presentation.V3; | ||
|
||
public class LabelValuePairXTests | ||
{ | ||
[Fact] | ||
public void TryGetValue_False_NullList() | ||
{ | ||
List<LabelValuePair> metadata = null; | ||
|
||
var result = metadata.TryGetValue("en", "bar", out var languageMap); | ||
|
||
result.Should().BeFalse(); | ||
languageMap.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void TryGetValue_False_EmptyList() | ||
{ | ||
var metadata = new List<LabelValuePair>(); | ||
|
||
var result = metadata.TryGetValue("en", "bar", out var languageMap); | ||
|
||
result.Should().BeFalse(); | ||
languageMap.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void TryGetValue_True_ReturnsCorrect_IfFound() | ||
{ | ||
var metadata = new List<LabelValuePair> { new("en", "foo", "bar", "baz") }; | ||
var expected = new LanguageMap("en", new[] { "bar", "baz" }); | ||
|
||
var result = metadata.TryGetValue("en", "foo", out var languageMap); | ||
|
||
result.Should().BeTrue(); | ||
languageMap.Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[Fact] | ||
public void TryGetValue_False_IfNotFoundForLanguage() | ||
{ | ||
var metadata = new List<LabelValuePair> { new("en", "foo", "bar", "baz") }; | ||
|
||
var result = metadata.TryGetValue("en", "bar", out var languageMap); | ||
|
||
result.Should().BeFalse(); | ||
languageMap.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void TryGetValue_False_IfLabelForDifferentLanguage() | ||
{ | ||
var metadata = new List<LabelValuePair> { new("en", "foo", "bar", "baz") }; | ||
|
||
var result = metadata.TryGetValue("fr", "bar", out var languageMap); | ||
|
||
result.Should().BeFalse(); | ||
languageMap.Should().BeNull(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using IIIF.Presentation.V3.Strings; | ||
using IIIF.Utils; | ||
|
||
namespace IIIF.Presentation.V3; | ||
|
||
public static class LabelValuePairX | ||
{ | ||
/// <summary> | ||
/// Try get values associated with specified language and label. | ||
/// </summary> | ||
/// <param name="labelValuePairs">Collection of <see cref="labelValuePairs"/> to search</param> | ||
/// <param name="language">Language to get values for</param> | ||
/// <param name="label">Label to get values for</param> | ||
/// <param name="languageMap">Found <see cref="LanguageMap"/></param> | ||
/// <returns>true if matching value found, else false</returns> | ||
public static bool TryGetValue( | ||
this List<LabelValuePair>? labelValuePairs, | ||
string language, | ||
string label, | ||
[NotNullWhen(true)] out LanguageMap? languageMap) | ||
{ | ||
languageMap = null; | ||
if (labelValuePairs.IsNullOrEmpty()) return false; | ||
|
||
foreach (var langValues in labelValuePairs.Where(lvp => lvp.Label.ContainsKey(language))) | ||
{ | ||
if (langValues.Label.Any(l => l.Value.Contains(label))) | ||
{ | ||
languageMap = langValues.Value; | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
namespace IIIF.Presentation.V3.Strings; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters