From 706c380b6e044b69c59217abf1afe6bcbee248a1 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 08:24:04 +0500 Subject: [PATCH 01/20] javascript-task-2 --- phone-book.js | 155 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 153 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 891d7c01..f1b3ce06 100644 --- a/phone-book.js +++ b/phone-book.js @@ -9,7 +9,46 @@ const isStar = true; /** * Телефонная книга */ -let phoneBook; +let phoneBook = []; + +function check(phone, name) { + if (typeof phone !== 'string' || typeof name !== 'string' || name === '' || name.trim === '') { + return false; + } + if (phone.search(/^\d{10}$/) === -1) { + + return false; + } + + return true; +} + +function check2(email) { + if (email !== undefined) { + if (typeof email !== 'string' || email === '' || email.trim === '') { + return false; + } + + return true; + } + + return 1; +} + +function generalCheck(phone, name, email) { + if (check(phone, name) === false || check2(email) === false) { + return false; + } + + + return true; +} + +function checkPhone(phoneBookk, phone) { + return phoneBookk.some(function (element) { + return (element.phone === phone); + }); +} /** * Добавление записи в телефонную книгу @@ -19,7 +58,27 @@ let phoneBook; * @returns {Boolean} */ function add(phone, name, email) { + if (generalCheck(phone, name, email) === false) { + return false; + } + if (checkPhone(phoneBook, phone) === true) { + return false; + } + var entry = { phone: phone, name: name, email: email, + getphone: function () { + return this.phone.toString(); + }, + getname: function () { + return this.name.toString(); + }, + getemail: function () { + return this.email.toString(); + } + }; + phoneBook.push(entry); + + return true; } /** @@ -30,7 +89,17 @@ function add(phone, name, email) { * @returns {Boolean} */ function update(phone, name, email) { + if (generalCheck(phone, name, email) === false) { + return false; + } + phoneBook.forEach(element => { + if (element.phone === phone) { + element.name = name; + element.email = email; + } + }); + return true; } /** @@ -39,7 +108,24 @@ function update(phone, name, email) { * @returns {Number} */ function findAndRemove(query) { + var numberRecords = 0; + phoneBook.forEach(element => { + if (element.email === undefined) { + if (element.phone.includes(query) === true || + element.name.includes(query) === true) { + delete phoneBook[element]; + numberRecords++; + } + } + if (element.email !== undefined) { + if (element.email.includes(query) === true) { + delete phoneBook[element]; + numberRecords++; + } + } + }); + return numberRecords; } /** @@ -47,8 +133,60 @@ function findAndRemove(query) { * @param {String} query * @returns {String[]} */ + +function findMore(query, string, found) { + phoneBook.forEach(element => { + if (element.email !== undefined) { + if (element.email.includes(query) === true || element.phone.includes(query) === true || + element.name.includes(query) === true) { + string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + + String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + + '-' + String(element.phone).slice(8, 10) + ', ' + element.email; + found.push(string); + } + } + + }); +} + +function findStar(query, string, found) { + phoneBook.forEach(element => { + if (element.email !== undefined && query === '*') { + string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + + String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + + '-' + String(element.phone).slice(8, 10) + ', ' + element.email; + found.push(string); + } + if (query === '*' && element.email === undefined) { + string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + + String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + + '-' + String(element.phone).slice(8, 10); + found.push(string); + } + }); +} + function find(query) { + var string; + var found = []; + if (query === '') { + return; + } + phoneBook.forEach(element => { + if (element.email === undefined) { + if (element.phone.includes(query) === true || + element.name.includes(query) === true) { + string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + + String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + + '-' + String(element.phone).slice(8, 10); + found.push(string); + } + } + }); + findStar(query, string, found); + findMore(query, string, found); + return found.sort(); } /** @@ -58,11 +196,24 @@ function find(query) { * @returns {Number} – количество добавленных и обновленных записей */ function importFromCsv(csv) { + var line = csv.split('\n'); + var lineAdd = 0; + var lineSplit; + for (var i = 0; i <= line.length; i++) { + lineSplit = String(line[i]).split(';'); + if (update(lineSplit[1], lineSplit[0], lineSplit[2])) { + lineAdd++; + } + if (add(lineSplit[1], lineSplit[0], lineSplit[2])) { + lineAdd++; + } + + } // Парсим csv // Добавляем в телефонную книгу // Либо обновляем, если запись с таким телефоном уже существует - return csv.split('\n').length; + return lineAdd; } module.exports = { From 9ec7afcc0bc0bf64443dc7a96f347bc91c39d349 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 08:28:42 +0500 Subject: [PATCH 02/20] javascript-task-2.1 --- phone-book.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phone-book.js b/phone-book.js index f1b3ce06..e3a3308d 100644 --- a/phone-book.js +++ b/phone-book.js @@ -92,6 +92,9 @@ function update(phone, name, email) { if (generalCheck(phone, name, email) === false) { return false; } + if (checkPhone(phoneBook, phone) === true) { + return false; + } phoneBook.forEach(element => { if (element.phone === phone) { element.name = name; From 65d71711dc878a88b2efb0d60648835aa8dc6517 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 08:53:29 +0500 Subject: [PATCH 03/20] javascript-task-2.2 --- phone-book.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/phone-book.js b/phone-book.js index e3a3308d..f9a76705 100644 --- a/phone-book.js +++ b/phone-book.js @@ -12,7 +12,7 @@ const isStar = true; let phoneBook = []; function check(phone, name) { - if (typeof phone !== 'string' || typeof name !== 'string' || name === '' || name.trim === '') { + if (typeof phone !== 'string' || typeof name !== 'string' || name.trim === '') { return false; } if (phone.search(/^\d{10}$/) === -1) { @@ -25,14 +25,12 @@ function check(phone, name) { function check2(email) { if (email !== undefined) { - if (typeof email !== 'string' || email === '' || email.trim === '') { + if (typeof email !== 'string' || email.trim === '') { return false; } return true; } - - return 1; } function generalCheck(phone, name, email) { @@ -64,17 +62,7 @@ function add(phone, name, email) { if (checkPhone(phoneBook, phone) === true) { return false; } - var entry = { phone: phone, name: name, email: email, - getphone: function () { - return this.phone.toString(); - }, - getname: function () { - return this.name.toString(); - }, - getemail: function () { - return this.email.toString(); - } - }; + var entry = { phone: phone, name: name, email: email }; phoneBook.push(entry); @@ -92,9 +80,6 @@ function update(phone, name, email) { if (generalCheck(phone, name, email) === false) { return false; } - if (checkPhone(phoneBook, phone) === true) { - return false; - } phoneBook.forEach(element => { if (element.phone === phone) { element.name = name; @@ -110,8 +95,24 @@ function update(phone, name, email) { * @param {String} query * @returns {Number} */ + +function findAndRemoveStar(numberRecords) { + phoneBook.forEach(element => { + delete phoneBook[element]; + numberRecords++; + }); + + return numberRecords; +} + function findAndRemove(query) { + if (query === '') { + return; + } var numberRecords = 0; + if (query === '*') { + return findAndRemoveStar(numberRecords); + } phoneBook.forEach(element => { if (element.email === undefined) { if (element.phone.includes(query) === true || From 4ee44318004afbc9ae1734d55d207fc20ed53c2a Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 09:08:35 +0500 Subject: [PATCH 04/20] javascript-task-2.3 --- phone-book.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/phone-book.js b/phone-book.js index f9a76705..0affd0be 100644 --- a/phone-book.js +++ b/phone-book.js @@ -12,20 +12,17 @@ const isStar = true; let phoneBook = []; function check(phone, name) { - if (typeof phone !== 'string' || typeof name !== 'string' || name.trim === '') { + if (typeof phone !== 'string' || typeof name !== 'string' || name.trim() === '' || + phone.trim() === '') { return false; } - if (phone.search(/^\d{10}$/) === -1) { - return false; - } - - return true; + return /^\d{10}$/.test(phone); } function check2(email) { if (email !== undefined) { - if (typeof email !== 'string' || email.trim === '') { + if (typeof email !== 'string' || email.trim() === '') { return false; } @@ -38,7 +35,6 @@ function generalCheck(phone, name, email) { return false; } - return true; } From 93663b2791f25e50527af75ad24b407b379157ed Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 09:24:13 +0500 Subject: [PATCH 05/20] javascript-task-2.4 --- phone-book.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 0affd0be..402dba0c 100644 --- a/phone-book.js +++ b/phone-book.js @@ -103,7 +103,7 @@ function findAndRemoveStar(numberRecords) { function findAndRemove(query) { if (query === '') { - return; + return []; } var numberRecords = 0; if (query === '*') { @@ -170,7 +170,7 @@ function find(query) { var string; var found = []; if (query === '') { - return; + return []; } phoneBook.forEach(element => { if (element.email === undefined) { From c55cf0506c99f09d5e2944e7b9cbc61472949702 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 09:30:52 +0500 Subject: [PATCH 06/20] javascript-task-2.5 --- phone-book.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/phone-book.js b/phone-book.js index 402dba0c..6e752146 100644 --- a/phone-book.js +++ b/phone-book.js @@ -102,6 +102,9 @@ function findAndRemoveStar(numberRecords) { } function findAndRemove(query) { + if (typeof query !== 'string') { + return []; + } if (query === '') { return []; } @@ -167,6 +170,9 @@ function findStar(query, string, found) { } function find(query) { + if (typeof query !== 'string') { + return []; + } var string; var found = []; if (query === '') { @@ -196,6 +202,9 @@ function find(query) { * @returns {Number} – количество добавленных и обновленных записей */ function importFromCsv(csv) { + if (typeof csv !== 'string') { + return 0; + } var line = csv.split('\n'); var lineAdd = 0; var lineSplit; From 4869afc44df1955db84c5c166852eae7765eb439 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 09:48:53 +0500 Subject: [PATCH 07/20] javascript-task-2.5 --- phone-book.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 6e752146..a1a0ff29 100644 --- a/phone-book.js +++ b/phone-book.js @@ -105,8 +105,8 @@ function findAndRemove(query) { if (typeof query !== 'string') { return []; } - if (query === '') { - return []; + if (query.trim() === '') { + return 0; } var numberRecords = 0; if (query === '*') { From 70c43ee9c66787091311012ce49524b2468d67e1 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 10:00:40 +0500 Subject: [PATCH 08/20] javascript-task-2.6 --- phone-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index a1a0ff29..533c2744 100644 --- a/phone-book.js +++ b/phone-book.js @@ -103,7 +103,7 @@ function findAndRemoveStar(numberRecords) { function findAndRemove(query) { if (typeof query !== 'string') { - return []; + return 0; } if (query.trim() === '') { return 0; From 5f557069655e759f1fc0ca8cf53600bfa6f3d3f6 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 10:23:02 +0500 Subject: [PATCH 09/20] javascript-task-2.7 --- phone-book.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/phone-book.js b/phone-book.js index 533c2744..11979cda 100644 --- a/phone-book.js +++ b/phone-book.js @@ -101,6 +101,21 @@ function findAndRemoveStar(numberRecords) { return numberRecords; } +function findAndRemoveEmail(numberRecords, query) { + phoneBook.forEach(element => { + if (element.email !== undefined) { + if (element.phone.includes(query) === true || + element.name.includes(query) === true || + element.email.includes(query) === true) { + delete phoneBook[element]; + numberRecords++; + } + } + }); + + return numberRecords; +} + function findAndRemove(query) { if (typeof query !== 'string') { return 0; @@ -120,13 +135,8 @@ function findAndRemove(query) { numberRecords++; } } - if (element.email !== undefined) { - if (element.email.includes(query) === true) { - delete phoneBook[element]; - numberRecords++; - } - } }); + numberRecords = numberRecords + findAndRemoveEmail(numberRecords, query); return numberRecords; } @@ -202,7 +212,7 @@ function find(query) { * @returns {Number} – количество добавленных и обновленных записей */ function importFromCsv(csv) { - if (typeof csv !== 'string') { + if (typeof csv !== 'string' || csv.trim() === '') { return 0; } var line = csv.split('\n'); From 9477dd7a42e38aced1c5a7e80e76a22dac39f99a Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 11:27:20 +0500 Subject: [PATCH 10/20] javascript-task-2.8 --- phone-book.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/phone-book.js b/phone-book.js index 11979cda..ebb61388 100644 --- a/phone-book.js +++ b/phone-book.js @@ -11,6 +11,7 @@ const isStar = true; */ let phoneBook = []; + function check(phone, name) { if (typeof phone !== 'string' || typeof name !== 'string' || name.trim() === '' || phone.trim() === '') { @@ -73,6 +74,7 @@ function add(phone, name, email) { * @returns {Boolean} */ function update(phone, name, email) { + var i = false; if (generalCheck(phone, name, email) === false) { return false; } @@ -80,10 +82,11 @@ function update(phone, name, email) { if (element.phone === phone) { element.name = name; element.email = email; + i = true; } }); - return true; + return i; } /** @@ -220,10 +223,10 @@ function importFromCsv(csv) { var lineSplit; for (var i = 0; i <= line.length; i++) { lineSplit = String(line[i]).split(';'); - if (update(lineSplit[1], lineSplit[0], lineSplit[2])) { + if (update(lineSplit[1], lineSplit[0], lineSplit[2]) === true) { lineAdd++; } - if (add(lineSplit[1], lineSplit[0], lineSplit[2])) { + if (add(lineSplit[1], lineSplit[0], lineSplit[2]) === true) { lineAdd++; } From 83749427f0683e9aa4fef11cf546174c52a780d5 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 11:50:43 +0500 Subject: [PATCH 11/20] javascript-task-2.9 --- phone-book.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index ebb61388..a1b6d7de 100644 --- a/phone-book.js +++ b/phone-book.js @@ -104,7 +104,8 @@ function findAndRemoveStar(numberRecords) { return numberRecords; } -function findAndRemoveEmail(numberRecords, query) { +function findAndRemoveEmail(query) { + var numberRecords = 0; phoneBook.forEach(element => { if (element.email !== undefined) { if (element.phone.includes(query) === true || @@ -119,6 +120,7 @@ function findAndRemoveEmail(numberRecords, query) { return numberRecords; } + function findAndRemove(query) { if (typeof query !== 'string') { return 0; @@ -139,7 +141,7 @@ function findAndRemove(query) { } } }); - numberRecords = numberRecords + findAndRemoveEmail(numberRecords, query); + numberRecords = numberRecords + findAndRemoveEmail(query); return numberRecords; } From 3b0a94c5f86c83ab3e865be99963bfedcb5cd06c Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 12:16:44 +0500 Subject: [PATCH 12/20] javascript-task-2.91 --- phone-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index a1b6d7de..83dd1747 100644 --- a/phone-book.js +++ b/phone-book.js @@ -207,7 +207,7 @@ function find(query) { findStar(query, string, found); findMore(query, string, found); - return found.sort(); + return found.sort((a, b) => a.split(',')[0].localeCompare(b.split(',')[0])); } /** From 6257f48715fb1643333203f408d0a9d956a55fd9 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 12:52:51 +0500 Subject: [PATCH 13/20] check --- phone-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index 83dd1747..1096e2b2 100644 --- a/phone-book.js +++ b/phone-book.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализован метод importFromCsv */ -const isStar = true; +const isStar = false; /** * Телефонная книга From 0822a482cb71c86531404bf3087ebbbefe20f432 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 12:54:16 +0500 Subject: [PATCH 14/20] javascript-task-2.91 --- phone-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index 1096e2b2..83dd1747 100644 --- a/phone-book.js +++ b/phone-book.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализован метод importFromCsv */ -const isStar = false; +const isStar = true; /** * Телефонная книга From a47065be47fc155a46becd0b4c428a5852cd466f Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 13:11:39 +0500 Subject: [PATCH 15/20] javascript-task-2.92 --- phone-book.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index 83dd1747..22e4dadd 100644 --- a/phone-book.js +++ b/phone-book.js @@ -14,7 +14,7 @@ let phoneBook = []; function check(phone, name) { if (typeof phone !== 'string' || typeof name !== 'string' || name.trim() === '' || - phone.trim() === '') { + phone.trim() === '' || name === undefined) { return false; } @@ -95,6 +95,7 @@ function update(phone, name, email) { * @returns {Number} */ + function findAndRemoveStar(numberRecords) { phoneBook.forEach(element => { delete phoneBook[element]; From 70efbba4ba00f313f14b13365ce95ac693a27e18 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 13:43:17 +0500 Subject: [PATCH 16/20] javascript-task-2.93 --- phone-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index 22e4dadd..8a556ebe 100644 --- a/phone-book.js +++ b/phone-book.js @@ -191,7 +191,7 @@ function find(query) { } var string; var found = []; - if (query === '') { + if (query.trim() === '') { return []; } phoneBook.forEach(element => { From 4aeab3aeba2e4544723160a70ea716ac1d71ef43 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 13:56:42 +0500 Subject: [PATCH 17/20] javascript-task-2.94 --- phone-book.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index 8a556ebe..3ee51378 100644 --- a/phone-book.js +++ b/phone-book.js @@ -26,7 +26,8 @@ function check2(email) { if (typeof email !== 'string' || email.trim() === '') { return false; } - + } + if (email === undefined) { return true; } } From a2e08ec7aab41e28c84b0ee2ae77d4b222ce3b02 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 14:24:31 +0500 Subject: [PATCH 18/20] javascript-task-2.95 --- phone-book.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/phone-book.js b/phone-book.js index 3ee51378..a76c03b9 100644 --- a/phone-book.js +++ b/phone-book.js @@ -159,9 +159,9 @@ function findMore(query, string, found) { if (element.email !== undefined) { if (element.email.includes(query) === true || element.phone.includes(query) === true || element.name.includes(query) === true) { - string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + - String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + - '-' + String(element.phone).slice(8, 10) + ', ' + element.email; + string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + + element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + + '-' + element.phone.slice(8, 10) + ', ' + element.email; found.push(string); } } @@ -172,15 +172,15 @@ function findMore(query, string, found) { function findStar(query, string, found) { phoneBook.forEach(element => { if (element.email !== undefined && query === '*') { - string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + - String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + - '-' + String(element.phone).slice(8, 10) + ', ' + element.email; + string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + + element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + + '-' + element.phone.slice(8, 10) + ', ' + element.email; found.push(string); } if (query === '*' && element.email === undefined) { - string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + - String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + - '-' + String(element.phone).slice(8, 10); + string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + + element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + + '-' + element.phone.slice(8, 10); found.push(string); } }); @@ -199,9 +199,9 @@ function find(query) { if (element.email === undefined) { if (element.phone.includes(query) === true || element.name.includes(query) === true) { - string = element.name + ', +7 (' + String(element.phone).slice(0, 3) + ') ' + - String(element.phone).slice(3, 6) + '-' + String(element.phone).slice(6, 8) + - '-' + String(element.phone).slice(8, 10); + string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + + element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + + '-' + element.phone.slice(8, 10); found.push(string); } } @@ -225,8 +225,8 @@ function importFromCsv(csv) { var line = csv.split('\n'); var lineAdd = 0; var lineSplit; - for (var i = 0; i <= line.length; i++) { - lineSplit = String(line[i]).split(';'); + for (var i = 0; i < line.length; i++) { + lineSplit = line[i].split(';'); if (update(lineSplit[1], lineSplit[0], lineSplit[2]) === true) { lineAdd++; } From a0432fe8a28cbf7b02c03ddbe9178e34fa27e996 Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 15:40:12 +0500 Subject: [PATCH 19/20] javascript-task-2.96 --- phone-book.js | 84 ++++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 61 deletions(-) diff --git a/phone-book.js b/phone-book.js index a76c03b9..88de7f04 100644 --- a/phone-book.js +++ b/phone-book.js @@ -13,8 +13,8 @@ let phoneBook = []; function check(phone, name) { - if (typeof phone !== 'string' || typeof name !== 'string' || name.trim() === '' || - phone.trim() === '' || name === undefined) { + if (typeof phone !== 'string' || typeof name !== 'string' || name.length <= 0 || + phone <= 0) { return false; } @@ -23,7 +23,7 @@ function check(phone, name) { function check2(email) { if (email !== undefined) { - if (typeof email !== 'string' || email.trim() === '') { + if (typeof email !== 'string' || email.length === 0) { return false; } } @@ -40,8 +40,8 @@ function generalCheck(phone, name, email) { return true; } -function checkPhone(phoneBookk, phone) { - return phoneBookk.some(function (element) { +function checkPhone(phone) { + return phoneBook.some(function (element) { return (element.phone === phone); }); } @@ -57,7 +57,7 @@ function add(phone, name, email) { if (generalCheck(phone, name, email) === false) { return false; } - if (checkPhone(phoneBook, phone) === true) { + if (checkPhone(phone) === true) { return false; } var entry = { phone: phone, name: name, email: email }; @@ -106,28 +106,9 @@ function findAndRemoveStar(numberRecords) { return numberRecords; } -function findAndRemoveEmail(query) { - var numberRecords = 0; - phoneBook.forEach(element => { - if (element.email !== undefined) { - if (element.phone.includes(query) === true || - element.name.includes(query) === true || - element.email.includes(query) === true) { - delete phoneBook[element]; - numberRecords++; - } - } - }); - - return numberRecords; -} - function findAndRemove(query) { - if (typeof query !== 'string') { - return 0; - } - if (query.trim() === '') { + if (typeof query !== 'string' || query.length === 0) { return 0; } var numberRecords = 0; @@ -135,15 +116,13 @@ function findAndRemove(query) { return findAndRemoveStar(numberRecords); } phoneBook.forEach(element => { - if (element.email === undefined) { - if (element.phone.includes(query) === true || - element.name.includes(query) === true) { - delete phoneBook[element]; - numberRecords++; - } + if (element.phone.includes(query) === true || + element.name.includes(query) === true || + (element.email ? element.email.includes(query) : false)) { + delete phoneBook[element]; + numberRecords++; } }); - numberRecords = numberRecords + findAndRemoveEmail(query); return numberRecords; } @@ -154,21 +133,6 @@ function findAndRemove(query) { * @returns {String[]} */ -function findMore(query, string, found) { - phoneBook.forEach(element => { - if (element.email !== undefined) { - if (element.email.includes(query) === true || element.phone.includes(query) === true || - element.name.includes(query) === true) { - string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + - element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + - '-' + element.phone.slice(8, 10) + ', ' + element.email; - found.push(string); - } - } - - }); -} - function findStar(query, string, found) { phoneBook.forEach(element => { if (element.email !== undefined && query === '*') { @@ -187,27 +151,25 @@ function findStar(query, string, found) { } function find(query) { - if (typeof query !== 'string') { + if (typeof query !== 'string' || query.length === 0) { return []; } var string; var found = []; - if (query.trim() === '') { - return []; - } phoneBook.forEach(element => { - if (element.email === undefined) { - if (element.phone.includes(query) === true || - element.name.includes(query) === true) { - string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + + if (element.phone.includes(query) === true || + element.name.includes(query) === true || + (element.email ? element.email.includes(query) : false)) { + string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + - '-' + element.phone.slice(8, 10); - found.push(string); - } + '-' + element.phone.slice(8, 10) + + (element.email ? ', ' + element.email : ''); + found.push(string); } + }); findStar(query, string, found); - findMore(query, string, found); + // findMore(query, string, found); return found.sort((a, b) => a.split(',')[0].localeCompare(b.split(',')[0])); } @@ -219,7 +181,7 @@ function find(query) { * @returns {Number} – количество добавленных и обновленных записей */ function importFromCsv(csv) { - if (typeof csv !== 'string' || csv.trim() === '') { + if (typeof csv !== 'string' || csv.length === 0) { return 0; } var line = csv.split('\n'); From 3ef7da63b1187fc8a83c6a807905679007c6fb0c Mon Sep 17 00:00:00 2001 From: LopatinNikolai Date: Thu, 25 Oct 2018 16:07:40 +0500 Subject: [PATCH 20/20] javascript-task-2.97 --- phone-book.js | 51 +++++++++++++++++---------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/phone-book.js b/phone-book.js index 88de7f04..e4fd280e 100644 --- a/phone-book.js +++ b/phone-book.js @@ -97,34 +97,22 @@ function update(phone, name, email) { */ -function findAndRemoveStar(numberRecords) { - phoneBook.forEach(element => { - delete phoneBook[element]; - numberRecords++; - }); - - return numberRecords; -} - - function findAndRemove(query) { if (typeof query !== 'string' || query.length === 0) { return 0; } - var numberRecords = 0; + var numberRecords = phoneBook.length; if (query === '*') { - return findAndRemoveStar(numberRecords); + phoneBook = []; + + return numberRecords; } - phoneBook.forEach(element => { - if (element.phone.includes(query) === true || - element.name.includes(query) === true || - (element.email ? element.email.includes(query) : false)) { - delete phoneBook[element]; - numberRecords++; - } - }); + phoneBook = phoneBook.filter(({ name, phone, email }) => + !(name.includes(query) || + phone.includes(query) || + (email ? email.includes(query) : false))); - return numberRecords; + return numberRecords - phoneBook.length; } /** @@ -134,20 +122,16 @@ function findAndRemove(query) { */ function findStar(query, string, found) { - phoneBook.forEach(element => { - if (element.email !== undefined && query === '*') { - string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + - element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + - '-' + element.phone.slice(8, 10) + ', ' + element.email; - found.push(string); - } - if (query === '*' && element.email === undefined) { + if (query === '*') { + phoneBook.forEach(element => { string = element.name + ', +7 (' + element.phone.slice(0, 3) + ') ' + - element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + - '-' + element.phone.slice(8, 10); + element.phone.slice(3, 6) + '-' + element.phone.slice(6, 8) + + '-' + element.phone.slice(8, 10) + + (element.email ? ', ' + element.email : ''); found.push(string); - } - }); + + }); + } } function find(query) { @@ -169,7 +153,6 @@ function find(query) { }); findStar(query, string, found); - // findMore(query, string, found); return found.sort((a, b) => a.split(',')[0].localeCompare(b.split(',')[0])); }