From 2f6df5d78bf792b91bd829de65d49057b49209b1 Mon Sep 17 00:00:00 2001 From: grishbar Date: Sun, 21 Jul 2019 22:07:35 +0500 Subject: [PATCH 1/9] first version, no star implemented functions: __add__ __update__ __find__ __findAndRemove__ --- index.js | 4 +++- phone-book.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 370cebb5..9f329700 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ 'use strict'; - const phoneBook = require('./phone-book'); // Эти записи добавятся, вернется `true` @@ -16,10 +15,12 @@ phoneBook.add('5555550055'); // Обновление ранее добавленных записей phoneBook.update('5551110011', 'Алексей', 'alex@example.com'); phoneBook.update('5553330033', 'Валерий'); +phoneBook.update('5553330033'); // В следующих примерах вернутся все записи console.info(phoneBook.find('*')); console.info(phoneBook.find('555')); +console.info(phoneBook.find('Григорий')); // В обоих случаях вывод будет следующий // [ // 'Алексей, +7 (555) 111-00-11, alex@example.com', @@ -30,6 +31,7 @@ console.info(phoneBook.find('555')); // Удаление записей, содержащих '@', вернется `3` phoneBook.findAndRemove('@'); +console.log(phoneBook.findAndRemove('@')); if (phoneBook.isStar) { const csv = [ diff --git a/phone-book.js b/phone-book.js index 891d7c01..d5de6347 100644 --- a/phone-book.js +++ b/phone-book.js @@ -4,12 +4,13 @@ * Сделано задание на звездочку * Реализован метод importFromCsv */ -const isStar = true; +const isStar = false; /** * Телефонная книга */ -let phoneBook; +let phoneBook = []; +let phoneNotes = []; /** * Добавление записи в телефонную книгу @@ -19,6 +20,20 @@ let phoneBook; * @returns {Boolean} */ function add(phone, name, email) { + let isNoteNew = true; + phoneBook.forEach(function (note, i, phoneBook) { + if (note.name === name || note.phone === phone) + isNoteNew = false; + }); + if (name !== undefined && isNoteNew && phone.length === 10 && !isNaN(phone)) { + let newName = {}; + newName.name = name; + newName.phone = phone; + newName.email = email; + phoneBook.push(newName); + return (true); + } + return (false); } @@ -30,7 +45,15 @@ function add(phone, name, email) { * @returns {Boolean} */ function update(phone, name, email) { - + for (let i = 0; i < phoneBook.length; i++) { + if (phoneBook[i].phone === phone) { + if (name !== undefined) + phoneBook[i].name = name; + phoneBook[i].email = email; + return (true); + } + } + return (false); } /** @@ -39,7 +62,20 @@ function update(phone, name, email) { * @returns {Number} */ function findAndRemove(query) { - + let noteEmail = ''; + let deletedNotesCount = 0; + for (let i = 0; i < phoneBook.length; i++) { + if (phoneBook[i].email === undefined) { + noteEmail = ''; + } else + noteEmail = phoneBook[i].email; + if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || + noteEmail.indexOf(query) >= 0 || query === '*') { + phoneNotes.splice(i, 1); + deletedNotesCount++; + } + } + return deletedNotesCount; } /** @@ -48,7 +84,22 @@ function findAndRemove(query) { * @returns {String[]} */ function find(query) { - + phoneNotes = []; + let noteEmail = ''; + if (query.length === 0 || query === undefined) + return; + for (let i = 0; i < phoneBook.length; i++) { + if (phoneBook[i].email === undefined) { + noteEmail = ''; + } else + noteEmail = phoneBook[i].email; + if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || + noteEmail.indexOf(query) >= 0 || query === '*') { + phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' + phoneBook[i].phone.substr(3, 3) + + '-' + phoneBook[i].phone.substr(6, 2) + '-' + phoneBook[i].phone.substr(8, 2) + ', ' + phoneBook[i].email); + } + } + return phoneNotes.sort(); } /** From c1f1b6cab3c6c3f614ca63dea36e37476a87d221 Mon Sep 17 00:00:00 2001 From: grishbar Date: Sun, 21 Jul 2019 22:38:01 +0500 Subject: [PATCH 2/9] fix linting added newlines before return statemantes, added '{' after every if condition, deleted no unused variables --- index.js | 1 - phone-book.js | 53 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 9f329700..cd7331c0 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,6 @@ console.info(phoneBook.find('Григорий')); // Удаление записей, содержащих '@', вернется `3` phoneBook.findAndRemove('@'); -console.log(phoneBook.findAndRemove('@')); if (phoneBook.isStar) { const csv = [ diff --git a/phone-book.js b/phone-book.js index d5de6347..adb88dd0 100644 --- a/phone-book.js +++ b/phone-book.js @@ -21,9 +21,10 @@ let phoneNotes = []; */ function add(phone, name, email) { let isNoteNew = true; - phoneBook.forEach(function (note, i, phoneBook) { - if (note.name === name || note.phone === phone) + phoneBook.forEach(function (note) { + if (note.name === name || note.phone === phone) { isNoteNew = false; + } }); if (name !== undefined && isNoteNew && phone.length === 10 && !isNaN(phone)) { let newName = {}; @@ -31,10 +32,11 @@ function add(phone, name, email) { newName.phone = phone; newName.email = email; phoneBook.push(newName); + return (true); } - return (false); + return (false); } /** @@ -47,12 +49,15 @@ function add(phone, name, email) { function update(phone, name, email) { for (let i = 0; i < phoneBook.length; i++) { if (phoneBook[i].phone === phone) { - if (name !== undefined) + if (name !== undefined) { phoneBook[i].name = name; + } phoneBook[i].email = email; + return (true); } } + return (false); } @@ -65,16 +70,12 @@ function findAndRemove(query) { let noteEmail = ''; let deletedNotesCount = 0; for (let i = 0; i < phoneBook.length; i++) { - if (phoneBook[i].email === undefined) { - noteEmail = ''; - } else - noteEmail = phoneBook[i].email; - if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || - noteEmail.indexOf(query) >= 0 || query === '*') { + if (isNoteFound(query, i)) { phoneNotes.splice(i, 1); deletedNotesCount++; } } + return deletedNotesCount; } @@ -86,22 +87,36 @@ function findAndRemove(query) { function find(query) { phoneNotes = []; let noteEmail = ''; - if (query.length === 0 || query === undefined) + if (query.length === 0 || query === undefined) { + return; + } for (let i = 0; i < phoneBook.length; i++) { - if (phoneBook[i].email === undefined) { - noteEmail = ''; - } else - noteEmail = phoneBook[i].email; - if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || - noteEmail.indexOf(query) >= 0 || query === '*') { - phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' + phoneBook[i].phone.substr(3, 3) + - '-' + phoneBook[i].phone.substr(6, 2) + '-' + phoneBook[i].phone.substr(8, 2) + ', ' + phoneBook[i].email); + if (isNoteFound(query, i)) { + phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' + + phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' + + phoneBook[i].phone.substr(8, 2) + ', ' + phoneBook[i].email); } } + return phoneNotes.sort(); } +function isNoteFound(query, i){ + if (phoneBook[i].email === undefined) { + noteEmail = ''; + } else { + noteEmail = phoneBook[i].email; + } + if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || + noteEmail.indexOf(query) >= 0 || query === '*') { + + return true; + } + + return false; +} + /** * Импорт записей из csv-формата * @star From 426333a67d9ed7cbcc3f4c7ea64fdc93891e8df2 Mon Sep 17 00:00:00 2001 From: grishbar Date: Mon, 22 Jul 2019 00:16:13 +0500 Subject: [PATCH 3/9] fix linting change too deeply blocks, delete trailing spaces, --- phone-book.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/phone-book.js b/phone-book.js index adb88dd0..f4361f58 100644 --- a/phone-book.js +++ b/phone-book.js @@ -48,15 +48,13 @@ function add(phone, name, email) { */ function update(phone, name, email) { for (let i = 0; i < phoneBook.length; i++) { - if (phoneBook[i].phone === phone) { - if (name !== undefined) { + if (phoneBook[i].phone === phone && name !== undefined) { phoneBook[i].name = name; } phoneBook[i].email = email; return (true); } - } return (false); } @@ -67,12 +65,11 @@ function update(phone, name, email) { * @returns {Number} */ function findAndRemove(query) { - let noteEmail = ''; let deletedNotesCount = 0; for (let i = 0; i < phoneBook.length; i++) { if (isNoteFound(query, i)) { - phoneNotes.splice(i, 1); - deletedNotesCount++; + phoneNotes.splice(i, 1); + deletedNotesCount++; } } @@ -86,23 +83,23 @@ function findAndRemove(query) { */ function find(query) { phoneNotes = []; - let noteEmail = ''; if (query.length === 0 || query === undefined) { return; } for (let i = 0; i < phoneBook.length; i++) { if (isNoteFound(query, i)) { - phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' - + phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' + + phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' + + phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' + phoneBook[i].phone.substr(8, 2) + ', ' + phoneBook[i].email); } } - + return phoneNotes.sort(); } -function isNoteFound(query, i){ +function isNoteFound(query, i) { + let noteEmail = ''; if (phoneBook[i].email === undefined) { noteEmail = ''; } else { @@ -111,8 +108,8 @@ function isNoteFound(query, i){ if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || noteEmail.indexOf(query) >= 0 || query === '*') { - return true; - } + return true; + } return false; } From a58cfb834dccc6edf67826ba8289c1efdeb10297 Mon Sep 17 00:00:00 2001 From: grishbar Date: Mon, 22 Jul 2019 00:19:01 +0500 Subject: [PATCH 4/9] fix indentation --- phone-book.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/phone-book.js b/phone-book.js index f4361f58..3d59f1f5 100644 --- a/phone-book.js +++ b/phone-book.js @@ -49,12 +49,12 @@ function add(phone, name, email) { function update(phone, name, email) { for (let i = 0; i < phoneBook.length; i++) { if (phoneBook[i].phone === phone && name !== undefined) { - phoneBook[i].name = name; - } - phoneBook[i].email = email; - - return (true); + phoneBook[i].name = name; } + phoneBook[i].email = email; + + return (true); + } return (false); } @@ -90,8 +90,8 @@ function find(query) { for (let i = 0; i < phoneBook.length; i++) { if (isNoteFound(query, i)) { phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' + - phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' + - phoneBook[i].phone.substr(8, 2) + ', ' + phoneBook[i].email); + phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' + + phoneBook[i].phone.substr(8, 2) + ', ' + phoneBook[i].email); } } @@ -106,7 +106,7 @@ function isNoteFound(query, i) { noteEmail = phoneBook[i].email; } if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || - noteEmail.indexOf(query) >= 0 || query === '*') { + noteEmail.indexOf(query) >= 0 || query === '*') { return true; } From 0795e19cb618cd6d2b75450043008636d1c66825 Mon Sep 17 00:00:00 2001 From: grishbar Date: Mon, 22 Jul 2019 00:51:00 +0500 Subject: [PATCH 5/9] fixed function - update function crushed while fixing linting forget, to run tests again before comitting) --- phone-book.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phone-book.js b/phone-book.js index 3d59f1f5..6ab35bab 100644 --- a/phone-book.js +++ b/phone-book.js @@ -47,16 +47,16 @@ function add(phone, name, email) { * @returns {Boolean} */ function update(phone, name, email) { + let isUpdated = false; for (let i = 0; i < phoneBook.length; i++) { if (phoneBook[i].phone === phone && name !== undefined) { phoneBook[i].name = name; } + isUpdated = true; phoneBook[i].email = email; - - return (true); } - return (false); + return (isUpdated); } /** From a5ab34c804133a804a16825a72c3c749faf2a8b6 Mon Sep 17 00:00:00 2001 From: grishbar Date: Mon, 22 Jul 2019 00:58:02 +0500 Subject: [PATCH 6/9] fix method - update im stupid, didnt see that mails all become undefined, fix this problem --- phone-book.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/phone-book.js b/phone-book.js index 6ab35bab..e295921e 100644 --- a/phone-book.js +++ b/phone-book.js @@ -11,6 +11,7 @@ const isStar = false; */ let phoneBook = []; let phoneNotes = []; +let noteEmail = ''; /** * Добавление записи в телефонную книгу @@ -51,9 +52,10 @@ function update(phone, name, email) { for (let i = 0; i < phoneBook.length; i++) { if (phoneBook[i].phone === phone && name !== undefined) { phoneBook[i].name = name; + phoneBook[i].email = email; + isUpdated = true; + return(isUpdated); } - isUpdated = true; - phoneBook[i].email = email; } return (isUpdated); @@ -91,7 +93,7 @@ function find(query) { if (isNoteFound(query, i)) { phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' + phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' + - phoneBook[i].phone.substr(8, 2) + ', ' + phoneBook[i].email); + phoneBook[i].phone.substr(8, 2) + ', ' + noteEmail); } } @@ -99,7 +101,7 @@ function find(query) { } function isNoteFound(query, i) { - let noteEmail = ''; + noteEmail = ''; if (phoneBook[i].email === undefined) { noteEmail = ''; } else { From ef235b2e4b14643e217d45501d762ad37e505b92 Mon Sep 17 00:00:00 2001 From: grishbar Date: Mon, 22 Jul 2019 01:01:38 +0500 Subject: [PATCH 7/9] fix linting mybe one day i will be more attentive) --- phone-book.js | 1 + 1 file changed, 1 insertion(+) diff --git a/phone-book.js b/phone-book.js index e295921e..3ab1769b 100644 --- a/phone-book.js +++ b/phone-book.js @@ -54,6 +54,7 @@ function update(phone, name, email) { phoneBook[i].name = name; phoneBook[i].email = email; isUpdated = true; + return(isUpdated); } } From 31d12a44fa8c366364bfe2e1e43d9d7af70b0eca Mon Sep 17 00:00:00 2001 From: grishbar Date: Mon, 22 Jul 2019 01:03:58 +0500 Subject: [PATCH 8/9] fix linting --- phone-book.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 3ab1769b..af7c086f 100644 --- a/phone-book.js +++ b/phone-book.js @@ -54,8 +54,8 @@ function update(phone, name, email) { phoneBook[i].name = name; phoneBook[i].email = email; isUpdated = true; - - return(isUpdated); + + return (isUpdated); } } From b233beb6067b8fcaedd0d4ef69d12efe2bbfd8f8 Mon Sep 17 00:00:00 2001 From: grishbar Date: Mon, 22 Jul 2019 01:08:14 +0500 Subject: [PATCH 9/9] fix how mail adds to phonebook --- phone-book.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index af7c086f..72ed552b 100644 --- a/phone-book.js +++ b/phone-book.js @@ -94,7 +94,7 @@ function find(query) { if (isNoteFound(query, i)) { phoneNotes.push(phoneBook[i].name + ', +7 (' + phoneBook[i].phone.substr(0, 3) + ') ' + phoneBook[i].phone.substr(3, 3) + '-' + phoneBook[i].phone.substr(6, 2) + '-' + - phoneBook[i].phone.substr(8, 2) + ', ' + noteEmail); + phoneBook[i].phone.substr(8, 2) + noteEmail); } } @@ -106,7 +106,7 @@ function isNoteFound(query, i) { if (phoneBook[i].email === undefined) { noteEmail = ''; } else { - noteEmail = phoneBook[i].email; + noteEmail = ', ' + phoneBook[i].email; } if (phoneBook[i].name.indexOf(query) >= 0 || phoneBook[i].phone.indexOf(query) >= 0 || noteEmail.indexOf(query) >= 0 || query === '*') {