2020-01-25 10:56:55 +01:00
|
|
|
import Save;
|
2020-01-10 08:36:57 +01:00
|
|
|
import of from UnitJS.Fun;
|
|
|
|
import {compare, of, proj} from UnitJS.Fun;
|
|
|
|
|
2020-01-13 23:08:20 +01:00
|
|
|
function Table(sortCriterion) {
|
2020-01-10 08:36:57 +01:00
|
|
|
var items = {};
|
|
|
|
return {
|
|
|
|
get: get,
|
|
|
|
getAll: getAll,
|
|
|
|
insert: insert,
|
|
|
|
insertAll: insertAll,
|
2020-01-25 10:56:55 +01:00
|
|
|
load: load,
|
|
|
|
remove: remove,
|
|
|
|
save
|
2020-01-10 08:36:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function get(key) {
|
|
|
|
return items[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAll(criterion) {
|
|
|
|
return Object.keys(items)
|
2020-01-13 23:08:20 +01:00
|
|
|
.map(function(key) {return {key: key, value: items[key]};})
|
2020-01-10 08:36:57 +01:00
|
|
|
.filter(criterion || function() {return true;})
|
2020-01-13 23:08:20 +01:00
|
|
|
.sort(compare(sortCriterion));
|
2020-01-10 08:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function insert(key, value) {
|
2020-01-13 23:08:20 +01:00
|
|
|
items[key] = value;
|
2020-01-10 08:36:57 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 19:46:41 +01:00
|
|
|
function insertAll(newItems) {
|
|
|
|
for(var key in newItems) {
|
|
|
|
insert(key, newItems[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 10:56:55 +01:00
|
|
|
function load(path) {
|
|
|
|
insertAll(Save.get(path) || {});
|
|
|
|
}
|
|
|
|
|
2020-01-10 08:36:57 +01:00
|
|
|
function remove(key) {
|
|
|
|
delete items[key];
|
|
|
|
}
|
2020-01-25 10:56:55 +01:00
|
|
|
|
|
|
|
function save(path) {
|
|
|
|
Save.set(path, items);
|
|
|
|
}
|
2020-01-10 08:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
make: Table
|
|
|
|
}
|