-
Notifications
You must be signed in to change notification settings - Fork 117
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
base: master
Are you sure you want to change the base?
Гафурова Софья #86
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,12 @@ | |
* Сделано задание на звездочку | ||
* Реализован метод importFromCsv | ||
*/ | ||
const isStar = true; | ||
const isStar = false; | ||
|
||
/** | ||
* Телефонная книга | ||
*/ | ||
let phoneBook; | ||
let phoneBook = {}; | ||
|
||
/** | ||
* Добавление записи в телефонную книгу | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно же /^\d{10}$/.test(phone). Повтори регулярки. |
||
const contact = phoneBook[phone]; | ||
|
||
if (!validPhone || !name || contact) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !validPhone || !name || phoneBook[phone] |
||
return false; | ||
} | ||
|
||
phoneBook[phone] = { | ||
phone: phone.toString(), | ||
name, | ||
}; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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)}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)}`; | ||
} | ||
|
||
/** | ||
|
@@ -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 = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
У тебя код написан на звездочку, а здесь false?