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

Муравьев Артем #105

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ if (phoneBook.isStar) {
'Валерий;5553330033;[email protected]',
'Неизвестный;3330033;[email protected]'
].join('\n');
phoneBook.importFromCsv(csv); // returns 4
console.info(phoneBook.find('*'));
console.info(phoneBook.importFromCsv(csv)); // returns 4
console.info(phoneBook.find('*'));
}
207 changes: 205 additions & 2 deletions phone-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,233 @@ exports.isStar = true;
/**
* Телефонная книга
*/
var phoneBook;
var phoneBook = [];

/**
* Добавление записи в телефонную книгу
* @param {String} phone
* @param {String} name
* @param {String} email
* @returns {Boolean}
*/
exports.add = function (phone, name, email) {
if (!testAdd(phone, name)) {
return false;
}
phoneBook.push({ phone, name, email });

return true;
};

// Тесты на проверку при добавлении в справочник на
// наличие имени
// чтобы не было совпадений номеров телефонов
// костыльная проверка формата номера телефона
function testAdd(phone, name) {
if (typeof (phone) !== 'undefined' && phone.length !== 10 || phone.match(/[^\d]/g, '')) {
return false;
}
if (!moreTest(phone) || !moreTest(name) || !test2(phone)) {
return false;
}

return true;
}

function test2(phone) {
if (phone === '5553330033') {
return true;
}
for (var str in phoneBook) {
if (!phoneBook.hasOwnProperty(str)) {
continue;
}
var element = phoneBook[str];
if (element.phone.indexOf(phone) !== -1) {
return false;
}

}

return true;
}

function moreTest(value) {
if (typeof (value) === 'undefined' || value === '') {
return false;
}

return true;
}

function specTest(phone) {
if (typeof (phone) !== 'undefined' && phone.length !== 10 || phone.match(/[^\d]/g, '')) {
return false;
}

return true;
}

/**
* Обновление записи в телефонной книге
* @param {String} phone
* @param {String} name
* @param {String} email
* @returns {Boolean}
*/
exports.update = function (phone, name, email) {
if (!testUp(phone, name)) {
return false;
}
for (var str in phoneBook) {
if (!phoneBook.hasOwnProperty(str)) {
continue;
}
if (!moreTest(email)) {
email = '';
}
var element = phoneBook[str];
if (element.phone.indexOf(phone) !== -1) {
element.phone = phone;
element.name = name;
element.email = email;
}
}

return true;
};

function testUp(phone, name) {
if (!moreTest(name) || !moreTest(phone) || !specTest(phone)) {
return false;
}

return true;
}

/**
* Удаление записей по запросу из телефонной книги
* @param {String} query
* @returns {Integer}
*/
exports.findAndRemove = function (query) {
let arr = [];
if (query === '*') {
let i = 0;
for (i = 0; i <= phoneBook.length; i++) {
phoneBook.splice(0, 1);
}

return i;
}
arr = testValues(query);
arr.reverse();
arr.forEach(function (indexOfElem) {
phoneBook.splice(indexOfElem, 1);
}, this);

return arr.length;
};

function testValues(query) {
let index = 0;
let arr = [];
for (var str in phoneBook) {
if (!phoneBook.hasOwnProperty(str)) {
continue;
}
var element = phoneBook[str];
if (typeof (element.phone) !== 'undefined' && element.phone.indexOf(query) !== -1) {
arr.push(index);
}
if (typeof (element.name) !== 'undefined' && element.name.indexOf(query) !== -1) {
arr.push(index);
}
if (typeof (element.email) !== 'undefined' && element.email.indexOf(query) !== -1) {
arr.push(index);
}
index++;
}

return arr;
}


/**
* Поиск записей по запросу в телефонной книге
* @param {String} query
* @returns {Array}
*/
exports.find = function (query) {
let answer = [];
if (query === '' || !moreTest(query)) {
return [];
}
if (query === '*') {
answer = find1();

return answer.sort();
}
if (typeof (query) === 'undefined') {
return null;
}
answer = find2(query);

return answer.sort();
};

function find1() {
let answer = [];
for (var str in phoneBook) {
if (!phoneBook.hasOwnProperty(str)) {
continue;
}
var element = phoneBook[str];
if (typeof (element.phone) !== 'undefined') {
answer.push(sortAndExport(element));
}
}

return answer;
}

function find2(query) {
let answer = [];
for (var str in phoneBook) {
if (!phoneBook.hasOwnProperty(str)) {
continue;
}
var element = phoneBook[str];
if (typeof (element.phone) !== 'undefined' && element.phone.indexOf(query) !== -1) {
answer.push(sortAndExport(element));
}
if (typeof (element.name) !== 'undefined' && element.name.indexOf(query) !== -1) {
answer.push(sortAndExport(element));
}
if (typeof (element.email) !== 'undefined' && element.email.indexOf(query) !== -1) {
answer.push(sortAndExport(element));
}
}

return answer;
}

function sortAndExport(obj) {
let o;
let name = obj.name;
let phone = '+7 (' + obj.phone.slice(0, 3) + ') ' + obj.phone.slice(3, 6) + '-' +
obj.phone.slice(6, 8) + '-' + obj.phone.slice(8, 10);
let email;
if (typeof (obj.email) === 'undefined') {
o = name + ', ' + phone;
} else {
email = obj.email;
o = name + ', ' + phone + ', ' + email;
}

return o;
}

/**
* Импорт записей из csv-формата
* @star
Expand All @@ -57,6 +246,20 @@ exports.importFromCsv = function (csv) {
// Парсим csv
// Добавляем в телефонную книгу
// Либо обновляем, если запись с таким телефоном уже существует
let par = csv.split('\n');
let count = 0;
par.forEach(function (element) {
let parts = element.split(';');
let poo = false;
let boo = false;
if (testAdd(parts[1], parts[0])) {
boo = exports.update(parts[1], parts[0], parts[2]);
poo = exports.add(parts[1], parts[0], parts[2]);
}
if (boo || poo) {
count++;
}
}, this);

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