forked from urfu-2016/javascript-task-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
126 lines (116 loc) · 3.82 KB
/
test.html
File metadata and controls
126 lines (116 loc) · 3.82 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
119
120
121
122
123
124
125
126
<html>
<head>
<script>
'use strict';
/**
* Сделано задание на звездочку
* Реализован метод importFromCsv
*/
var exports = {};
exports.isStar = true;
/**
* Телефонная книга
*/
var phoneBook = {};
exports.add = function (phone, name, email) {
if (!checkForExistence(phone, name, email) && checkAddArguments(name, phone)) {
if (typeof(email) === 'undefined') {
email = '';
}
phoneBook[phone] = { 'name': name, 'phone': phone, 'email': email };
return true;
}
return false;
};
function checkForExistence(phone) {
return (phone in phoneBook);
}
function checkAddArguments(name, phone) {
return name !== '' && typeof(name) !== 'undefined' &&
typeof(name) !== 'undefined' && name !== null &&
/^\d{10}$/.test(phone) && typeof(phone) !== 'undefined';
}
exports.update = function (phone, name, email) {
if (typeof(name) !== 'undefined' && name !== '' && checkForExistence(phone)) {
if (typeof(email) === 'undefined') {
email = '';
}
phoneBook[phone] = { 'phone': phone, 'email': email };
return true;
}
return false;
};
exports.findAndRemove = function (query) {
var deletedItems = exports.find(query, true);
return deletedItems.length;
};
exports.find = function (query, needToDelete) {
if (typeof(needToDelete) === 'undefined') {
needToDelete = false;
}
if (query.length === 0) {
return [];
}
if (query === '*') {
query = '';
}
var foundItems = [];
Object.keys(phoneBook).forEach(function (item) {
var record = phoneBook[item];
if (!recordContains(record, query)) {
return;
}
if (needToDelete) {
delete phoneBook[item];
}
foundItems.push(record);
});
foundItems.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
var resultStrings = [];
foundItems.forEach(function (record) {
var result = [record.name,
convertPhoneNumberToUnified(record.phone),
record.email]
.filter(function (value) {
return value;
})
.join(', ');
resultStrings.push(result);
});
return resultStrings;
};
function recordContains(record, query) {
return record.phone.includes(query) ||
(typeof(record.email) !== 'undefined' &&
record.email.includes(query)) ||
record.name.includes(query);
}
function convertPhoneNumberToUnified(number) {
return '+7 (' + number.substr(0, 3) + ') ' +
[number.substr(3, 3), number.substr(6, 2), number.substr(8, 2)].join('-');
}
exports.importFromCsv = function (csv) {
var lines = csv.split('\n');
var successesCount = 0;
lines.forEach(function (line) {
var splitted = line.split(';');
var record = { 'name': splitted[0], 'phone': splitted[1], 'email': splitted[2] };
var isResultSuccessfull = false;
if (checkForExistence(record.phone)) {
isResultSuccessfull = exports.update(record.phone, record.name, record.email);
} else {
isResultSuccessfull = exports.add(record.phone, record.name, record.email);
}
if (isResultSuccessfull) {
successesCount += 1;
}
});
return successesCount;
};
</script>
</head>
<body>
</body>
</html>