hexicon 0.1.0

Hexicon, for writing interactive fiction: builds a game, plays it in a terminal, tests it, documents it, and serves editors
// The reference's search: what matches the words typed, from the index
// of names search-index.js sets (SEARCH: each a name, its kind, what it
// is part of or says, and its page). A name that is the words comes
// first, then one that starts with them, then one that has them.
(function () {
  var input = document.getElementById("query");
  var list = document.getElementById("results");
  var count = document.getElementById("count");
  var most = 300;

  function show(words) {
    var q = words.trim().toLowerCase();
    list.textContent = "";
    if (!q) {
      count.textContent = "";
      return;
    }
    var found = [];
    for (var i = 0; i < SEARCH.length; i++) {
      var name = SEARCH[i][0].toLowerCase();
      var at = name.indexOf(q);
      if (at < 0) {
        continue;
      }
      var rank = name === q ? 0 : at === 0 ? 1 : 2;
      found.push([rank, name.length, SEARCH[i]]);
    }
    found.sort(function (a, b) {
      return a[0] - b[0] || a[1] - b[1] || (a[2][0] < b[2][0] ? -1 : a[2][0] > b[2][0] ? 1 : 0);
    });
    count.textContent =
      found.length === 0
        ? "Nothing matches."
        : found.length > most
          ? found.length + " match; the first " + most + " are shown."
          : found.length + (found.length === 1 ? " matches." : " match.");
    for (var j = 0; j < found.length && j < most; j++) {
      var entry = found[j][2];
      var item = document.createElement("li");
      var link = document.createElement("a");
      link.href = entry[3];
      link.textContent = entry[0];
      var kind = document.createElement("span");
      kind.className = "kind";
      kind.textContent = " " + entry[1];
      item.appendChild(link);
      item.appendChild(kind);
      if (entry[2]) {
        var what = document.createElement("span");
        what.className = "what";
        what.textContent = " " + entry[2];
        item.appendChild(what);
      }
      list.appendChild(item);
    }
  }

  var asked = new URLSearchParams(window.location.search).get("q") || "";
  input.value = asked;
  show(asked);
  input.addEventListener("input", function () {
    show(input.value);
  });
})();