Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions src/task_1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@
@returns {boolean} Результат добавления
*/
function add(phoneBook, phone, name, email) {
phoneBook = [];
if (name !== '' && name !== undefined && isPhjneCorrect(phone)){
phone = phone.match(/\d/g).join('');
const entry = { phone: phone, name: name, email: email };
if (!isPhoneBook(phone, phoneBook)){
phoneBook.push(entry);
return true;
}
}
return false;
}

function isPhjneCorrect(phone){
let reg = /^\+7-\d{3}-\d{3}-\d{2}-\d{2}$/;
let reg1 = /^\+7\d{10}$/;
return reg.test(phone) || reg1.test(phone);
}

function isPhoneBook(phoneBook, phone){
for (let entry of phoneBook){
if (entry.phone === phone){
return true;
}
}
return false;
}

module.exports.add = add;
module.exports.isPhjneCorrect = isPhjneCorrect;
14 changes: 14 additions & 0 deletions src/task_2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@
@param {string} email Электронная почта
@returns {boolean} Результат обновления
*/
const { isPhjneCorrect } = require('../../src/task_1/index');
function update(phoneBook, phone, name, email) {
if(name !== undefined && name !== ''){
if (isPhjneCorrect(phone)){
phone = phone.match(/\d/g).join('');
for (let entry of phoneBook){
if (entry.phone === phone){
entry.name = name;
entry.email = email;
return true;
}
}
}
}
return false;
}

module.exports.update = update;
39 changes: 39 additions & 0 deletions src/task_3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@
@returns {Array<string>} Результаты поиска
*/
function find(phoneBook, query) {
let result = [];
let firstBufer;
let secondBufer;
let array;
if (query ==='')
return query;

for (const entry of phoneBook)
{
if (query === '*')
{
secondBufer = entry.phone.replace(/\d/g,'');
array = secondBufer.split('');
entry.phone =`+${array[0]} (${array[1]+array[2]+array[3]}) ${array[4]+array[5]+array[6]}-${array[7]+array[8]}-${array[9]+array[10]}`;
if (entry.email !== undefined)
{
result.push(entry.name+' '+entry.phone+' '+entry.email);
}
else result.push(entry.name+' '+entry.phone);
}
else
{
secondBufer = entry.phone.replace(/\d/g,'');
array = secondBufer.split('');
firstBufer = array.join('');
secondBufer = `+${array[0]}-${array[1]+array[2]+array[3]}-${array[4]+array[5]+array[6]}-${array[7]+array[8]}-${array[9]+array[10]}`;
if (firstBufer.includes(query)||secondBufer.includes(query)||(entry.email !== undefined && entry.email.includes(query))||entry.name.includes(query))
{
entry.phone =`+${array[0]} (${array[1]+array[2]+array[3]}) ${array[4]+array[5]+array[6]}-${array[7]+array[8]}-${array[9]+array[10]}`;
if (entry.email !== undefined)
{
result.push(entry.name+' '+entry.phone+' '+entry.email);
}
else result.push(entry.name+' '+entry.phone);
}
}
}

return result.sort();
}

module.exports.find = find;
2 changes: 1 addition & 1 deletion test/task_1/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { add } = require('../../src/task_1/index');
const { add } = require('GitHub/js-task-2/src/task_1/index');

const phoneBook = [];

Expand Down