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

Мифтахов Валерий #131

Open
wants to merge 8 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
125 changes: 118 additions & 7 deletions phone-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,144 @@ exports.isStar = true;
/**
* Телефонная книга
*/
var phoneBook;
let phoneBook = [];

/**
* Добавление записи в телефонную книгу
* @param {String} phone
* @param {String} name
* @param {String} email
*/

exports.add = function (phone, name, email) {
let re = /^\d{10}$/;
if (!re.test(phone) || name === '' || typeof (name) === 'undefined') {
return false;
}
for (let phoneNumber of phoneBook) {
if (phoneNumber.phone === phone) {
return false;
}
}
phoneBook.push({ phone, name, email });

return true;
};

/**
/*
* Обновление записи в телефонной книге
* @param {String} phone
* @param {String} name
* @param {String} email
*/

exports.update = function (phone, name, email) {
for (let i = 0; i < phoneBook.length; i++) {
if (name === null || name === '') {
return false;
}

if (phoneBook[i].phone === phone && name !== undefined) {
phoneBook[i].name = name;
phoneBook[i].email = email;

return true;
}

if (phoneBook[i].phone === phone && name === undefined) {
phoneBook[i].email = email;

return true;
}
}
};

/**
/*
* Удаление записей по запросу из телефонной книги
* @param {String} query
*/
exports.findAndRemove = function (query) {
exports.findAndRemove = function (query) { // eslint-disable-line complexity
let iteration = 0;
if (query === '') {
return 0;
}
if (query === '*') {
for (let i = 0; i < phoneBook.length; i++) {
phoneBook.splice(i, 1);
i--;
iteration++;
}

return iteration;
}
for (let i = 0; i < phoneBook.length; i++) {
if (phoneBook[i].name.indexOf(query) !== -1 ||
phoneBook[i].phone.indexOf(query) !== -1 ||
(phoneBook[i].email !== undefined) && phoneBook[i].email.indexOf(query) !== -1) {
phoneBook.splice(i, 1);
i--;
iteration++;
}
}

return iteration;
};

/**
/*
* Поиск записей по запросу в телефонной книге
* @param {String} query
*/
exports.find = function (query) {
function formContact(phone, name, email) {
let formOfContact = [];
formOfContact.name = name;
formOfContact.phone = '+7 (' + phone.slice(0, 3) + ') ' + phone.slice(3, 6) + '-' +
phone.slice(6, 8) + '-' + phone.slice(8, 10);
formOfContact.email = email;
if (email === undefined) {
formOfContact.email = '';
let str = formOfContact.name + ', ' + formOfContact.phone;

return str;
}
let str = formOfContact.name + ', ' + formOfContact.phone + ', ' + formOfContact.email;

return str;
}

exports.find = function (query) { // eslint-disable-line max-statements
let findResult = [];
if (query === '') {
return findResult;
}
if (query === '*') {
for (let i = 0; i < phoneBook.length; i++) {
let phone = phoneBook[i].phone;
let name = phoneBook[i].name;
let email = phoneBook[i].email;
findResult[i] = formContact(phone, name, email);
}
} else {
for (let i = 0; i < phoneBook.length; i++) {
if (test(query, phoneBook[i].name, // eslint-disable-line max-depth
phoneBook[i].phone, phoneBook[i].email)) {
let phone = phoneBook[i].phone;
let name = phoneBook[i].name;
let email = phoneBook[i].email;
findResult[i] = formContact(phone, name, email);
}
}
}

return findResult.sort();
};

function test(query, name, phone, email) {
if (name.indexOf(query) !== -1 || phone.indexOf(query) !== -1 ||
(email !== undefined) && email.indexOf(query) !== -1) {
return 1;
}
}

/**
* Импорт записей из csv-формата
* @star
Expand All @@ -57,6 +157,17 @@ exports.importFromCsv = function (csv) {
// Парсим csv
// Добавляем в телефонную книгу
// Либо обновляем, если запись с таким телефоном уже существует
let stringsCSV = csv.split('\n');
let iteration = 0;
for (let i = 0; i < stringsCSV.length; i++) {
let arrayString = stringsCSV[i].split(';');
let name = arrayString[0];
let phone = arrayString[1];
let email = arrayString[2];
if (exports.add(phone, name, email) || exports.update(phone, name, email)) {
iteration++;
}
}

return csv.split('\n').length;
return iteration;
};