webclient/js/Table.js

53 lines
862 B
JavaScript

import Save;
import of from UnitJS.Fun;
import {compare, of, proj} from UnitJS.Fun;
function Table(sortCriterion) {
var items = {};
return {
get: get,
getAll: getAll,
insert: insert,
insertAll: insertAll,
load: load,
remove: remove,
save
};
function get(key) {
return items[key];
}
function getAll(criterion) {
return Object.keys(items)
.map(function(key) {return {key: key, value: items[key]};})
.filter(criterion || function() {return true;})
.sort(compare(sortCriterion));
}
function insert(key, value) {
items[key] = value;
}
function insertAll(newItems) {
for(var key in newItems) {
insert(key, newItems[key]);
}
}
function load(path) {
insertAll(Save.get(path) || {});
}
function remove(key) {
delete items[key];
}
function save(path) {
Save.set(path, items);
}
}
return {
make: Table
}