I am trying to port an interactive fiction game from inform7 to javascript. A lot of the syntax is easily converted into javascript; some of it can even be optimized after conversion. But the 'Table' feature does not exist natively in javascript, and I can think of many ways to mimic it. This is what the syntax for an inform7 table looks like:
Table of example table
column one column two column three column four
---------- ---------- ------------ ----------
---------- ---------- ------------ ---------
When Play begins:
Choose a blank row from Table of example table;
now column one entry is "text";
now column two entry is 400;
now column three entry is true
now column four entry is [non primitive]
What is the most efficient way to implement this in javascript?
Please keep in mind that I plan on using strict mode, and do not want to even consider the use of 3rd party frameworks/libraries such as JQuery. Only JavaScript as defined in the latest ECMAScript Specs.
Some of my ideas:
using a map:
const tables = new Map();
// to create table
tables.set("wandering monsters", [
new Map([
["monster name","default"]
])
]);
//to get table entry
tables.get("wandering monsters")[0].get("monster name");
using a function:
function table(name, row, key, val, entrylist){
var result;
if (table.data == undefined){
table.data = new Map();
};
switch (arguments.length) {
case 1:
result = table.data.get(name);
break;
case 2:
table.data.set(name, []);
result = table.data.get(name);
break;
case 3:
result = table.data.get(name)[row].get(key);
break;
case 4:
table.data.get(name)[row].set(key, val);
result = table.data.get(name)[row].get(key);
break;
case 5:
table.data.get(name)[row] = new Map(entrylist);
result = table.data.get(name)[row];
break;
default:
result = table.data;
};
return result;
}
im editing on mobile