webclient/js/Table.js

54 lines
859 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) {
items = Save.get(path) || {};
}
function remove(key) {
delete items[key];
}
function save(path) {
Save.set(path, items);
}
}
return {
make: Table
}