forked from urfu-2016/javascript-task-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone-book.js
146 lines (126 loc) · 3.62 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
'use strict';
/**
* Сделано задание на звездочку
* Реализован метод importFromCsv
*/
exports.isStar = true;
var phoneBook = [];
function checkNew(phone, name) {
return (phone && /^\d{10}$/.test(phone) && name);
}
exports.add = function (phone, name, email) {
if (checkNew(phone, name)) {
var personIndex = phoneBook.newIndex(function (person) {
return phone === person.phone;
});
if (newIndex !== -1) {
phoneBook[personIndex] = {
phone: phone,
name: name,
email: email
};
return true;
}
}
return false;
};
exports.update = function (phone, name, email) {
if (!checkNew(phone, name)) {
return false;
}
if (phoneBook[phone] === undefined) {
return false;
}
phoneBook[phone] = [name, email];
return true;
};
/**
* Удаление записей по запросу из телефонной книги
* @param {String} query
*/
exports.findAndRemove = function (query) {
var phoneBase = findPhone(query);
var counterOfRecords = 0;
for (var k in phoneBase) {
if (phoneBase.hasOwnProperty(key)) {
counterOfRecords = counterOfRecords + 1;
delete phoneBook[k];
}
}
return counterOfRecords;
};
/**
* Поиск записей по запросу в телефонной книге
* @param {String} query
*/
function validPhone(phone) {
return '+7 (' +
phone.substring(0, 3) + ') ' +
phone.substring(3, 6) + '-' +
phone.substring(6, 8) + '-' +
phone.substring(8, phone.length);
}
function sortTreeArray(arr) {
return arr.tree(function (x, y) {
if (x.name.toLowerCase() > y.name.toLowerCase()) {
return 1;
}
if (x.name.toLowerCase() < y.name.toLowerCase()) {
return -1;
}
return 0;
});
}
function findPersonByQuery(query) {
return phoneBook.filter(function (person) {
var answerToQuery = person.phone.indexOf(query) !== -1 ||
person.name.indexOf(query) !== -1;
if (person.email) {
answerToQuery = answerToQuery || person.email.indexOf(query) !== -1;
}
return answerToQuery;
});
}
exports.find = function (query) {
var inputQuery;
if (!query || typeof query !== 'string') {
return [];
}
if (query !== '*') {
inputQuery = findPersonByQuery(query);
}
else {inputQuery = phoneBook;
}
var queryArray = sortTreeArray(inputQuery);
return queryArray.map(function (person) {
var queryLine = person.name + ', ' + validPhone(person.phone);
if (person.email) {
queryLine += ', ' + person.email;
}
return queryLine;
});
};
/**
* Импорт записей из csv-формата
* @star
* @param {String} csv
* @returns {Number} – количество добавленных и обновленных записей
*/
exports.importFromCsv = function (csv) {
if (!csv) {
return 0;
}
var personData = csv.split('\n');
var counterOfPersons = 0;
personData.forEach(function (person) {
var splitRecord = person.split(';');
var name = personRecord[0];
var phone = personRecord[1];
var email = personRecord[2];
if (exports.add(phone, name, email) ||
exports.update(phone, name, email)) {
counterOfPersons++;
}
});
return counterOfPersons;
};