-
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
Мартовицкая Вероника #109
base: master
Are you sure you want to change the base?
Мартовицкая Вероника #109
Changes from 33 commits
82479ad
ed0b546
cbfc634
f09eb4c
6723851
90b7643
7ac257b
877fc76
d174d3c
9e50221
f014f2f
17076db
a0d467b
b549743
1049d54
f872bab
fb52055
4332b66
da4465e
badf908
4011397
bb8ceb9
ade0c45
1c20dca
813fd16
b69c0ba
0316c9b
6cbfd8c
69dbe50
845661b
eae35db
dca7ec7
37cdc90
8c543f3
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 |
---|---|---|
|
@@ -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}$)/; | ||
|
||
return phone && (tel.test(phone)) && (name !== '') && | ||
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. скобки 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.
|
||
name && (typeof name === 'string'); | ||
} | ||
|
||
/** | ||
* Добавление записи в телефонную книгу | ||
|
@@ -19,6 +33,17 @@ let phoneBook; | |
* @returns {Boolean} | ||
*/ | ||
function add(phone, name, email) { | ||
if (!(phone in phoneBook) && isValidParams(phone, name, email)) { | ||
if (!email || (email === '')) { | ||
phoneBook[phone] = [phone, name]; | ||
} else { | ||
phoneBook[phone] = [phone, name, email]; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
|
||
} | ||
|
||
|
@@ -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 === '')) { | ||
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. скобки лишние |
||
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; | ||
var count = 0; | ||
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. let |
||
for (var phone of Object.keys(phoneBook)) { | ||
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. const |
||
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 || | ||
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. давай извлечем из record все сразу |
||
(record[2] && record[2].includes(query) === true)) { | ||
|
||
return record; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -48,7 +108,44 @@ function findAndRemove(query) { | |
* @returns {String[]} | ||
*/ | ||
function find(query) { | ||
if (query === '' || typeof (query) !== 'string') { | ||
return []; | ||
} | ||
const result = []; | ||
var res; | ||
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. let |
||
for (var phone of Object.keys(phoneBook)) { | ||
if (!phoneBook[phone]) { | ||
return []; | ||
} | ||
res = matchSearch(phoneBook[phone], query); | ||
if (res) { | ||
result.push(res); | ||
} | ||
} | ||
var record; | ||
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. const |
||
record = view(result); | ||
|
||
return record.sort(); | ||
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. как сортируем? |
||
} | ||
|
||
function newNumber(phone) { | ||
return `+7 (${phone.slice(0, 3)}) ${phone.slice(3, 6)}-${phone.slice(6, 8)}-${phone.slice(8)}`; | ||
} | ||
|
||
function view(result) { | ||
var recordSought = []; | ||
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. const |
||
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; | ||
} | ||
|
||
/** | ||
|
@@ -61,8 +158,22 @@ function importFromCsv(csv) { | |
// Парсим csv | ||
// Добавляем в телефонную книгу | ||
// Либо обновляем, если запись с таким телефоном уже существует | ||
if (!csv || csv === '') { | ||
return 0; | ||
} | ||
const newCsv = csv.split('\n'); | ||
var count = 0; | ||
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. let |
||
for (let line of newCsv) { | ||
let contact = line.split(';'); | ||
var phone = contact[1]; | ||
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. const |
||
var name = contact[0]; | ||
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. const |
||
var email = contact[2]; | ||
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. const |
||
if (add(phone, name, email) || update(phone, name, email)) { | ||
count++; | ||
} | ||
} | ||
|
||
return csv.split('\n').length; | ||
return count; | ||
} | ||
|
||
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.
const