diff --git a/src/task_1/index.js b/src/task_1/index.js index 966f6fc..4a63bcb 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -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; \ No newline at end of file diff --git a/src/task_2/index.js b/src/task_2/index.js index a5ad6b9..746891c 100644 --- a/src/task_2/index.js +++ b/src/task_2/index.js @@ -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; diff --git a/src/task_3/index.js b/src/task_3/index.js index 67fd3cf..9c3a1eb 100644 --- a/src/task_3/index.js +++ b/src/task_3/index.js @@ -19,6 +19,45 @@ @returns {Array} Результаты поиска */ 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; diff --git a/test/task_1/index.test.js b/test/task_1/index.test.js index c08a22a..0a461b3 100644 --- a/test/task_1/index.test.js +++ b/test/task_1/index.test.js @@ -1,4 +1,4 @@ -const { add } = require('../../src/task_1/index'); +const { add } = require('GitHub/js-task-2/src/task_1/index'); const phoneBook = [];