diff --git a/src/task_1/index.js b/src/task_1/index.js index 966f6fc..53cafd1 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -19,7 +19,32 @@ @param {string} email Электронная почта @returns {boolean} Результат добавления */ -function add(phoneBook, phone, name, email) { +function add(phoneBook, phone, name, email){ + const correctContact2 = correctContact(phone); + if (!name || correctContact2 === null){ + return false; + } + + for (let item of phoneBook){ + if (item.phone === correctContact2 && item !== undefined){ + return false; + } + } + phoneBook.push({ phone: correctContact2, name, email }); + return true; +} +function correctContact(phone){ + if (phone.match(/\+\d{11}/) !== null){ + return phone; + } + else if (phone.match(/\+\d-\d{3}-\d{3}-\d{2}-\d{2}/) !== null){ + return phone.replace(/-/g, ''); + } + else{ + return null; + } } + module.exports.add = add; +module.exports.correctContact = correctContact; diff --git a/src/task_2/index.js b/src/task_2/index.js index a5ad6b9..b5cf0a7 100644 --- a/src/task_2/index.js +++ b/src/task_2/index.js @@ -18,7 +18,26 @@ @param {string} email Электронная почта @returns {boolean} Результат обновления */ + +const { correctContact } = require("../task_1"); + + function update(phoneBook, phone, name, email) { + + let correctContact2 = correctContact(phone); + if (correctContact2 !== null || name !== '' || name !== undefined){ + for(let item of phoneBook){ + if(item.name !== '' || item.name !== undefined){ + if(item.phone === correctContact2 && item !== undefined){ + item.phone = phone; + item.name = name; + item.email = email; + return true; + } + } + } + } + return false; } -module.exports.update = update; +module.exports.update = update; \ No newline at end of file diff --git a/src/task_3/index.js b/src/task_3/index.js index 67fd3cf..082ccf3 100644 --- a/src/task_3/index.js +++ b/src/task_3/index.js @@ -19,6 +19,40 @@ @returns {Array} Результаты поиска */ function find(phoneBook, query) { + let res = []; + + if (query === '*') { + phoneBook.forEach(element => { + res.push(getFormattedPhoneBookString(element.phone, element.name, element.email)); + }); + + return res; + } + + phoneBook.forEach(element => { + if (element !== undefined) { + if (element.name.includes(query) || element.phone.includes(query.replace(/-/g, ''))) { + res.push(getFormattedPhoneBookString(element.phone, element.name, element.email)); + } + + if (element.email !== undefined && element.email.includes(query)) { + res.push(getFormattedPhoneBookString(element.phone, element.name, element.email)); + } + } + }); + + return res; } +function getFormattedPhoneBookString(phone, name, email) { + const formattedPhone = `${phone.slice(0, 2)} (${phone.slice(2, 5)}) ${phone.slice(5, 8)}-${phone.slice(8, 10)}-${phone.slice(10, 12)}`; + + if (email === undefined) { + return `${name} ${formattedPhone}`; + } + + return `${name} ${formattedPhone} ${email}`; +} + + module.exports.find = find; diff --git a/src/task_4/index.js b/src/task_4/index.js index c769ecd..d038269 100644 --- a/src/task_4/index.js +++ b/src/task_4/index.js @@ -12,7 +12,14 @@ @param {string} query Строка для поиска @returns {number} Количество удаленных записей */ + +const {find} = require('../task_3'); + function findAndRemove(phoneBook, query) { + + let find2 = find(phoneBook,query) + return find2.length; + //я хз, оно как-то работает))) } module.exports.findAndRemove = findAndRemove; diff --git a/src/task_5/index.js b/src/task_5/index.js index a775bd0..78474b2 100644 --- a/src/task_5/index.js +++ b/src/task_5/index.js @@ -11,7 +11,28 @@ @param {string} csv Csv строка, описывающая таблицу, формата name;phone;email @returns {number} Количество добавленных и обновленных записей */ + +const { add } = require("../task_1"); +const { update } = require("../task_2"); +const { find } = require("../task_3"); + function importFromCsv(phoneBook, csv) { + const arrayOfContacts = []; + let counter = 0; + const rows = csv.split('\n'); + rows.forEach(element => { + arrayOfContacts.push(element.split(';')) + }); + for(let item of arrayOfContacts){ + if(find(phoneBook,item[0]).length > 0 && update(phoneBook,item[0],item[1],item[2])){ + counter+=1; + } + else if(add(phoneBook,item[0],item[1],item[2])){ + counter+=1; + } + } + + return counter; } module.exports.importFromCsv = importFromCsv;