From 10409187154500d9ad4822b35f7701b7761bbb4b Mon Sep 17 00:00:00 2001 From: Nizhegorodov Anton Date: Mon, 1 Apr 2024 16:49:43 +0500 Subject: [PATCH 1/6] -m some --- src/task_1/index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/task_1/index.js b/src/task_1/index.js index 966f6fc..cd50350 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -20,6 +20,19 @@ @returns {boolean} Результат добавления */ function add(phoneBook, phone, name, email) { + phoneBook.push(phone, name, email); + if (toString(name).length == 0){ + return false; + } + if (toString(phone) != phone.replace(/\+7\-\d{3}\-\d{3}\-\d{2}\-\d{2}/) || phone.replace(/\+7\d{10}/)){ + return false; + } + + return true; } +function check(phoneInBook, currentPhone){ + +} + module.exports.add = add; From a5e81c67ab2fe2a67a3ebb3ee8f1a405b708cfcc Mon Sep 17 00:00:00 2001 From: Nizhegorodov Anton Date: Mon, 1 Apr 2024 17:01:24 +0500 Subject: [PATCH 2/6] some do --- src/task_1/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/task_1/index.js b/src/task_1/index.js index cd50350..6480d14 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -24,14 +24,16 @@ function add(phoneBook, phone, name, email) { if (toString(name).length == 0){ return false; } - if (toString(phone) != phone.replace(/\+7\-\d{3}\-\d{3}\-\d{2}\-\d{2}/) || phone.replace(/\+7\d{10}/)){ + if (toString(phone) != check(phone).option1 || check(phone).option2){ return false; } return true; } -function check(phoneInBook, currentPhone){ - +function check(phone){ + let option1 = phone.replace(/\+7\-\d{3}\-\d{3}\-\d{2}\-\d{2}/) + let option2 = phone.replace(/\+7\d{10}/) + return(option1,option2) } From 6b0b9526cdbc1f9f6a91e580692632b4257b7453 Mon Sep 17 00:00:00 2001 From: Nizhegorodov Anton Date: Mon, 1 Apr 2024 17:33:03 +0500 Subject: [PATCH 3/6] loading... --- src/task_1/index.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/task_1/index.js b/src/task_1/index.js index 6480d14..7957def 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -21,19 +21,24 @@ */ function add(phoneBook, phone, name, email) { phoneBook.push(phone, name, email); - if (toString(name).length == 0){ - return false; + if (name !== '' && name !== undefined && check(phone) && checkPhone(phoneBook,phone)){ + return true } - if (toString(phone) != check(phone).option1 || check(phone).option2){ - return false; - } - - return true; + return false } function check(phone){ - let option1 = phone.replace(/\+7\-\d{3}\-\d{3}\-\d{2}\-\d{2}/) - let option2 = phone.replace(/\+7\d{10}/) - return(option1,option2) + let option1 = /^\+7\-\d{3}\-\d{3}\-\d{2}\-\d{2}$/; + let option2 = /^\+7\d{10}$/; + return option1.test(phone) || option2.test(phone) +} +function checkPhone(phoneBook,phone){ + phone = phone.replace(/\D/g, ''); + for (let i of phoneBook){ + // i = i.replace(/\D/g, ''); + if(phoneBook[i]===phone){ + return false; + } + } } From 5c05e5aa4b8b0c302ad9ac16d574b9e9bb3411ae Mon Sep 17 00:00:00 2001 From: Nizhegorodov Anton Date: Mon, 1 Apr 2024 17:40:45 +0500 Subject: [PATCH 4/6] -_- --- src/task_1/index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/task_1/index.js b/src/task_1/index.js index 7957def..51821f5 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -20,9 +20,13 @@ @returns {boolean} Результат добавления */ function add(phoneBook, phone, name, email) { - phoneBook.push(phone, name, email); - if (name !== '' && name !== undefined && check(phone) && checkPhone(phoneBook,phone)){ - return true + if (name !== '' && name !== undefined && check(phone)){ + phone = phone.replace(/\D/g, ''); + const entry = { phone: phone, name: name, email: email }; + if (!checkPhone(phone, phoneBook)){ + phoneBook.push(entry); + return true; + } } return false } @@ -32,12 +36,12 @@ function check(phone){ return option1.test(phone) || option2.test(phone) } function checkPhone(phoneBook,phone){ - phone = phone.replace(/\D/g, ''); + // phone = phone.replace(/\D/g, ''); for (let i of phoneBook){ - // i = i.replace(/\D/g, ''); - if(phoneBook[i]===phone){ - return false; + if(phoneBook[phone]===phone){ + return true; } + return false; } } From 9fab9ceef95daec26ee64da9d49f0853d15c11f2 Mon Sep 17 00:00:00 2001 From: Nizhegorodov Anton Date: Mon, 1 Apr 2024 17:59:01 +0500 Subject: [PATCH 5/6] hh --- src/task_1/index.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/task_1/index.js b/src/task_1/index.js index 51821f5..da91abb 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -21,24 +21,22 @@ */ function add(phoneBook, phone, name, email) { if (name !== '' && name !== undefined && check(phone)){ - phone = phone.replace(/\D/g, ''); const entry = { phone: phone, name: name, email: email }; - if (!checkPhone(phone, phoneBook)){ + if (!checkPhone(phoneBook, phone)){ phoneBook.push(entry); return true; } } - return false + return false; } function check(phone){ let option1 = /^\+7\-\d{3}\-\d{3}\-\d{2}\-\d{2}$/; let option2 = /^\+7\d{10}$/; - return option1.test(phone) || option2.test(phone) + return option1.test(phone) || option2.test(phone); } function checkPhone(phoneBook,phone){ - // phone = phone.replace(/\D/g, ''); for (let i of phoneBook){ - if(phoneBook[phone]===phone){ + if (i.phone.replace(/\D/g, '') === phone.replace(/\D/g, '')){ return true; } return false; @@ -47,3 +45,4 @@ function checkPhone(phoneBook,phone){ module.exports.add = add; +module.exports.check = check; From 4221883b0853184d5ceb71608ec439dcac52ff82 Mon Sep 17 00:00:00 2001 From: Nizhegorodov Anton Date: Thu, 11 Apr 2024 21:56:25 +0500 Subject: [PATCH 6/6] =?UTF-8?q?=D0=AF=20=D1=83=D1=81=D1=82=D0=B0=D0=BB,=20?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=83=20=D0=BC=D0=B5=D0=BD=D1=8F=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B8=D0=BB=D0=BE=D1=81=D1=8C...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/task_1/index.js | 40 +++++++++++++++++++++------------------- src/task_2/index.js | 21 ++++++++++++++++++++- src/task_3/index.js | 34 ++++++++++++++++++++++++++++++++++ src/task_4/index.js | 7 +++++++ src/task_5/index.js | 21 +++++++++++++++++++++ 5 files changed, 103 insertions(+), 20 deletions(-) diff --git a/src/task_1/index.js b/src/task_1/index.js index da91abb..53cafd1 100644 --- a/src/task_1/index.js +++ b/src/task_1/index.js @@ -19,30 +19,32 @@ @param {string} email Электронная почта @returns {boolean} Результат добавления */ -function add(phoneBook, phone, name, email) { - if (name !== '' && name !== undefined && check(phone)){ - const entry = { phone: phone, name: name, email: email }; - if (!checkPhone(phoneBook, phone)){ - phoneBook.push(entry); - return true; +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; } } - return false; + phoneBook.push({ phone: correctContact2, name, email }); + return true; } -function check(phone){ - let option1 = /^\+7\-\d{3}\-\d{3}\-\d{2}\-\d{2}$/; - let option2 = /^\+7\d{10}$/; - return option1.test(phone) || option2.test(phone); -} -function checkPhone(phoneBook,phone){ - for (let i of phoneBook){ - if (i.phone.replace(/\D/g, '') === phone.replace(/\D/g, '')){ - return true; - } - return false; +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.check = check; +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;