orbit/js/utils/WorldNames.js

34 lines
1.2 KiB
JavaScript

/**
* WorldNames — casing-safe world-name resolution. Pure (no Phaser),
* Node-tested (dev/world-names.test.mjs).
*
* World state is keyed by the world's CANONICAL name (its discovery
* name — build records, tether labels, the home key). UI paths that
* display names uppercase them (the comms panel's decode shows
* "ALKHAQO"); if that display casing leaks back into the data path
* (the landing handoff), the name-keyed lookups — isBuilt(planet),
* tetherLevelFor(planet) — silently miss, and the world reads as
* "nothing installed / no tether" on top of its own home world.
*
* canonicalPlanetName(name, worlds) → the canonical spelling of a known
* world regardless of the casing `name` arrived in; unknown names pass
* through untouched.
*/
export function canonicalPlanetName(name, worlds) {
const n = String(name ?? '').trim();
if (!n || !Array.isArray(worlds)) return n;
// 1) exact (any casing in the list is accepted, its spelling wins)
for (const w of worlds) {
const s = String(w ?? '').trim();
if (s && s === n) return s;
}
// 2) case-insensitive
const l = n.toLowerCase();
for (const w of worlds) {
const s = String(w ?? '').trim();
if (s && s.toLowerCase() === l) return s;
}
return n;
}