Skip to content

Commit

Permalink
fix CustomKanaMapping issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kmoroz committed Oct 14, 2024
1 parent 591121f commit 85c1dd4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion WanaKanaShaapu.UnitTest/ToKanaUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using WanaKanaShaapu;

namespace WanaKanaShaapu.UnitTests
{
Expand Down Expand Up @@ -111,9 +112,12 @@ public void ToKana_WhenPassedObsoleteKanaFlagTrue_ReturnsObsoleteKanaString(stri
}

[TestCase("wanakana", "わにBanaに")]
[TestCase("GG", "GG")]
[TestCase(" GG ", " GG ")]
[TestCase("TT", "TT")]
public void ToKana_WhenPassedCustomMapping_ReturnsACorrectString(string input, string expectedOutput)
{
string result = WanaKana.ToKana(input, new DefaultOptions { CustomKanaMapping = new Dictionary<string, string> { { "na", "" }, { "ka", "Bana" } } });
string result = WanaKana.ToKana(input, new DefaultOptions { CustomKanaMapping = new Dictionary<string, string> { { "na", "" }, { "ka", "Bana" }, { "GG", "GG" }, { "TT", "TT" } } });

Assert.AreEqual(result, expectedOutput);
}
Expand Down
3 changes: 2 additions & 1 deletion WanaKanaShaapu/Internal/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ internal static Dictionary<string, Node> CreateCustomTree(DefaultOptions options
{
var treeCopy = TreeBuilder.BuildRomajiToKanaTree();
foreach (var pair in options.CustomKanaMapping)
ChangeNodeData(treeCopy, pair.Key, pair.Value);
ChangeNodeData(treeCopy, pair.Key.ToLower(), pair.Value);
return treeCopy;
}

internal static void ChangeNodeData(Dictionary<string, Node> tree, string key, string value)
{

Node node = tree[key.First().ToString()];
if (key.Length == 1 || !node.Children.Any())
node.Data = value;
Expand Down
2 changes: 1 addition & 1 deletion WanaKanaShaapu/TreeTraverser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static string TraverseTree(string result, string word, Dictionary<string,

if (word.Length == 1)
return node.Data.Length == 0 && WanaKana.IsRomaji(word)
? word : node.Data;
? result += word : node.Data;

if (WanaKana.IsHiragana(word.First().ToString()))
result = node.Data;
Expand Down
2 changes: 1 addition & 1 deletion WanaKanaShaapu/WanaKana.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public static string ToKana(string input, [Optional] DefaultOptions options)
var slicedInput = Utils.SliceInput(input);
foreach(string slice in slicedInput)
{
if (slice.Any(char.IsUpper))
if (slice.Any(char.IsUpper) && !options.CustomKanaMapping.Any(k => slice.Contains(k.Key)))
convertedString += ToKatakana(TreeTraverser.TraverseTree(convertedString, slice.ToLower(), tree, tree, options));
else
convertedString += TreeTraverser.TraverseTree(convertedString, slice, tree, tree, options);
Expand Down

0 comments on commit 85c1dd4

Please sign in to comment.