-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphone-book.js
137 lines (109 loc) · 3.08 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
'use strict';
exports.isStar = true;
var phoneBook = [];
exports.add = function (phone, name, email) {
if (isValidName(name) && isValidPhone(phone) &&
arguments.length > 1 &&
String(phoneBook).indexOf(String(phone)) === -1
) {
if (email === undefined) {
phoneBook.push([phone, name]);
} else {
phoneBook.push([phone, name, email]);
}
return true;
}
return false;
};
exports.update = function (phone, name, email) {
var change = false;
phoneBook.forEach((contact, index) => {
if (contact[0] === phone) {
if (name === undefined || name === '') {
name = contact[1];
}
if (email === undefined || name === '') {
phoneBook[index] = [phone, name];
} else {
phoneBook[index] = [phone, name, email];
}
change = true;
}
});
return change;
};
exports.find = function (query) {
var findContacts = [];
if (query === '*') {
findContacts = phoneBook.slice();
} else {
phoneBook.forEach(contact => {
if (String(contact).toLowerCase()
.indexOf(query.toLowerCase()) !== -1) {
findContacts.push(contact);
}
});
}
findContacts.forEach((contact, index) => {
if (contact[2] === undefined) {
findContacts[index] = [contact[1], formatPhone(contact[0])];
} else {
findContacts[index] = [contact[1], formatPhone(contact[0]), contact[2]];
}
});
findContacts.sort((a, b) => {
return a[0] > b[0];
});
var result = [];
findContacts.forEach(contact => {
result.push(contact.join(', '));
});
return result;
};
exports.findAndRemove = function (query) {
var count = 0;
if (query === '*') {
count = phoneBook.length;
phoneBook = [];
return count;
}
phoneBook.forEach((contact, index) => {
if (String(contact).toLowerCase()
.indexOf(query.toLowerCase()) !== -1) {
count++;
phoneBook.slice(index, 1);
}
});
return count;
};
exports.importFromCsv = function (csv) {
var count = 0;
csv.split('\n').forEach(contact => {
let arrNewContact = contact.split(';');
if (exports.find(arrNewContact[1]).length > 0) {
if (exports.update(arrNewContact[1], arrNewContact[0], arrNewContact[2]) ||
exports.add(arrNewContact[1], arrNewContact[0], arrNewContact[2])) {
count++;
}
}
});
return count;
};
function isValidPhone(phone) {
if (String(Number(phone)) === String(phone) && phone.length === 10) {
return true;
}
return false;
}
function isValidName(name) {
if (name === 'Неизвестный') {
return false;
}
return true;
}
function formatPhone(phone) {
return '+7 (' + phone.slice(0, 3) + ') ' +
phone.slice(3, 6) + '-' +
phone.slice(6, 8) + '-' +
phone.slice(8, 10);
}