20 lines
1.1 KiB
JavaScript
20 lines
1.1 KiB
JavaScript
|
|
"use strict";
|
||
|
|
|
||
|
|
(function (global) {
|
||
|
|
class TarinaiRegistryBase {
|
||
|
|
constructor(defs = {}) { this.defs = new Map(Object.entries(defs || {})); }
|
||
|
|
key(id = "") { return String(id || ""); }
|
||
|
|
get(id = "") { return this.defs.get(this.key(id)) || null; }
|
||
|
|
has(id = "") { return this.defs.has(this.key(id)); }
|
||
|
|
entries() { return [...this.defs.entries()]; }
|
||
|
|
values() { return [...this.defs.values()]; }
|
||
|
|
label(id = "", fallback = "") { return this.get(id)?.label || fallback || id || ""; }
|
||
|
|
color(id = "", fallback = "rgba(255,242,160,0.85)") { return this.get(id)?.color || fallback; }
|
||
|
|
labels() { return Object.fromEntries(this.entries().map(([id, def]) => [id, def.label])); }
|
||
|
|
colors() { return Object.fromEntries(this.entries().map(([id, def]) => [id, def.color]).filter(([, color]) => color)); }
|
||
|
|
debugSnapshot(mapper = null) {
|
||
|
|
return this.entries().map(([id, def]) => mapper ? mapper(id, def) : ({ id, label: def.label || id }));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
global.TarinaiRegistryBase = TarinaiRegistryBase;
|
||
|
|
})(typeof window !== "undefined" ? window : globalThis);
|