webclient/js/Table.js

63 lines
996 B
JavaScript
Raw Normal View History

import of from UnitJS.Fun;
import {compare, of, proj} from UnitJS.Fun;
function Table(itemMaker, sortCriterion) {
var items = {};
return {
dump: dump,
get: get,
getAll: getAll,
insert: insert,
insertAll: insertAll,
remove: remove
};
function dump() {
return items;
}
function get(key) {
return items[key];
}
function getAll(criterion) {
return Object.keys(items)
.map(of(items))
.filter(criterion || function() {return true;})
.sort(compare(proj(sortCriterion)));
}
function insert(key, value) {
items[key] = itemMaker(key, value);
}
function insertAll(newItems) {
for(var key in newItems) {
insert(key, newItems[key]);
}
}
function update(mapper) {
for(var key in items) {
var newValue = mapper(items[key]);
if(newValue != undefined) {
insert(key, newValue);
} else {
remove(key);
}
}
}
function remove(key) {
delete items[key];
}
function restore(dumped) {
items = dumped;
}
}
return {
make: Table
}