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

Корякин Александр #108

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
78 changes: 73 additions & 5 deletions phone-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ const isStar = true;
/**
* Телефонная книга
*/
let phoneBook;
let phoneBook = [];

function checkPhone(phone) {
return typeof(phone) === 'string' && /^[0-9]{10}$/.test(phone);
}

function checkName(name) {
return typeof(name) === 'string' && name.length > 0;
}

function checkAll(phone, name) {
return checkPhone(phone) && checkName(name);
}

function transform(phone) {
return '+7 (' + phone.substring(0, 3) + ') ' + phone.substring(3, 6) +
'-' + phone.substring(6, 8) + '-' + phone.substring(8, 10);
}

/**
* Добавление записи в телефонную книгу
Expand All @@ -19,7 +36,13 @@ let phoneBook;
* @returns {Boolean}
*/
function add(phone, name, email) {
if (checkAll(phone, name) && typeof(phoneBook[phone]) === 'undefined') {
phoneBook[phone] = [name, email];

return true;
}

return false;
}

/**
Expand All @@ -30,7 +53,13 @@ function add(phone, name, email) {
* @returns {Boolean}
*/
function update(phone, name, email) {
if (checkAll(phone, name) && typeof(phoneBook[phone]) !== 'undefined') {
phoneBook[phone] = [name, email];

return true;
}

return false;
}

/**
Expand All @@ -39,7 +68,32 @@ function update(phone, name, email) {
* @returns {Number}
*/
function findAndRemove(query) {
let forRemove = find(query);
for (let i = 0; i < forRemove.length; i++) {
delete phoneBook[forRemove[i].split(', ')[1]];
phoneBook[forRemove[i].split(', ')[1]] = undefined;
}

return forRemove.length;
}

function extraChecking(query, data, key) {
return typeof(query) === 'undefined' || key.indexOf(query) >= 0 ||
data[key][0].indexOf(query) >= 0 ||
typeof(data[key][1]) !== 'undefined' && data[key][1].indexOf(query) >= 0;
}

function getData(data, query) {
let result = [];
const keys = Object.keys(data);
for (let i = 0; i < keys.length; i++) {
if (extraChecking(query, data, keys[i])) {
result.push(data[keys[i]][0] + ', ' + transform(keys[i]) +
(typeof(data[keys[i]][1]) !== 'undefined' ? ', ' + data[keys[i]][1] : ''));
}
}

return result;
}

/**
Expand All @@ -48,21 +102,35 @@ function findAndRemove(query) {
* @returns {String[]}
*/
function find(query) {
let result = [];
if (query === '*') {
result = getData(phoneBook);
} else if (typeof(query) !== 'undefined' && query.trim().length > 0) {
result = getData(phoneBook, query);
}
result.sort();

return result;
}


/**
* Импорт записей из csv-формата
* @star
* @param {String} csv
* @returns {Number} – количество добавленных и обновленных записей
*/
function importFromCsv(csv) {
// Парсим csv
// Добавляем в телефонную книгу
// Либо обновляем, если запись с таким телефоном уже существует
let phones = csv.split('\n');
let count = 0;
for (let i = 0; i < phones.length; i++) {
let data = phones[i].split(';');
if (add(data[1], data[0], data[2]) || update(data[1], data[0], data[2])) {
count++;
}
}

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

module.exports = {
Expand Down