2021-03-22 12:14:38 +01:00
|
|
|
function Mapper() {
|
2017-09-22 15:57:59 +02:00
|
|
|
var sources = {};
|
|
|
|
|
|
|
|
|
2021-03-22 12:14:38 +01:00
|
|
|
this.forEach = function (callback) {
|
|
|
|
for (var key in sources) {
|
2017-09-22 15:57:59 +02:00
|
|
|
var source = sources[key];
|
|
|
|
|
2021-03-22 12:14:38 +01:00
|
|
|
for (var key2 in source)
|
2017-09-22 15:57:59 +02:00
|
|
|
callback(source[key2]);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-03-22 12:14:38 +01:00
|
|
|
this.get = function (id, source) {
|
2017-09-22 15:57:59 +02:00
|
|
|
var ids = sources[source];
|
2021-03-22 12:14:38 +01:00
|
|
|
if (ids == undefined)
|
2017-09-22 15:57:59 +02:00
|
|
|
return undefined;
|
|
|
|
|
|
|
|
return ids[id];
|
|
|
|
};
|
|
|
|
|
2021-03-22 12:14:38 +01:00
|
|
|
this.remove = function (id, source) {
|
2017-09-22 15:57:59 +02:00
|
|
|
var ids = sources[source];
|
2021-03-22 12:14:38 +01:00
|
|
|
if (ids == undefined)
|
2017-09-22 15:57:59 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
delete ids[id];
|
|
|
|
|
|
|
|
// Check it's empty
|
2021-03-22 12:14:38 +01:00
|
|
|
for (var i in ids) {
|
|
|
|
return false
|
|
|
|
}
|
2017-09-22 15:57:59 +02:00
|
|
|
|
|
|
|
delete sources[source];
|
|
|
|
};
|
|
|
|
|
2021-03-22 12:14:38 +01:00
|
|
|
this.set = function (value, id, source) {
|
|
|
|
if (value == undefined)
|
2017-09-22 15:57:59 +02:00
|
|
|
return this.remove(id, source);
|
|
|
|
|
|
|
|
var ids = sources[source];
|
2021-03-22 12:14:38 +01:00
|
|
|
if (ids == undefined)
|
2017-09-22 15:57:59 +02:00
|
|
|
sources[source] = ids = {};
|
|
|
|
|
|
|
|
ids[id] = value;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-03-22 12:14:38 +01:00
|
|
|
Mapper.prototype.pop = function (id, source) {
|
2017-09-22 15:57:59 +02:00
|
|
|
var value = this.get(id, source);
|
2021-03-22 12:14:38 +01:00
|
|
|
if (value == undefined)
|
2017-09-22 15:57:59 +02:00
|
|
|
return undefined;
|
|
|
|
|
|
|
|
this.remove(id, source);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-03-22 12:14:38 +01:00
|
|
|
module.exports = Mapper;
|