@auttam/easycli
Version:
A quick and easy way of creating cli for your npm package.
68 lines (67 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Collection = void 0;
const config_error_1 = require("../errors/config-error");
class Collection {
constructor() {
this._collection = new Map();
}
get length() {
return this._collection.size;
}
get(key) {
return this._collection.get(key);
}
hasKey(key) {
return this._collection.has(key);
}
find(target, propertyName) {
for (var [key, item] of this._collection) {
if (propertyName && typeof item == 'object' && item[propertyName] == target) {
return item;
}
if (item == target) {
return item;
}
}
}
append(key, item) {
if (!key)
throw new config_error_1.ConfigurationError('Unable to add to collection, key cannot be null or empty');
if (typeof key != 'string' && typeof key != 'symbol')
throw new config_error_1.ConfigurationError('Unable to add to collection, key must be a string or a symbol');
if (this._collection.has(key)) {
throw new config_error_1.ConfigurationError('Unable to add to collection, Key already defined', key);
}
if (this.validate(item) !== false) {
this._collection.set(key, item);
this.itemAdded(item);
}
}
update(key, item) {
if (!key)
throw new config_error_1.ConfigurationError('Unable to add to collection, key cannot be null or empty');
if (typeof key != 'string' && typeof key != 'symbol')
throw new config_error_1.ConfigurationError('Unable to add to collection, key must be a string or a symbol');
if (!this._collection.has(key)) {
throw new config_error_1.ConfigurationError('Unable to update to collection, Key not found', key);
}
if (this.validate(item) !== false) {
this._collection.set(key, item);
this.itemAdded(item);
}
}
clear() {
this._collection.clear();
}
getItems() {
return this._collection.values();
}
getKeys() {
return this._collection.keys();
}
toArray() {
return Array.from(this.getItems());
}
}
exports.Collection = Collection;