-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressBook.js
39 lines (37 loc) · 1.23 KB
/
AddressBook.js
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
var AddressBook = function () {
this.addressBookElems = [];
this.counter = 0;
};
AddressBook.prototype.addItem = function (item) {
item.id = this.counter++;
var added = this.addressBookElems.push(item);
return this.addressBookElems[added-1];
};
AddressBook.prototype.updateItem = function (item) {
var itemToUpdate = this.getItem(item.id);
return itemToUpdate ? itemToUpdate = item : null;
};
AddressBook.prototype.getItem = function (itemId) {
var result = this.addressBookElems.filter(function (item) {
if (item.id === itemId) {
return item;
}
});
return result.length > 0 ? result[0] : null;
};
AddressBook.prototype.getItems = function () {
return this.addressBookElems;
};
AddressBook.prototype.getItemsByName = function (itemName) {
var result = this.addressBookElems.filter(function (item) {
if (item.name.indexOf(itemName) !== -1) {
return item;
}
});
return result.length > 0 ? result : [];
};
AddressBook.prototype.deleteItem = function (itemId) {
var itemToDelete = this.getItem(itemId);
return itemToDelete ? this.addressBookElems.splice(this.addressBookElems.indexOf(itemToDelete), 1)[0] : null;
};
module.exports = AddressBook;