fuzzyGet to get bestMatch and value in ifLet at the same time

This commit is contained in:
2024-12-05 21:04:11 -06:00
parent 9619563210
commit a1cdf75140
4 changed files with 40 additions and 1 deletions

View File

@@ -10,6 +10,11 @@ typedef MapInfo = {
matches:Map<String,Dynamic>
};
enum FuzzyGetResult<T> {
Found(realKey:String, value:T, score:Float);
NotFound;
}
class FuzzyMapTools {
static var serializingMaps = new Map<StringMap<Dynamic>, MapInfo>();
@@ -63,6 +68,15 @@ class FuzzyMapTools {
return bestKey;
}
public static function fuzzyGet<T>(map:FuzzyMap<T>, fuzzySearchKey:String): FuzzyGetResult<T> {
return switch (bestMatch(map, fuzzySearchKey, false)) {
case null:
NotFound;
case key:
Found(key, map[key], fuzzyMatchScore(key, fuzzySearchKey));
};
}
@:allow(kiss.FuzzyMap)
static function onMatchMade(m:StringMap<Dynamic>, key:String, value:Dynamic) {
#if ((sys || hxnodejs) && !frontend)

View File

@@ -143,7 +143,8 @@ class Kiss {
"walkDirectory" => Symbol("Prelude.walkDirectory"),
"purgeDirectory" => Symbol("Prelude.purgeDirectory"),
"getTarget" => Symbol("Prelude.getTarget"),
// These work with (apply) because they are added as "opAliases" in Macros.kiss:
"fuzzyGet" => Symbol("kiss.FuzzyMapTools.fuzzyGet"),
// These work with (apply) because they are added as "opAliases" in Macros.hx:
"min" => Symbol("Prelude.min"),
"max" => Symbol("Prelude.max"),
"iHalf" => Symbol("Prelude.iHalf"),

View File

@@ -0,0 +1,19 @@
package test.cases;
import utest.Test;
import utest.Assert;
import kiss.Prelude;
import kiss.List;
import kiss.Stream;
import haxe.ds.Option;
import kiss.Kiss;
import kiss.FuzzyMapTools;
using StringTools;
@:build(kiss.Kiss.build())
class FuzzyMapTestCase extends Test {
function testFuzzyGet() {
_testFuzzyGet();
}
}

View File

@@ -0,0 +1,5 @@
(function _testFuzzyGet []
(let [:kiss.FuzzyMap<String> m [=>"glurg" "burgle"]]
(assertLet [(Found "glurg" "burgle" _) (fuzzyGet m "gurg")
NotFound (fuzzyGet m "totally different string")]
(Assert.pass))))