2020-01-10 08:36:57 +01:00
|
|
|
import of from UnitJS.Fun;
|
|
|
|
import {compare, of, proj} from UnitJS.Fun;
|
|
|
|
|
|
|
|
function Table(itemMaker, sortCriterion) {
|
|
|
|
var items = {};
|
|
|
|
return {
|
2020-01-13 08:39:32 +01:00
|
|
|
dump: dump,
|
2020-01-10 08:36:57 +01:00
|
|
|
get: get,
|
|
|
|
getAll: getAll,
|
|
|
|
insert: insert,
|
|
|
|
insertAll: insertAll,
|
|
|
|
remove: remove
|
|
|
|
};
|
|
|
|
|
2020-01-13 08:39:32 +01:00
|
|
|
function dump() {
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2020-01-10 08:36:57 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-01-11 19:46:41 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-01-10 08:36:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove(key) {
|
|
|
|
delete items[key];
|
|
|
|
}
|
2020-01-13 08:39:32 +01:00
|
|
|
|
|
|
|
function restore(dumped) {
|
|
|
|
items = dumped;
|
|
|
|
}
|
2020-01-10 08:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
make: Table
|
|
|
|
}
|