Skip to content
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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 124 additions & 2 deletions phone-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

– длина строки не может быть отрицательной
phone <= 0 не лучшая идея сравнивать на строку с числом

return false;
}

return /^\d{10}$/.test(phone);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

длину телефона можно не проверять, т.к. в регулярном выражении ты задаешь фиксированное количество символов

}

function check2(email) {

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно сразу сделать return

return check() || check2();

return false;
}

return true;
}

function checkPhone(phone) {
return phoneBook.some(function (element) {
return (element.phone === phone);
});
}

/**
* Добавление записи в телефонную книгу
Expand All @@ -19,7 +54,17 @@ let phoneBook;
* @returns {Boolean}
*/
function add(phone, name, email) {
if (generalCheck(phone, name, email) === false) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!generalCheck()) {

}

return false;
}
if (checkPhone(phone) === true) {

Choose a reason for hiding this comment

The 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;
}

/**
Expand All @@ -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 => {

Choose a reason for hiding this comment

The 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) + ') ' +

Choose a reason for hiding this comment

The 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]));
}

/**
Expand All @@ -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 = {
Expand Down