@hackoregon/mock-wrapper
Version:
A simple way to wrap a project as if it is in a year package
80 lines (68 loc) • 2.6 kB
JavaScript
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Registry = function () {
function Registry(entries) {
_classCallCheck(this, Registry);
this.entries = entries.slice();
this.validateEntries();
}
_createClass(Registry, [{
key: "validateEntries",
value: function validateEntries() {
var _this = this;
var duplicates = [];
var slugs = new Set();
this.entries.forEach(function (entry) {
if (slugs.has(entry.slug)) {
duplicates.push(entry.slug);
}
slugs.add(entry.slug);
});
var errors = [];
duplicates.forEach(function (duplicate) {
var labels = _this.entries.filter(function (entry) {
return entry.slug === duplicate;
}).map(function (entry) {
return "".concat(entry.slug, "\n\t(").concat(entry.component.displayName, ") in ").concat(entry.project);
});
errors.push(labels.join("\n"));
});
if (errors.length) {
throw new Error("Duplicate slugs found. All card slugs must be unique\n\n".concat(errors.join("\n\n"), "\n"));
}
}
}, {
key: "add",
value: function add(entry) {
this.entries.push(entry);
this.validateEntries();
}
}, {
key: "remove",
value: function remove(entryOrSlug) {
var toRemove = typeof entryOrSlug === "string" ? this.entries.find(function (entry) {
return entry.slug === entryOrSlug;
}) : this.entries.find(function (entry) {
return entry === entryOrSlug;
});
if (toRemove) {
this.entries.splice(this.entries.indexOf(toRemove), 1);
}
}
}, {
key: "find",
value: function find(slug) {
return this.entries.find(function (entry) {
return entry.slug === slug;
});
}
}, {
key: "length",
get: function get() {
return this.entries.length;
}
}]);
return Registry;
}();
export { Registry as default };