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

Гафурова Софья #86

Open
wants to merge 5 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
81 changes: 77 additions & 4 deletions phone-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const isStar = true;
/**
* Телефонная книга
*/
let phoneBook;
let phoneBook = {};

/**
* Добавление записи в телефонную книгу
Expand All @@ -18,8 +18,21 @@ let phoneBook;
* @param {String?} email
* @returns {Boolean}
*/
function add(phone, name, email) {
function add(phone, name = '', email = '') {
const validPhone = /^(\d)\1\1(\d)\2\2(\d)\3(\d)\4$/.test(phone);
Copy link

Choose a reason for hiding this comment

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

Можно же /^\d{10}$/.test(phone). Повтори регулярки.

const contact = phoneBook[phone];

if (!validPhone || !name || contact) {
Copy link

Choose a reason for hiding this comment

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

!validPhone || !name || phoneBook[phone]
Можно и не выносить в переменную

return false;
}

phoneBook[phone] = {
phone: phone.toString(),
name,
email
};

return true;
}

/**
Expand All @@ -29,8 +42,17 @@ function add(phone, name, email) {
* @param {String?} email
* @returns {Boolean}
*/
function update(phone, name, email) {
function update(phone = '', name = '', email = '') {
const contact = phoneBook[phone];

if (!contact || !name) {
return false;
}

contact.name = name;
contact.email = email;

return true;
}

/**
Expand All @@ -39,7 +61,10 @@ function update(phone, name, email) {
* @returns {Number}
*/
function findAndRemove(query) {
const result = findAll(query);
result.forEach(r => delete phoneBook[r.phone]);

return result.length;
}

/**
Expand All @@ -48,7 +73,44 @@ function findAndRemove(query) {
* @returns {String[]}
*/
function find(query) {
const result = findAll(query);

return result.map(c => {
let r = `${c.name}, ${toFullPhone(c.phone)}`;
Copy link

Choose a reason for hiding this comment

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

Переименуй r и c на что-то информативное.


if (c.email) {
r += `, ${c.email}`;
}

return r;
});
}

function findAll(query) {

if (!query.trim() || typeof query !== 'string') {
return [];
}

let allContacts = Object.keys(phoneBook).map(p => phoneBook[p]);

if (query !== '*') {
allContacts = allContacts.filter(c =>
c.name.indexOf(query) !== -1 ||
c.phone.indexOf(query) !== -1 ||
c.email.indexOf(query) !== -1
);
}

allContacts.sort((a, b) => b.name > a.name ? -1 : 1);

return allContacts;
}

function toFullPhone(phone) {
Copy link

Choose a reason for hiding this comment

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

formatPhone?

const code = phone.substr(0, 3);

return `+7 (${code}) ${phone.substr(3, 3)}-${phone.substr(6, 2)}-${phone.substr(8, 2)}`;
}

/**
Expand All @@ -61,8 +123,19 @@ function importFromCsv(csv) {
// Парсим csv
// Добавляем в телефонную книгу
// Либо обновляем, если запись с таким телефоном уже существует
csv.split('\n')
.forEach(str => {
const contactArray = str.split(';');
const obj = {
name: contactArray[0],
phone: contactArray[1],
email: contactArray[2]
};

phoneBook[obj.phone] = obj;
});

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

module.exports = {
Expand Down