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

Мартовицкая Вероника #109

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
82479ad
пока не все
Veronica2998 Oct 25, 2018
ed0b546
пока не все
Veronica2998 Oct 25, 2018
cbfc634
пока не все
Veronica2998 Oct 25, 2018
f09eb4c
доработочкиÐ
Veronica2998 Oct 25, 2018
6723851
доработочки
Veronica2998 Oct 25, 2018
90b7643
доработочки
Veronica2998 Oct 25, 2018
7ac257b
доработочки
Veronica2998 Oct 25, 2018
877fc76
доработочки
Veronica2998 Oct 25, 2018
d174d3c
что то не так
Veronica2998 Oct 27, 2018
9e50221
доошибочка
Veronica2998 Oct 28, 2018
f014f2f
hello
Veronica2998 Oct 28, 2018
17076db
hello
Veronica2998 Oct 28, 2018
a0d467b
hello
Veronica2998 Oct 28, 2018
b549743
доработочки
Veronica2998 Oct 29, 2018
1049d54
доработочки
Veronica2998 Oct 29, 2018
f872bab
доработочки
Veronica2998 Oct 29, 2018
fb52055
доработочки
Veronica2998 Oct 29, 2018
4332b66
доработочки
Veronica2998 Oct 29, 2018
da4465e
something
Veronica2998 Oct 29, 2018
badf908
something
Veronica2998 Oct 29, 2018
4011397
changes
Veronica2998 Nov 1, 2018
bb8ceb9
changes
Veronica2998 Nov 1, 2018
ade0c45
changes
Veronica2998 Nov 1, 2018
1c20dca
changes
Veronica2998 Nov 1, 2018
813fd16
changes
Veronica2998 Nov 1, 2018
b69c0ba
changes
Veronica2998 Nov 1, 2018
0316c9b
Дороботки
Veronica2998 Nov 5, 2018
6cbfd8c
something
Veronica2998 Nov 5, 2018
69dbe50
hi
Veronica2998 Nov 5, 2018
845661b
забыла
Veronica2998 Nov 5, 2018
eae35db
забыла
Veronica2998 Nov 5, 2018
dca7ec7
забыла
Veronica2998 Nov 5, 2018
37cdc90
забя запуталааась
Veronica2998 Nov 5, 2018
8c543f3
task
Veronica2998 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
115 changes: 113 additions & 2 deletions phone-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ const isStar = true;
/**
* Телефонная книга
*/
let phoneBook;
let phoneBook = {};

/**
* Проверка параметров
* @param {String} phone
* @param {String?} name
* @param {String?} email
* @returns {Boolean}
*/
function isValidParams(phone, name) {
var tel = /(^[0-9]{10}$)/;

Choose a reason for hiding this comment

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

const


return phone && tel.test(phone) &&
name && typeof name === 'string';
}

/**
* Добавление записи в телефонную книгу
Expand All @@ -19,6 +33,17 @@ let phoneBook;
* @returns {Boolean}
*/
function add(phone, name, email) {
if (!(phone in phoneBook) && isValidParams(phone, name, email)) {
if (!email || email === '') {

Choose a reason for hiding this comment

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

'' == false

phoneBook[phone] = [phone, name];
} else {
phoneBook[phone] = [phone, name, email];
}

return true;
}

return false;

}

Expand All @@ -30,16 +55,51 @@ function add(phone, name, email) {
* @returns {Boolean}
*/
function update(phone, name, email) {
if (phoneBook[phone] && isValidParams(phone, name, email)) {
if (!email || email === '') {

Choose a reason for hiding this comment

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

'' == false

phoneBook[phone] = [phone, name];
} else {
phoneBook[phone] = [phone, name, email];
}

return true;
}

return false;
}

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

function findAndRemove(query) {
if (query === '') {
return 0;
}
let res;
let count = 0;
for (const phone of Object.keys(phoneBook)) {
res = matchSearch(phoneBook[phone], query);
if (res) {
delete phoneBook[phone];
count++;
}
}

return count;
}

function matchSearch(record, query) {
if (query === '*') {
return record;
}
if (record[0].includes(query) === true || record[1].includes(query) === true ||

Choose a reason for hiding this comment

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

давай извлечем из record все сразу

(record[2] && record[2].includes(query) === true)) {

return record;
}
}

/**
Expand All @@ -48,7 +108,44 @@ function findAndRemove(query) {
* @returns {String[]}
*/
function find(query) {
if (query === '' || typeof (query) !== 'string') {
return [];
}
const result = [];
let res;
for (var phone of Object.keys(phoneBook)) {
if (!phoneBook[phone]) {
return [];
}
res = matchSearch(phoneBook[phone], query);
if (res) {
result.push(res);
}
}
var record;

Choose a reason for hiding this comment

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

const

record = view(result);

return record.sort();

Choose a reason for hiding this comment

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

как сортируем?

}

function newNumber(phone) {
return `+7 (${phone.slice(0, 3)}) ${phone.slice(3, 6)}-${phone.slice(6, 8)}-${phone.slice(8)}`;
}

function view(result) {
const recordSought = [];
for (let info of result) {
if (!info) {
return [];
}
if (info[2]) {
recordSought.push(info[1] + ', ' + newNumber(info[0]) + ', ' + info[2]);
} else {
recordSought.push(info[1] + ', ' + newNumber(info[0]));
}
}

return recordSought;
}

/**
Expand All @@ -61,8 +158,22 @@ function importFromCsv(csv) {
// Парсим csv
// Добавляем в телефонную книгу
// Либо обновляем, если запись с таким телефоном уже существует
if (!csv || csv === '') {
return 0;
}
const newCsv = csv.split('\n');
let count = 0;
for (let line of newCsv) {
let contact = line.split(';');
var phone = contact[1];

Choose a reason for hiding this comment

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

const

var name = contact[0];

Choose a reason for hiding this comment

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

const

var email = contact[2];

Choose a reason for hiding this comment

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

const

if (add(phone, name, email) || update(phone, name, email)) {
count++;
}
}

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

module.exports = {
Expand Down