Last active
August 31, 2020 15:06
-
-
Save eschen42/05380fd59f3e4e249c6879d214bb3a11 to your computer and use it in GitHub Desktop.
A simple associative table for JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Table - A simple associative table for JavaScript | |
// methods: | |
// - size:integer - number of members in the table | |
// - keys:Array - the keys used to look up values in the Table | |
// - values:Array - the values in the table in a numbered array | |
// TODO: | |
// - add slicing | |
// - add delete-by-key | |
function Table() { | |
var t = Object.create(Table.methods) | |
return t | |
} | |
Table.methods = { | |
size: function() { | |
var quid = this | |
//Logger.log("Called Table.size") | |
var size = 0; | |
for (var key in quid) | |
{ | |
//Logger.log("Table.size: For property '%s' hasOwnProperty is '%s'", key, quid.hasOwnProperty(key)); | |
if (quid.hasOwnProperty(key)) size++; | |
} | |
return size; | |
}, // end size | |
keys: function() { | |
var quid = this | |
//Logger.log("Called Table.keys") | |
var result = [] | |
for (var key in quid) | |
{ | |
//Logger.log("Table.keys: For property '%s' hasOwnProperty is '%s'", key, quid.hasOwnProperty(key)); | |
if (quid.hasOwnProperty(key) && key != 0) { | |
//Logger.log("Table.keys: result.push('%s')",key) | |
result.push(key); | |
} | |
} | |
return result; | |
}, // end keys | |
values: function() { | |
var quid = this | |
//Logger.log("Called Table.values") | |
var result = [] | |
for (var key in quid) | |
{ | |
//Logger.log("Table.values: For property '%s' hasOwnProperty is '%s'", key, quid.hasOwnProperty(key)); | |
if (quid.hasOwnProperty(key) && key != 0) { | |
//Logger.log("Table.values: result.push('this[%s]')",key) | |
result.push(this[key]); | |
} | |
} | |
return result; | |
}, // end values | |
} // end Table.methods | |
//* | |
function tableTester() { | |
var atable = new Table() | |
atable["foo"] = "bar" | |
atable["baz"] = "xyzzy" | |
Logger.log("Size of atable is %d", atable.size()) | |
var somekeys = atable.keys() | |
Logger.log("Length of somekeys of atable is %d", somekeys.length) | |
Logger.log("First key of atable is %s", somekeys[0]) | |
Logger.log("atable[First key of atable] is %s", atable[somekeys[0]]) | |
Logger.log("First value of atable.values() is %s", atable.values()[0]) | |
} | |
// */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment