forked from urfu-2016/javascript-task-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone-book.js
187 lines (154 loc) · 4.5 KB
/
phone-book.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'use strict';
/**
* Сделано задание на звездочку
* Реализован метод importFromCsv
*/
exports.isStar = true;
/**
* Телефонная книга
*/
var phoneBook = [];
function isPhone(phone) {
var rePhone = /^\d{10}$/;
return (rePhone.test(phone) && phone.length === 10 && (phone[0] === phone[1]) &&
(phone[1] === phone[2]) && (phone[3] === phone[4]) && (phone[4] === phone[5]) &&
(phone[6] === phone[7]) && (phone[8] === phone[9]));
}
function inPhoneBook(phone) {
for (var i = 0; i < phoneBook.length; i++) {
if (phoneBook[i].phone === phone) {
return true;
}
}
return false;
}
function findInNote(query, note) {
var n = note.phone.indexOf(query);
n += note.name.indexOf(query);
n += note.email === undefined ? -1 : note.email.indexOf(query);
return (n > -3);
}
function validName(name) {
return (/\S+/.test(name) && Boolean(name));
}
function validMail(email) {
if (email !== undefined) {
if (email.length === 0 || email === ' ') {
return false;
}
}
return true;
}
/**
* Добавление записи в телефонную книгу
* @param {String} phone
* @param {String} name
* @param {String} email
* @returns {Boolean}
*/
exports.add = function (phone, name, email) {
if (!isPhone(phone) || !validMail(email) || !validName(name) || inPhoneBook(phone)) {
return false;
}
var note = {
phone: phone,
name: name,
email: email
};
phoneBook.push(note);
return true;
};
function boolUpd(phone, name, email) {
return (!inPhoneBook(phone) || !validMail(email) || !validName(name) || !isPhone(phone));
}
/**
* Обновление записи в телефонной книге
* @param {String} phone
* @param {String} name
* @param {String} email
* @returns {Boolean}
*/
exports.update = function (phone, name, email) {
if (boolUpd(phone, name, email)) {
return false;
}
for (var i = 0; i < phoneBook.length; i++) {
if (phoneBook[i].phone === phone) {
phoneBook[i].name = name;
phoneBook[i].email = email;
return true;
}
}
};
/**
* Удаление записей по запросу из телефонной книги
* @param {String} query
* @returns {Number} nStr
*/
exports.findAndRemove = function (query) {
if (!validName(query)) {
return 0;
}
var nStr = 0;
var lenBook = phoneBook.length; // !!!!!!!!!!
var allNotes = (query === '*');
for (var i = lenBook - 1; i > -1; i--) {
if (findInNote(query, phoneBook[i]) || allNotes) {
phoneBook.splice(i, 1);
nStr += 1;
}
}
return nStr;
};
/**
* Поиск записей по запросу в телефонной книге
* @param {String} query
* @returns {Array} allFound
*/
exports.find = function (query) {
if (!validName(query)) {
return [];
}
var allFound = [];
var addnote = (query === '*');
for (var i = 0; i < phoneBook.length; i++) {
if (findInNote(query, phoneBook[i]) || addnote) {
var phone = '+7 (' + phoneBook[i].phone.slice(0, 3) + ') ' +
phoneBook[i].phone.slice(3, 6) + '-' + phoneBook[i].phone.slice(6, 8) + '-' +
phoneBook[i].phone.slice(8);
var strValue = phoneBook[i].name + ', ' + phone;
var endStr = phoneBook[i].email ? ', ' + phoneBook[i].email : '';
strValue += endStr;
allFound.push(strValue);
}
}
allFound.sort();
return allFound;
};
/**
* Импорт записей из csv-формата
* @star
* @param {String} csv
* @returns {Number} – количество добавленных и обновленных записей
*/
exports.importFromCsv = function (csv) {
// Парсим csv
// Добавляем в телефонную книгу
// Либо обновляем, если запись с таким телефоном уже существует
var notesCsv = csv.split('\n');
var name;
var phone;
var email;
var n = 0;
for (var i = 0; i < notesCsv.length; i++) {
name = notesCsv[i].split(';')[0];
phone = notesCsv[i].split(';')[1];
email = notesCsv[i].split(';')[2];
if (!exports.add(phone, name, email)) {
n += exports.update(phone, name, email);
} else {
n += 1;
}
}
return n;
};