regard
Version:
Sugar-interface to access multiple data sources.
44 lines (30 loc) • 808 B
JavaScript
var _ = require('lodash');
exports = module.exports = AbstractManager;
function AbstractManager() {
if (!(this instanceof AbstractManager)) {
return new AbstractManager();
}
this.data = [];
this.index = [];
}
AbstractManager.prototype.add = function (item) {
var key = item.key;
if (!_.isString(key) || _.isEmpty(key)) {
throw new Error('A key was expected');
}
if (this.has(key)) {
//throw new Error('A same item already exists');
_.remove(this.data, function (value) {
return value.key === key;
});
}
this.data.push(item);
this.index.push(key);
return this;
};
AbstractManager.prototype.get = function (key) {
return _.find(this.data, 'key', key);
};
AbstractManager.prototype.has = function (key) {
return _.includes(this.index, key);
};