built-in FuzzyMap match serializing

This commit is contained in:
2021-12-30 11:27:45 -07:00
parent 89c0c30e98
commit eb4ad546a5
4 changed files with 51 additions and 2 deletions

View File

@@ -51,7 +51,12 @@ abstract FuzzyMap<T>(StringMap<T>) from StringMap<T> to StringMap<T> {
@:arrayAccess
public inline function get(fuzzySearchKey:String):Null<T> {
return this.get(bestMatch(fuzzySearchKey));
var match = bestMatch(fuzzySearchKey);
var value = this.get(match);
if (match != null) {
FuzzyMapTools.onMatchMade(this, fuzzySearchKey, value);
}
return value;
}
public inline function remove(fuzzySearchKey:String):Bool {

View File

@@ -0,0 +1,39 @@
package kiss;
import haxe.Json;
import haxe.ds.StringMap;
typedef MapInfo = {
file:String,
matches:Map<String,Dynamic>
};
/**
* FuzzyMap is highly inefficient, so you may wish to memoize the matches that it makes before
* releasing your project. FuzzyMapTools helps with this
*/
class FuzzyMapTools {
static var serializingMaps = new Map<StringMap<Dynamic>, MapInfo>();
public static function serializeMatches(m:StringMap<Dynamic>, file:String) {
serializingMaps[m] = { file: file, matches: new Map() };
}
@:allow(kiss.FuzzyMap)
static function onMatchMade(m:StringMap<Dynamic>, key:String, value:Dynamic) {
#if (sys || hxnodejs)
if (serializingMaps.exists(m)) {
var info = serializingMaps[m];
info.matches[key] = value;
sys.io.File.saveContent(info.file, Json.stringify(info.matches));
}
#end
}
public static function loadMatches(m:StringMap<Dynamic>, json:String) {
var savedMatches:haxe.DynamicAccess<Dynamic> = Json.parse(json);
for (key => value in savedMatches.keyValueIterator()) {
m.set(key, value);
}
}
}