fuzzyGet to get bestMatch and value in ifLet at the same time
This commit is contained in:
@@ -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)
|
||||
|
@@ -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"),
|
||||
|
19
src/test/cases/FuzzyMapTestCase.hx
Normal file
19
src/test/cases/FuzzyMapTestCase.hx
Normal 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();
|
||||
}
|
||||
}
|
5
src/test/cases/FuzzyMapTestCase.kiss
Normal file
5
src/test/cases/FuzzyMapTestCase.kiss
Normal 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))))
|
Reference in New Issue
Block a user