Stackoverflow #62855170
JavaScript
const mailFormat = /^([\w-]\.?)+@([\w-]+\.)+([A-Za-z]{2,4})+$/g;
const notEmpty = (value) => (value === " " ? false : true);
const isEmpty = (value) => !notEmpty(value);
const isNull = (value) => value === null;
const maxLength = (length) => (value) => value.length < length;
class Category {
constructor(name) {
this.name = name;
}
set name(name) {
if (isEmpty(name) || isNull(name))
throw new Error(`category field needs to be filled`);
this._name = name;
}
get name() {
return this._name;
}
}
class CategoryDAO {
constructor() {
this._list = [];
}
add(category) {
if (!(category instanceof Category)) {
throw new Error("the object is not of type category");
}
if (this._list.some((c) => c.name === category.name)) {
throw new Error("category has been created");
}
this._list.push(category);
}
}
try {
const categoryDAO = new CategoryDAO();
const category1 = new Category(" ");
const category2 = new Category("Devops");
categoryDAO.add(category1);
categoryDAO.add(category2);
console.log(`saved ${category1.name}`);
console.log(`saved ${category2.name}`);
} catch (err) {
console.log(`WRG ${err}`);
}