-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Лопатин Николай #93
base: master
Are you sure you want to change the base?
Лопатин Николай #93
Changes from all commits
706c380
9ec7afc
65d7171
4ee4431
93663b2
c55cf05
4869afc
70c43ee
5f55706
9477dd7
8374942
3b0a94c
6257f48
0822a48
a47065b
70efbba
4aeab3a
a2e08ec
a0432fe
3ef7da6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,42 @@ const isStar = true; | |
/** | ||
* Телефонная книга | ||
*/ | ||
let phoneBook; | ||
let phoneBook = []; | ||
|
||
|
||
function check(phone, name) { | ||
if (typeof phone !== 'string' || typeof name !== 'string' || name.length <= 0 || | ||
phone <= 0) { | ||
return false; | ||
} | ||
|
||
return /^\d{10}$/.test(phone); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. длину телефона можно не проверять, т.к. в регулярном выражении ты задаешь фиксированное количество символов |
||
} | ||
|
||
function check2(email) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай осмысленные названия функциям |
||
if (email !== undefined) { | ||
if (typeof email !== 'string' || email.length === 0) { | ||
return false; | ||
} | ||
} | ||
if (email === undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут лучше поменять местами, и тогда проверка станет проще if (email === undefined) {
return true;
}
return typeof email === 'string' && email.length; |
||
return true; | ||
} | ||
} | ||
|
||
function generalCheck(phone, name, email) { | ||
if (check(phone, name) === false || check2(email) === false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно сразу сделать return check(…) || check2(…); |
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function checkPhone(phone) { | ||
return phoneBook.some(function (element) { | ||
return (element.phone === phone); | ||
}); | ||
} | ||
|
||
/** | ||
* Добавление записи в телефонную книгу | ||
|
@@ -19,7 +54,17 @@ let phoneBook; | |
* @returns {Boolean} | ||
*/ | ||
function add(phone, name, email) { | ||
if (generalCheck(phone, name, email) === false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!generalCheck(…)) {
…
} |
||
return false; | ||
} | ||
if (checkPhone(phone) === true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. аналогично |
||
return false; | ||
} | ||
var entry = { phone: phone, name: name, email: email }; | ||
|
||
phoneBook.push(entry); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -30,25 +75,86 @@ function add(phone, name, email) { | |
* @returns {Boolean} | ||
*/ | ||
function update(phone, name, email) { | ||
var i = false; | ||
if (generalCheck(phone, name, email) === false) { | ||
return false; | ||
} | ||
phoneBook.forEach(element => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. здесь лучше подойдет |
||
if (element.phone === phone) { | ||
element.name = name; | ||
element.email = email; | ||
i = true; | ||
} | ||
}); | ||
|
||
return i; | ||
} | ||
|
||
/** | ||
* Удаление записей по запросу из телефонной книги | ||
* @param {String} query | ||
* @returns {Number} | ||
*/ | ||
|
||
|
||
function findAndRemove(query) { | ||
if (typeof query !== 'string' || query.length === 0) { | ||
return 0; | ||
} | ||
var numberRecords = phoneBook.length; | ||
if (query === '*') { | ||
phoneBook = []; | ||
|
||
return numberRecords; | ||
} | ||
phoneBook = phoneBook.filter(({ name, phone, email }) => | ||
!(name.includes(query) || | ||
phone.includes(query) || | ||
(email ? email.includes(query) : false))); | ||
|
||
return numberRecords - phoneBook.length; | ||
} | ||
|
||
/** | ||
* Поиск записей по запросу в телефонной книге | ||
* @param {String} query | ||
* @returns {String[]} | ||
*/ | ||
|
||
function findStar(query, string, found) { | ||
if (query === '*') { | ||
phoneBook.forEach(element => { | ||
string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + | ||
element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + | ||
'-' + element.phone.slice(8, 10) + | ||
(element.email ? ', ' + element.email : ''); | ||
found.push(string); | ||
|
||
}); | ||
} | ||
} | ||
|
||
function find(query) { | ||
if (typeof query !== 'string' || query.length === 0) { | ||
return []; | ||
} | ||
var string; | ||
var found = []; | ||
phoneBook.forEach(element => { | ||
if (element.phone.includes(query) === true || | ||
element.name.includes(query) === true || | ||
(element.email ? element.email.includes(query) : false)) { | ||
string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше сделать отдельную функцию для форматирования записи |
||
element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + | ||
'-' + element.phone.slice(8, 10) + | ||
(element.email ? ', ' + element.email : ''); | ||
found.push(string); | ||
} | ||
|
||
}); | ||
findStar(query, string, found); | ||
|
||
return found.sort((a, b) => a.split(',')[0].localeCompare(b.split(',')[0])); | ||
} | ||
|
||
/** | ||
|
@@ -58,11 +164,27 @@ function find(query) { | |
* @returns {Number} – количество добавленных и обновленных записей | ||
*/ | ||
function importFromCsv(csv) { | ||
if (typeof csv !== 'string' || csv.length === 0) { | ||
return 0; | ||
} | ||
var line = csv.split('\n'); | ||
var lineAdd = 0; | ||
var lineSplit; | ||
for (var i = 0; i < line.length; i++) { | ||
lineSplit = line[i].split(';'); | ||
if (update(lineSplit[1], lineSplit[0], lineSplit[2]) === true) { | ||
lineAdd++; | ||
} | ||
if (add(lineSplit[1], lineSplit[0], lineSplit[2]) === true) { | ||
lineAdd++; | ||
} | ||
|
||
} | ||
// Парсим csv | ||
// Добавляем в телефонную книгу | ||
// Либо обновляем, если запись с таким телефоном уже существует | ||
|
||
return csv.split('\n').length; | ||
return lineAdd; | ||
} | ||
|
||
module.exports = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
– длина строки не может быть отрицательной
–
phone <= 0
не лучшая идея сравнивать на строку с числом