forked from urfu-2016/javascript-task-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphone-book.js
More file actions
118 lines (102 loc) · 3.13 KB
/
phone-book.js
File metadata and controls
118 lines (102 loc) · 3.13 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict';
/**
* Сделано задание на звездочку
* Реализован метод importFromCsv
*/
exports.isStar = true;
/**
* Телефонная книга
*/
var phoneBook = [];
exports.add = function (phone, name, email) {
if (checkInput(phone, name, email) ||
!(typeof email === 'string' || email === undefined)) {
return false;
}
if (this.find(phone).length) {
return false;
}
var contact = { phone: phone.toString(), name: name, email: email };
phoneBook.push(contact);
return true;
};
function checkInput(phone, name) {
return !name ||
!phone || name === null ||
typeof name !== 'string' || !(/^\d{10}$/.test(phone));
}
exports.update = function (phone, name, email) {
var check = false;
if (checkInput(phone, name) ||
!(typeof email === 'string' || email === undefined)) {
return false;
}
phoneBook.forEach(function (contact) {
if (contact.phone === phone) {
contact.name = name;
contact.email = email;
if (!email) {
contact.email = '';
}
check = true;
}
});
return check;
};
exports.findAndRemove = function (query) {
var deleteCollection = this.find(query);
phoneBook = phoneBook.filter(function (contacts) {
return deleteCollection.indexOf(format(contacts)) === -1;
});
return deleteCollection.length;
};
exports.find = function (query) {
if (!query) {
return [];
}
if (query === '*') {
return phoneBook.map(format).sort();
}
var response = phoneBook.filter(function (contact) {
return contact.phone.toString().indexOf(query) !== -1 ||
contact.name.indexOf(query) !== -1 ||
(contact.email !== undefined && contact.email.indexOf(query) !== -1);
});
return response.map(format).sort();
};
function format(contact) {
var emailFormat = '';
if (contact.email) {
emailFormat += ', ' + contact.email;
}
var phone = contact.phone;
return contact.name + ', ' +
'+7 (' + phone.toString().slice(0, 3) + ') ' +
phone.toString().slice(3, 6) + '-' +
phone.toString().slice(6, 8) + '-' +
phone.toString().slice(8, 10) +
emailFormat;
}
/**
* Импорт записей из csv-формата
* @star
* @param {String} csv
* @returns {Number} – количество добавленных и обновленных записей
*/
exports.importFromCsv = function (csv) {
var contacts = csv.split('\n');
var firstCount = phoneBook.length;
contacts.forEach(function (contact) {
var newContact = contact.split(';');
if (newContact.length === 2 || newContact.length === 3) {
if (exports.find(newContact[1]).length &&
newContact[1].length === 10) {
exports.update(newContact[1], newContact[0], newContact[2]);
firstCount --;
} else {
exports.add(newContact[1], newContact[0], newContact[2]);
}
}
});
return phoneBook.length - firstCount;
};