From ff52f66f9443ffae6db7daa85ca28a41746ca93b Mon Sep 17 00:00:00 2001 From: rybufc Date: Tue, 10 Oct 2017 13:02:19 +0500 Subject: [PATCH 01/20] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20=D1=80=D0=B5=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phone-book.js | 173 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 69fb468..5c4ecd2 100644 --- a/phone-book.js +++ b/phone-book.js @@ -6,47 +6,203 @@ */ exports.isStar = true; +/** + * Регулярка для проверки номера телефона + */ +var regexp = /^[0-9]{10}$/; + /** * Телефонная книга */ -var phoneBook; +var phoneBook = []; + +/** + * Запись в телефонной книге + */ +class Contact { + constructor(phone, name, email) { + if (name === undefined || name === null || name === '') { + throw Error('1'); + } + if (phone.match(regexp) === null) { + throw Error('2'); + } + this.phone = phone; + this.name = name; + this.email = email; + } + + toString() { + let phone = '+7 (' + this.phone.substr(0, 3) + ') ' + this.phone.substr(3, 3) + '-' + + this.phone.substr(6, 2) + '-' + this.phone.substr(8, 2); + if (this.email === undefined) { + return this.name + ', ' + phone; + } + + return this.name + ', ' + phone + ', ' + this.email; + } +} /** * Добавление записи в телефонную книгу * @param {String} phone * @param {String} name * @param {String} email + * @returns {Boolean} result */ exports.add = function (phone, name, email) { + if (phoneBook.length > 0) { + if (!checkContact(phone, name, email)) { + return false; + } + } + try { + var contact = new Contact(phone, name, email); + phoneBook.push(contact); + } catch (error) { + return false; + } + return true; }; +/** + * Check contact for existing in phone book + * @param {String} phone + * @param {String} name + * @param {String} email + * @returns {Boolean} exist ? true : false + */ +function checkContact(phone, name, email) { + for (let contact in phoneBook) { + if (phoneBook[contact].name === name || + phoneBook[contact].phone === phone || + phoneBook[contact].email === email) { + return false; + } + } + + return true; +} + /** * Обновление записи в телефонной книге * @param {String} phone * @param {String} name * @param {String} email + * @returns {Boolean} result */ exports.update = function (phone, name, email) { + if (name === null || name === '') { + return false; + } + for (let contact in phoneBook) { + if (phoneBook[contact].phone === phone) { + phoneBook[contact].name = name; + phoneBook[contact].email = email; + + return true; + } + } + return false; }; /** * Удаление записей по запросу из телефонной книги * @param {String} query + * @returns {Integer} number of deleted elements */ exports.findAndRemove = function (query) { + let deleted = 0; + if (query === '*') { + return removeAll(); + } + deleted = removeByKey(query); + return deleted; }; +/** + * Removes all elements and returns number of deleted items + * @returns {Integer} number of deleted elements + */ +function removeAll() { + let deleted = phoneBook.length; + phoneBook = []; + + return deleted; +} + +function removeByKey(query) { + let deleted = 0; + for (let contact in phoneBook) { + if (contact < phoneBook.length) { + deleted = checkAndDelete(contact, query); + } + } + + return deleted; +} + +/** + * Check contact for matching query and deletes from the phone book if match + * @param {Integer} contact + * @param {String} query + * @returns {Integer} numOfDeleted + */ +function checkAndDelete(contact, query) { + let deleted = 0; + if (phoneBook[contact].name.match(query) || + (phoneBook[contact].phone !== undefined && phoneBook[contact].phone.match(query)) || + (phoneBook[contact].email !== undefined && phoneBook[contact].email.match(query))) { + phoneBook.splice(contact, 1); + deleted = contact === phoneBook.length ? deleted + 1 + : deleted + 1 + checkAndDelete(contact, query); + } + + return deleted; +} + /** * Поиск записей по запросу в телефонной книге * @param {String} query + * @returns {Array} result */ exports.find = function (query) { + let result = []; + if (query === '*') { + return getAllContacts(); + } + for (let contact in phoneBook) { + if (phoneBook[contact].name.match(query) !== null) { + result.push(phoneBook[contact].toString()); + } else if (!phoneBook[contact].phone !== undefined && + phoneBook[contact].phone.match(query) !== null) { + result.push(phoneBook[contact].toString()); + } else if (!phoneBook[contact].email !== undefined && + phoneBook[contact].email.match(query) !== null) { + result.push(phoneBook[contact].toString()); + } + } + return result.sort(); }; +/** + * @returns {Arrayy} of all contacts + */ +function getAllContacts() { + let result = []; + for (let contact in phoneBook) { + if (phoneBook[contact].name !== undefined) { + result.push(phoneBook[contact].toString()); + } + } + + return result.sort(); +} + /** * Импорт записей из csv-формата * @star @@ -57,6 +213,19 @@ exports.importFromCsv = function (csv) { // Парсим csv // Добавляем в телефонную книгу // Либо обновляем, если запись с таким телефоном уже существует + let contacts = csv.split('\n'); + let added = 0; + for (let contact of contacts) { + let tokens = contact.split(';'); + let name = tokens[0]; + let phone = tokens[1]; + let email = tokens[2]; + if (this.add(phone, name, email)) { + added += 1; + } else if (this.update(phone, name, email)) { + added += 1; + } + } - return csv.split('\n').length; + return added; }; From 6958edca155d9141bf39aa6082e487b30939de98 Mon Sep 17 00:00:00 2001 From: rybufc Date: Tue, 10 Oct 2017 13:10:46 +0500 Subject: [PATCH 02/20] isstar = false --- phone-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index 5c4ecd2..de4612c 100644 --- a/phone-book.js +++ b/phone-book.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализован метод importFromCsv */ -exports.isStar = true; +exports.isStar = false; /** * Регулярка для проверки номера телефона From 73cf12c24fcf32eb0a16a6d98dae246a3c723e8f Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 10:44:02 +0500 Subject: [PATCH 03/20] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phone-book.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/phone-book.js b/phone-book.js index de4612c..5eb29e3 100644 --- a/phone-book.js +++ b/phone-book.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализован метод importFromCsv */ -exports.isStar = false; +exports.isStar = true; /** * Регулярка для проверки номера телефона @@ -67,7 +67,7 @@ exports.add = function (phone, name, email) { }; /** - * Check contact for existing in phone book + * Проверка контакта на его наличие в телефонной книге * @param {String} phone * @param {String} name * @param {String} email @@ -124,7 +124,7 @@ exports.findAndRemove = function (query) { }; /** - * Removes all elements and returns number of deleted items + * Удаление ВСЕХ записей из телефонной книги. Возвращает число удалённых записей. * @returns {Integer} number of deleted elements */ function removeAll() { @@ -177,10 +177,10 @@ exports.find = function (query) { for (let contact in phoneBook) { if (phoneBook[contact].name.match(query) !== null) { result.push(phoneBook[contact].toString()); - } else if (!phoneBook[contact].phone !== undefined && + } else if (phoneBook[contact].phone !== undefined && phoneBook[contact].phone.match(query) !== null) { result.push(phoneBook[contact].toString()); - } else if (!phoneBook[contact].email !== undefined && + } else if (phoneBook[contact].email !== undefined && phoneBook[contact].email.match(query) !== null) { result.push(phoneBook[contact].toString()); } From b79856af34af8502388572e348d0426ef2dfbfe3 Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 11:01:52 +0500 Subject: [PATCH 04/20] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B0=D1=8F=20=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phone-book.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/phone-book.js b/phone-book.js index 5eb29e3..1e60819 100644 --- a/phone-book.js +++ b/phone-book.js @@ -21,8 +21,8 @@ var phoneBook = []; */ class Contact { constructor(phone, name, email) { - if (name === undefined || name === null || name === '') { - throw Error('1'); + if (isEmpty(name)) { + throw TypeError; } if (phone.match(regexp) === null) { throw Error('2'); @@ -43,6 +43,15 @@ class Contact { } } +/** + * Проверяет строку, пустая-ли она. + * @param {String} str + * @returns {Boolean} empty ? true: false + */ +function isEmpty(str) { + return str === undefined || str === null || str === ''; +} + /** * Добавление записи в телефонную книгу * @param {String} phone @@ -93,7 +102,7 @@ function checkContact(phone, name, email) { * @returns {Boolean} result */ exports.update = function (phone, name, email) { - if (name === null || name === '') { + if (isEmpty(name)) { return false; } for (let contact in phoneBook) { @@ -114,13 +123,11 @@ exports.update = function (phone, name, email) { * @returns {Integer} number of deleted elements */ exports.findAndRemove = function (query) { - let deleted = 0; if (query === '*') { return removeAll(); } - deleted = removeByKey(query); - return deleted; + return removeByKey(query); }; /** @@ -153,9 +160,9 @@ function removeByKey(query) { */ function checkAndDelete(contact, query) { let deleted = 0; - if (phoneBook[contact].name.match(query) || - (phoneBook[contact].phone !== undefined && phoneBook[contact].phone.match(query)) || - (phoneBook[contact].email !== undefined && phoneBook[contact].email.match(query))) { + if (phoneBook[contact].name.match(query) !== null || + (!isEmpty(phoneBook[contact].phone) && phoneBook[contact].phone.match(query) !== null) || + (!isEmpty(phoneBook[contact].email) && phoneBook[contact].email.match(query) !== null)) { phoneBook.splice(contact, 1); deleted = contact === phoneBook.length ? deleted + 1 : deleted + 1 + checkAndDelete(contact, query); From a4d424486201d22b0221bb6977675a857431896e Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 11:06:49 +0500 Subject: [PATCH 05/20] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B0=D1=8F=20=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phone-book.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 1e60819..3b27824 100644 --- a/phone-book.js +++ b/phone-book.js @@ -184,10 +184,10 @@ exports.find = function (query) { for (let contact in phoneBook) { if (phoneBook[contact].name.match(query) !== null) { result.push(phoneBook[contact].toString()); - } else if (phoneBook[contact].phone !== undefined && + } else if (!isEmpty(phoneBook[contact].phone) && phoneBook[contact].phone.match(query) !== null) { result.push(phoneBook[contact].toString()); - } else if (phoneBook[contact].email !== undefined && + } else if (!isEmpty(phoneBook[contact].email) && phoneBook[contact].email.match(query) !== null) { result.push(phoneBook[contact].toString()); } From 4940156d7aab5df25d391678e968f3c88150f6b9 Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 11:33:17 +0500 Subject: [PATCH 06/20] for in -> forEach --- phone-book.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 3b27824..bfb68e9 100644 --- a/phone-book.js +++ b/phone-book.js @@ -143,11 +143,16 @@ function removeAll() { function removeByKey(query) { let deleted = 0; - for (let contact in phoneBook) { + phoneBook.forEach(function (element, index) { + if (index < phoneBook.length) { + deleted = checkAndDelete(index, query); + } + });/* + for (let contact of phoneBook) { if (contact < phoneBook.length) { deleted = checkAndDelete(contact, query); } - } + }*/ return deleted; } From 7478fe1e7945c1321e9984f2b83ed89fe9bad5a2 Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 11:37:03 +0500 Subject: [PATCH 07/20] string.match -> string.find --- phone-book.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/phone-book.js b/phone-book.js index bfb68e9..63c0a3e 100644 --- a/phone-book.js +++ b/phone-book.js @@ -147,12 +147,7 @@ function removeByKey(query) { if (index < phoneBook.length) { deleted = checkAndDelete(index, query); } - });/* - for (let contact of phoneBook) { - if (contact < phoneBook.length) { - deleted = checkAndDelete(contact, query); - } - }*/ + }); return deleted; } @@ -166,8 +161,8 @@ function removeByKey(query) { function checkAndDelete(contact, query) { let deleted = 0; if (phoneBook[contact].name.match(query) !== null || - (!isEmpty(phoneBook[contact].phone) && phoneBook[contact].phone.match(query) !== null) || - (!isEmpty(phoneBook[contact].email) && phoneBook[contact].email.match(query) !== null)) { + (!isEmpty(phoneBook[contact].phone) && phoneBook[contact].phone.find(query) !== -1) || + (!isEmpty(phoneBook[contact].email) && phoneBook[contact].email.find(query) !== -1)) { phoneBook.splice(contact, 1); deleted = contact === phoneBook.length ? deleted + 1 : deleted + 1 + checkAndDelete(contact, query); From 29233f91145826ab3f64db938fd65504f3cd480a Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 11:38:33 +0500 Subject: [PATCH 08/20] find -> search* --- phone-book.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phone-book.js b/phone-book.js index 63c0a3e..d68ef40 100644 --- a/phone-book.js +++ b/phone-book.js @@ -161,8 +161,8 @@ function removeByKey(query) { function checkAndDelete(contact, query) { let deleted = 0; if (phoneBook[contact].name.match(query) !== null || - (!isEmpty(phoneBook[contact].phone) && phoneBook[contact].phone.find(query) !== -1) || - (!isEmpty(phoneBook[contact].email) && phoneBook[contact].email.find(query) !== -1)) { + (!isEmpty(phoneBook[contact].phone) && phoneBook[contact].phone.search(query) !== -1) || + (!isEmpty(phoneBook[contact].email) && phoneBook[contact].email.search(query) !== -1)) { phoneBook.splice(contact, 1); deleted = contact === phoneBook.length ? deleted + 1 : deleted + 1 + checkAndDelete(contact, query); From 17eb3671c57622ef5ebd1607f45a6c415625023f Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 11:40:53 +0500 Subject: [PATCH 09/20] find -> search --- phone-book.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phone-book.js b/phone-book.js index d68ef40..96457cc 100644 --- a/phone-book.js +++ b/phone-book.js @@ -182,13 +182,13 @@ exports.find = function (query) { return getAllContacts(); } for (let contact in phoneBook) { - if (phoneBook[contact].name.match(query) !== null) { + if (phoneBook[contact].name.search(query) !== -1) { result.push(phoneBook[contact].toString()); } else if (!isEmpty(phoneBook[contact].phone) && - phoneBook[contact].phone.match(query) !== null) { + phoneBook[contact].phone.search(query) !== -1) { result.push(phoneBook[contact].toString()); } else if (!isEmpty(phoneBook[contact].email) && - phoneBook[contact].email.match(query) !== null) { + phoneBook[contact].email.search(query) !== -1) { result.push(phoneBook[contact].toString()); } } @@ -202,7 +202,7 @@ exports.find = function (query) { function getAllContacts() { let result = []; for (let contact in phoneBook) { - if (phoneBook[contact].name !== undefined) { + if (!isEmpty(phoneBook[contact].name)) { result.push(phoneBook[contact].toString()); } } From edf8497001e2fcf2768f8505d36a2f2ecbe2a2b3 Mon Sep 17 00:00:00 2001 From: rybufc Date: Wed, 11 Oct 2017 11:43:47 +0500 Subject: [PATCH 10/20] isEmpty(phone) in update function --- phone-book.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/phone-book.js b/phone-book.js index 96457cc..8b2c5d8 100644 --- a/phone-book.js +++ b/phone-book.js @@ -105,10 +105,11 @@ exports.update = function (phone, name, email) { if (isEmpty(name)) { return false; } - for (let contact in phoneBook) { - if (phoneBook[contact].phone === phone) { - phoneBook[contact].name = name; - phoneBook[contact].email = email; + for (let index in phoneBook) { + if (!isEmpty(phoneBook[index].phone) && + phoneBook[index].phone === phone) { + phoneBook[index].name = name; + phoneBook[index].email = email; return true; } From 5d56bc5100714b88f89f624716e7af5b5dd4c200 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 15:52:00 +0500 Subject: [PATCH 11/20] =?UTF-8?q?=D0=BD=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B0=D1=8F=20=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phone-book.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/phone-book.js b/phone-book.js index 8b2c5d8..918d5b1 100644 --- a/phone-book.js +++ b/phone-book.js @@ -106,8 +106,7 @@ exports.update = function (phone, name, email) { return false; } for (let index in phoneBook) { - if (!isEmpty(phoneBook[index].phone) && - phoneBook[index].phone === phone) { + if (phoneBook[index].phone === phone) { phoneBook[index].name = name; phoneBook[index].email = email; @@ -161,9 +160,8 @@ function removeByKey(query) { */ function checkAndDelete(contact, query) { let deleted = 0; - if (phoneBook[contact].name.match(query) !== null || - (!isEmpty(phoneBook[contact].phone) && phoneBook[contact].phone.search(query) !== -1) || - (!isEmpty(phoneBook[contact].email) && phoneBook[contact].email.search(query) !== -1)) { + if (checkEntry(phoneBook[contact].name, query) || checkEntry(phoneBook[contact].phone, query) || + checkEntry(phoneBook[contact].email, query)) { phoneBook.splice(contact, 1); deleted = contact === phoneBook.length ? deleted + 1 : deleted + 1 + checkAndDelete(contact, query); @@ -172,6 +170,16 @@ function checkAndDelete(contact, query) { return deleted; } +/** + * Проверяет строку на наличие в ней подстроки. + * @param {*} str + * @param {*} query + * @returns {Boolean} result + */ +function checkEntry(str, query) { + return !isEmpty(str) && str.indexOf(query) !== -1; +} + /** * Поиск записей по запросу в телефонной книге * @param {String} query @@ -183,13 +191,13 @@ exports.find = function (query) { return getAllContacts(); } for (let contact in phoneBook) { - if (phoneBook[contact].name.search(query) !== -1) { + if (phoneBook[contact].name.indexOf(query) !== -1) { result.push(phoneBook[contact].toString()); } else if (!isEmpty(phoneBook[contact].phone) && - phoneBook[contact].phone.search(query) !== -1) { + phoneBook[contact].phone.indexOf(query) !== -1) { result.push(phoneBook[contact].toString()); } else if (!isEmpty(phoneBook[contact].email) && - phoneBook[contact].email.search(query) !== -1) { + phoneBook[contact].email.indexOf(query) !== -1) { result.push(phoneBook[contact].toString()); } } From 7b9ada8bd07baed549a948f9c2b20af04474f23f Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 15:55:51 +0500 Subject: [PATCH 12/20] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B4=D0=BE=D0=BB?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=87=D0=B8=D1=81=D1=82=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=BA=D0=BE=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phone-book.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/phone-book.js b/phone-book.js index 918d5b1..fc80c62 100644 --- a/phone-book.js +++ b/phone-book.js @@ -191,13 +191,11 @@ exports.find = function (query) { return getAllContacts(); } for (let contact in phoneBook) { - if (phoneBook[contact].name.indexOf(query) !== -1) { + if (checkEntry(phoneBook[contact].name, query)) { result.push(phoneBook[contact].toString()); - } else if (!isEmpty(phoneBook[contact].phone) && - phoneBook[contact].phone.indexOf(query) !== -1) { + } else if (checkEntry(phoneBook[contact].phone, query)) { result.push(phoneBook[contact].toString()); - } else if (!isEmpty(phoneBook[contact].email) && - phoneBook[contact].email.indexOf(query) !== -1) { + } else if (checkEntry(phoneBook[contact].email, query)) { result.push(phoneBook[contact].toString()); } } From d6c9bf92867c9019081e97b3753e553548869709 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:01:59 +0500 Subject: [PATCH 13/20] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BA=D0=BE=D0=BD=D1=81=D1=82=D1=80=D1=83=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Con?= =?UTF-8?q?tact?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phone-book.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/phone-book.js b/phone-book.js index fc80c62..c81164e 100644 --- a/phone-book.js +++ b/phone-book.js @@ -24,8 +24,10 @@ class Contact { if (isEmpty(name)) { throw TypeError; } - if (phone.match(regexp) === null) { - throw Error('2'); + if (!isEmpty(phone)) { + if (phone.match(regexp) === null) { + throw TypeError; + } } this.phone = phone; this.name = name; @@ -61,7 +63,7 @@ function isEmpty(str) { */ exports.add = function (phone, name, email) { if (phoneBook.length > 0) { - if (!checkContact(phone, name, email)) { + if (checkExistence(phone, name, email)) { return false; } } @@ -82,16 +84,16 @@ exports.add = function (phone, name, email) { * @param {String} email * @returns {Boolean} exist ? true : false */ -function checkContact(phone, name, email) { +function checkExistence(phone, name, email) { for (let contact in phoneBook) { if (phoneBook[contact].name === name || phoneBook[contact].phone === phone || phoneBook[contact].email === email) { - return false; + return true; } } - return true; + return false; } /** From 7d95f42e82e5b1d2de72f8707a3932ece3ee82f4 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:06:00 +0500 Subject: [PATCH 14/20] update function was updated! --- phone-book.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phone-book.js b/phone-book.js index c81164e..2d134d6 100644 --- a/phone-book.js +++ b/phone-book.js @@ -107,6 +107,9 @@ exports.update = function (phone, name, email) { if (isEmpty(name)) { return false; } + if (phone.match(regexp) === null) { + return false; + } for (let index in phoneBook) { if (phoneBook[index].phone === phone) { phoneBook[index].name = name; From ccec1f856b8ef2978eace0311088847335569087 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:13:59 +0500 Subject: [PATCH 15/20] function fin was updated! --- phone-book.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/phone-book.js b/phone-book.js index 2d134d6..fa38da9 100644 --- a/phone-book.js +++ b/phone-book.js @@ -107,9 +107,6 @@ exports.update = function (phone, name, email) { if (isEmpty(name)) { return false; } - if (phone.match(regexp) === null) { - return false; - } for (let index in phoneBook) { if (phoneBook[index].phone === phone) { phoneBook[index].name = name; @@ -191,10 +188,18 @@ function checkEntry(str, query) { * @returns {Array} result */ exports.find = function (query) { - let result = []; if (query === '*') { return getAllContacts(); } + if (isEmpty(query)) { + return []; + } + + return findByQuery(query).sort(); +}; + +function findByQuery(query) { + let result = []; for (let contact in phoneBook) { if (checkEntry(phoneBook[contact].name, query)) { result.push(phoneBook[contact].toString()); @@ -205,8 +210,8 @@ exports.find = function (query) { } } - return result.sort(); -}; + return result; +} /** * @returns {Arrayy} of all contacts From 4a876594c9fb096a850dc3cb7ed3c5c5395cbb48 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:17:30 +0500 Subject: [PATCH 16/20] function findAndremove was updated! --- phone-book.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phone-book.js b/phone-book.js index fa38da9..d6527ab 100644 --- a/phone-book.js +++ b/phone-book.js @@ -128,6 +128,9 @@ exports.findAndRemove = function (query) { if (query === '*') { return removeAll(); } + if (isEmpty(query)) { + return 0; + } return removeByKey(query); }; From 139227c2bea45655878ebeb04c3f0e9fe4c187e2 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:24:09 +0500 Subject: [PATCH 17/20] function remove was updated --- phone-book.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phone-book.js b/phone-book.js index d6527ab..0c1897e 100644 --- a/phone-book.js +++ b/phone-book.js @@ -150,7 +150,7 @@ function removeByKey(query) { let deleted = 0; phoneBook.forEach(function (element, index) { if (index < phoneBook.length) { - deleted = checkAndDelete(index, query); + deleted += checkAndDelete(index, query); } }); From a7372dba9f8ad87862bc72c9318f18d82ec62c07 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:33:50 +0500 Subject: [PATCH 18/20] delete function was updated! --- phone-book.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/phone-book.js b/phone-book.js index 0c1897e..cda7410 100644 --- a/phone-book.js +++ b/phone-book.js @@ -148,11 +148,14 @@ function removeAll() { function removeByKey(query) { let deleted = 0; + for (let i = phoneBook.length - 1; i >= 0; i--) { + deleted += checkAndDelete(i, query); + }/* phoneBook.forEach(function (element, index) { if (index < phoneBook.length) { deleted += checkAndDelete(index, query); } - }); + });*/ return deleted; } @@ -164,15 +167,16 @@ function removeByKey(query) { * @returns {Integer} numOfDeleted */ function checkAndDelete(contact, query) { - let deleted = 0; if (checkEntry(phoneBook[contact].name, query) || checkEntry(phoneBook[contact].phone, query) || checkEntry(phoneBook[contact].email, query)) { - phoneBook.splice(contact, 1); + phoneBook.splice(contact, 1);/* deleted = contact === phoneBook.length ? deleted + 1 - : deleted + 1 + checkAndDelete(contact, query); + : deleted + 1 + checkAndDelete(contact, query);*/ + + return 1; } - return deleted; + return 0; } /** From ae160ce32b96284ae377b9a03272df422e31369e Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:47:20 +0500 Subject: [PATCH 19/20] updated all function --- phone-book.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/phone-book.js b/phone-book.js index cda7410..a342066 100644 --- a/phone-book.js +++ b/phone-book.js @@ -107,6 +107,9 @@ exports.update = function (phone, name, email) { if (isEmpty(name)) { return false; } + if (phoneBook.length === 0) { + return false; + } for (let index in phoneBook) { if (phoneBook[index].phone === phone) { phoneBook[index].name = name; @@ -125,6 +128,9 @@ exports.update = function (phone, name, email) { * @returns {Integer} number of deleted elements */ exports.findAndRemove = function (query) { + if (phoneBook.length === 0) { + return 0; + } if (query === '*') { return removeAll(); } @@ -150,12 +156,7 @@ function removeByKey(query) { let deleted = 0; for (let i = phoneBook.length - 1; i >= 0; i--) { deleted += checkAndDelete(i, query); - }/* - phoneBook.forEach(function (element, index) { - if (index < phoneBook.length) { - deleted += checkAndDelete(index, query); - } - });*/ + } return deleted; } @@ -169,9 +170,7 @@ function removeByKey(query) { function checkAndDelete(contact, query) { if (checkEntry(phoneBook[contact].name, query) || checkEntry(phoneBook[contact].phone, query) || checkEntry(phoneBook[contact].email, query)) { - phoneBook.splice(contact, 1);/* - deleted = contact === phoneBook.length ? deleted + 1 - : deleted + 1 + checkAndDelete(contact, query);*/ + phoneBook.splice(contact, 1); return 1; } @@ -195,6 +194,9 @@ function checkEntry(str, query) { * @returns {Array} result */ exports.find = function (query) { + if (phoneBook.length === 0) { + return []; + } if (query === '*') { return getAllContacts(); } From e24dee2e43a000270db37ea1ce57a557c1773968 Mon Sep 17 00:00:00 2001 From: rybufc Date: Thu, 12 Oct 2017 16:53:19 +0500 Subject: [PATCH 20/20] updated result.toString() method. --- phone-book.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/phone-book.js b/phone-book.js index a342066..2b46ab9 100644 --- a/phone-book.js +++ b/phone-book.js @@ -35,13 +35,17 @@ class Contact { } toString() { - let phone = '+7 (' + this.phone.substr(0, 3) + ') ' + this.phone.substr(3, 3) + '-' + + let result = this.name; + if (!isEmpty(this.phone)) { + let phone = '+7 (' + this.phone.substr(0, 3) + ') ' + this.phone.substr(3, 3) + '-' + this.phone.substr(6, 2) + '-' + this.phone.substr(8, 2); - if (this.email === undefined) { - return this.name + ', ' + phone; + result += ', ' + phone; + } + if (!isEmpty(this.email)) { + result += ', ' + this.email; } - return this.name + ', ' + phone + ', ' + this.email; + return result; } }