-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement.js
More file actions
65 lines (56 loc) · 1.86 KB
/
management.js
File metadata and controls
65 lines (56 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
let Item = (function() {
function generateSku(name, category) {
let sku = ''
let firstPart = name.split(' ');
if (firstPart[0].length < 3) {
sku = firstPart[0] + firstPart[1].slice(0, (3 - firstPart.length));
} else {
sku = firstPart[0].slice(0, 3);
}
sku += category.slice(0, 2)
return sku;
}
function validateName(name) {
return name.replace(/ /g, '').length >= 5;
}
function validateCategory(category) {
return !category.match(/ /) && category.length >= 5;
}
function validateQuantity(quantity) {
return typeof quantity === 'number' && quantity >= 0 && Number.isInteger(quantity);
}
return {
init(name, category, quantity) {
if (!(validateQuantity(quantity) && validateName(name) &&
validateCategory(category))) {
return {notValid: true}
} else {
this.sku = generateSku(name, category);
this.name = name;
this.category = category;
this.quantity = quantity;
return this;
}
}
}
})();
class ItemManager {
static items = [];
static create(name, category, quantity) {
let item = Object.create(Item).init(name, category, quantity)
if (item.notValid) {
return false;
} else {
ItemManager.items.push(item);
}
}
static update() {}
}
console.log(ItemManager.create('basket ball', 'sports', 0)); // valid item
console.log(ItemManager.create('asd', 'sports', 0));
console.log(ItemManager.create('soccer ball', 'sports', 5)); // valid item
console.log(ItemManager.create('football', 'sports'));
console.log(ItemManager.create('football', 'sports', 3)); // valid item
console.log(ItemManager.create('kitchen pot', 'cooking items', 0));
console.log(ItemManager.create('kitchen pot', 'cooking', 3)); // valid item
console.log(ItemManager.items);