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

Галиакбаров Эдуард #52

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
d778291
Update phone-book.js
Raseen7 Oct 23, 2018
bf7bf0b
Update phone-book.js
Raseen7 Oct 23, 2018
a57483b
Update phone-book.js
Raseen7 Oct 23, 2018
d9daa68
Update phone-book.js
Raseen7 Oct 23, 2018
eab8924
Update phone-book.js
Raseen7 Oct 23, 2018
21998db
Update phone-book.js
Raseen7 Oct 23, 2018
745ab73
Update phone-book.js
Raseen7 Oct 23, 2018
05a9ec6
Update phone-book.js
Raseen7 Oct 23, 2018
26d342c
Update phone-book.js
Raseen7 Oct 23, 2018
e305a66
Update phone-book.js
Raseen7 Oct 23, 2018
e6a826c
Update phone-book.js
Raseen7 Oct 23, 2018
1bdb6fa
Update phone-book.js
Raseen7 Oct 24, 2018
9d2501d
Update phone-book.js
Raseen7 Oct 24, 2018
84c56e0
Update phone-book.js
Raseen7 Oct 24, 2018
aa7b831
Update phone-book.js
Raseen7 Oct 24, 2018
ef57b6f
Update phone-book.js
Raseen7 Oct 24, 2018
6c1d320
Update phone-book.js
Raseen7 Oct 24, 2018
76b10e4
Update phone-book.js
Raseen7 Oct 24, 2018
a8d5be8
Update phone-book.js
Raseen7 Oct 24, 2018
f7917a2
Update phone-book.js
Raseen7 Oct 24, 2018
486686f
Update phone-book.js
Raseen7 Oct 24, 2018
05f016a
Update phone-book.js
Raseen7 Oct 24, 2018
e34131d
Update phone-book.js
Raseen7 Oct 24, 2018
04d03f4
Update phone-book.js
Raseen7 Oct 24, 2018
70cb2de
Update phone-book.js
Raseen7 Oct 24, 2018
a197316
Update phone-book.js
Raseen7 Oct 24, 2018
c8a6197
Update phone-book.js
Raseen7 Oct 24, 2018
4381f54
Update phone-book.js
Raseen7 Oct 24, 2018
63c9ff6
Update phone-book.js
Raseen7 Oct 24, 2018
db912e6
Update phone-book.js
Raseen7 Oct 24, 2018
5ef3204
Update phone-book.js
Raseen7 Oct 24, 2018
f465e8b
Update phone-book.js
Raseen7 Oct 25, 2018
1de3143
Update phone-book.js
Raseen7 Oct 25, 2018
0a7b8ac
Update phone-book.js
Raseen7 Oct 25, 2018
9d1ceff
Update phone-book.js
Raseen7 Oct 25, 2018
1ef7805
Update phone-book.js
Raseen7 Oct 25, 2018
b0c1607
Update phone-book.js
Raseen7 Oct 25, 2018
42aa6ba
Update phone-book.js
Raseen7 Oct 25, 2018
dc5be1a
Update phone-book.js
Raseen7 Oct 25, 2018
3cfd854
Update phone-book.js
Raseen7 Oct 25, 2018
7c6056e
Update phone-book.js
Raseen7 Oct 25, 2018
6949114
Update phone-book.js
Raseen7 Oct 25, 2018
a1d4a7f
Update phone-book.js
Raseen7 Oct 25, 2018
eda63e9
Update phone-book.js
Raseen7 Oct 25, 2018
04e3bc1
Update phone-book.js
Raseen7 Oct 25, 2018
0a8318f
Update phone-book.js
Raseen7 Oct 25, 2018
66d722c
Update phone-book.js
Raseen7 Oct 25, 2018
84916d2
Update phone-book.js
Raseen7 Oct 25, 2018
b081483
Add files via upload
Raseen7 Nov 11, 2018
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
113 changes: 108 additions & 5 deletions phone-book.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';

/**
* Сделано задание на звездочку
* Реализован метод importFromCsv
*/
const isStar = true;
const isStar = false;

/**
* Телефонная книга
*/
let phoneBook;
let phoneBook = [];

/**
* Добавление записи в телефонную книгу
Expand All @@ -18,8 +16,49 @@ let phoneBook;
* @param {String?} email
* @returns {Boolean}
*/
function isCorrectPhone(phone) {
if (phone !== undefined && typeof(phone) === 'string' && phone.length === 10 &&
/^\d{10}$/.test(phone)) {
return true;
}

return false;
}

function isCorrectName(name) {
if (name !== undefined && typeof(name) === 'string' && name !== '') {
return true;
}

return false;
}

function recordExists(phone) {
for (let i = 0; i < phoneBook.length; i++) {
if (phoneBook[i].phone === phone) {
return true;
}
}

return false;
}

function isAllArgumentsCorrect(phone, name) {
return isCorrectPhone(phone) && isCorrectName(name);
}

function add(phone, name, email) {
if (isAllArgumentsCorrect(phone, name) && !recordExists(phone)) {
phoneBook.push({
phone: phone,
name: name,
email: email
});

return true;
}

return false;
}

/**
Expand All @@ -30,25 +69,78 @@ function add(phone, name, email) {
* @returns {Boolean}
*/
function update(phone, name, email) {
if (email !== undefined) {
email = String(email);
}
if (!isAllArgumentsCorrect(phone, name) && !recordExists(phone)) {
return false;
}
for (let i = 0; i < phoneBook.length; i++) {
if (phoneBook[i].phone === phone) {
phoneBook[i] = {
phone: phone,
name: name,
email: email
};

return true;
}
}
}


/**
* Удаление записей по запросу из телефонной книги
* @param {String} query
* @returns {Number}
*/

function findAndRemove(query) {
if (typeof query !== 'string' || query === '') {
return 0;
}
let findedRecords = find(query);
for (let i = 0; i < findedRecords.length; i++) {
phoneBook.splice(phoneBook.findIndex(element => element === findedRecords[i]), 1);
}

return findedRecords.length;
}

function toFullPhoneForm(phone) {
const fullPhoneForm = '+7 (' + phone.substring(0, 3) + ') ' + phone.substring(3, 6) +
'-' + phone.substring(6, 8) + '-' + phone.substring(8, 10);

return fullPhoneForm;
}

/**
* Поиск записей по запросу в телефонной книге
* @param {String} query
* @returns {String[]}
*/


function find(query) {
if (query === undefined || typeof query !== 'string' || query === '') {
return [];
}
let findedRecords = [];
if (query === '*') {
findedRecords = phoneBook.map(element => element.name + ', ' +
toFullPhoneForm(element.phone) + ((typeof element.email === 'undefined')
? '' : ', ' + element.email));

return findedRecords.sort();
}

findedRecords = phoneBook.filter(element => element.phone.includes(query) ||
element.name.includes(query) || ((typeof element.email === 'undefined')
? false : element.email.includes(query)));

return findedRecords.map(element => element.name + ', ' +
toFullPhoneForm(element.phone) + ((typeof element.email === 'undefined')
? '' : ', ' + element.email)).sort();
}

/**
Expand All @@ -61,8 +153,19 @@ function importFromCsv(csv) {
// Парсим csv
// Добавляем в телефонную книгу
// Либо обновляем, если запись с таким телефоном уже существует
let counter = 0;
csv.split('\n').forEach(element => {
const elements = element.split(';');
let flag = add(elements[0], elements[1], elements[2]);
if (!flag) {
flag = update(elements[0], elements[1], elements[2]);
}
if (flag) {
counter++;
}
});

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

module.exports = {
Expand Down