From 893f87df8dcf0784ff254eb8dd35f9fd27643334 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 03:37:13 +0500 Subject: [PATCH 01/24] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 5 deletions(-) diff --git a/lib.js b/lib.js index 60f30ab..d1bbeb4 100644 --- a/lib.js +++ b/lib.js @@ -1,5 +1,69 @@ 'use strict'; +function clone(obj) { + if (obj === null || typeof obj !== 'object') { + return obj; + } + + var temp = obj.constructor(); + var keys = Object.keys(obj); + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + temp[key] = clone(obj[key]); + } + + return temp; +} + +function getNextCircle(friends) { + return friends.reduce(function (nextCircle, friend) { + nextCircle = nextCircle.concat(friend.friends); + + return nextCircle; + }, []); +} +function getFriendByName(friendName, friends) { + var foundFriend = friends.find(function (friend) { + return friend.name === friendName; + }); + + return clone(foundFriend); +} +function removeExistingFriends(candidates, friendsCircle) { + return friendsCircle.filter(function (friend) { + return !candidates.some(function (candidate) { + return candidate.name === friend.name; + }); + }); +} +function getCandidate(friends) { + var currentFriendsCircle = friends.filter(function (friend) { + return friend.hasOwnProperty('best'); + }) + .map(function (friend) { + friend.level = 1; + + return friend; + }); + var candidates = []; + var currentLevel = 1; + while (currentFriendsCircle.length > 0) { + currentLevel++; + candidates = candidates.concat(currentFriendsCircle); + currentFriendsCircle = getNextCircle(currentFriendsCircle) + .sort() + .map(function (name) { + return getFriendByName(name, friends); + }); + for (var i = 0; i < currentFriendsCircle.length; i++) { + currentFriendsCircle[i].level = currentLevel; + } + currentFriendsCircle = removeExistingFriends(candidates, currentFriendsCircle); + } + + return candidates; +} + /** * Итератор по друзьям * @constructor @@ -7,9 +71,25 @@ * @param {Filter} filter */ function Iterator(friends, filter) { - console.info(friends, filter); + if (!(filter instanceof Filter)) { + throw new TypeError('Мне нужен ФИЛЬТР, а не ЭТО!!!'); + } + this.invitedFriends = getCandidate(friends).filter(function (a) { + return filter.isRight(a); + }); } +Iterator.prototype.done = function () { + return this.invitedFriends.length === 0; +}; + +Iterator.prototype.next = function () { + var friend = this.invitedFriends.shift(); + delete friend.level; + + return friend || null; +}; + /** * Итератор по друзям с ограничением по кругу * @extends Iterator @@ -19,15 +99,22 @@ function Iterator(friends, filter) { * @param {Number} maxLevel – максимальный круг друзей */ function LimitedIterator(friends, filter, maxLevel) { - console.info(friends, filter, maxLevel); + Iterator.call(this, friends, filter); + this.invitedFriends = this.invitedFriends.filter(function (friend) { + return friend.level <= maxLevel; + }); } +LimitedIterator.prototype = Object.create(Iterator.prototype); + /** * Фильтр друзей * @constructor */ function Filter() { - console.info('Filter'); + this.isRight = function () { + return true; + }; } /** @@ -36,8 +123,11 @@ function Filter() { * @constructor */ function MaleFilter() { - console.info('MaleFilter'); + this.isRight = function (human) { + return human.gender === 'male'; + }; } +MaleFilter.prototype = Object.create(Filter.prototype); /** * Фильтр друзей-девушек @@ -45,8 +135,11 @@ function MaleFilter() { * @constructor */ function FemaleFilter() { - console.info('FemaleFilter'); + this.isRight = function (human) { + return human.gender === 'female'; + }; } +FemaleFilter.prototype = Object.create(Filter.prototype); exports.Iterator = Iterator; exports.LimitedIterator = LimitedIterator; From a71224ba57ce64a715212d6d6e339ca44697fa96 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 10:52:44 +0500 Subject: [PATCH 02/24] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA?= =?UTF-8?q?=D1=83=20=D0=B4=D0=BB=D1=8F=20=D0=BB=D1=83=D1=87=D1=88=D0=B8?= =?UTF-8?q?=D1=85=20=D0=B4=D1=80=D1=83=D0=B7=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib.js b/lib.js index d1bbeb4..d953ada 100644 --- a/lib.js +++ b/lib.js @@ -36,10 +36,22 @@ function removeExistingFriends(candidates, friendsCircle) { }); }); } +function compare(a, b) { + if (a > b) { + return 1; + } else if (a < b) { + return -1; + } + + return 0; +} function getCandidate(friends) { var currentFriendsCircle = friends.filter(function (friend) { return friend.hasOwnProperty('best'); }) + .sort(function (a, b) { + return compare(a.name, b.name); + }) .map(function (friend) { friend.level = 1; From fe435d475d86eebfad3f84d26b6b878cc3b55abb Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 11:21:57 +0500 Subject: [PATCH 03/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib.js b/lib.js index d953ada..d7b26a4 100644 --- a/lib.js +++ b/lib.js @@ -48,29 +48,23 @@ function compare(a, b) { function getCandidate(friends) { var currentFriendsCircle = friends.filter(function (friend) { return friend.hasOwnProperty('best'); - }) - .sort(function (a, b) { - return compare(a.name, b.name); - }) - .map(function (friend) { - friend.level = 1; - - return friend; }); var candidates = []; var currentLevel = 1; while (currentFriendsCircle.length > 0) { - currentLevel++; - candidates = candidates.concat(currentFriendsCircle); - currentFriendsCircle = getNextCircle(currentFriendsCircle) - .sort() - .map(function (name) { - return getFriendByName(name, friends); + currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { + return compare(a.name, b.name); }); for (var i = 0; i < currentFriendsCircle.length; i++) { currentFriendsCircle[i].level = currentLevel; } currentFriendsCircle = removeExistingFriends(candidates, currentFriendsCircle); + candidates = candidates.concat(currentFriendsCircle); + currentFriendsCircle = getNextCircle(currentFriendsCircle) + .map(function (name) { + return getFriendByName(name, friends); + }); + currentLevel++; } return candidates; From 6ff1e7023ad55160e316d22b9b3661ae55052d48 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 16:04:47 +0500 Subject: [PATCH 04/24] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=BE=D0=BD=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/lib.js b/lib.js index d7b26a4..ceb7d78 100644 --- a/lib.js +++ b/lib.js @@ -1,20 +1,5 @@ 'use strict'; -function clone(obj) { - if (obj === null || typeof obj !== 'object') { - return obj; - } - - var temp = obj.constructor(); - var keys = Object.keys(obj); - for (var i = 0; i < keys.length; i++) { - var key = keys[i]; - temp[key] = clone(obj[key]); - } - - return temp; -} - function getNextCircle(friends) { return friends.reduce(function (nextCircle, friend) { nextCircle = nextCircle.concat(friend.friends); @@ -27,13 +12,11 @@ function getFriendByName(friendName, friends) { return friend.name === friendName; }); - return clone(foundFriend); + return foundFriend; } function removeExistingFriends(candidates, friendsCircle) { return friendsCircle.filter(function (friend) { - return !candidates.some(function (candidate) { - return candidate.name === friend.name; - }); + return candidates.indexOf(friend) === -1; }); } function compare(a, b) { @@ -55,10 +38,10 @@ function getCandidate(friends) { currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { return compare(a.name, b.name); }); + currentFriendsCircle = removeExistingFriends(candidates, currentFriendsCircle); for (var i = 0; i < currentFriendsCircle.length; i++) { currentFriendsCircle[i].level = currentLevel; } - currentFriendsCircle = removeExistingFriends(candidates, currentFriendsCircle); candidates = candidates.concat(currentFriendsCircle); currentFriendsCircle = getNextCircle(currentFriendsCircle) .map(function (name) { From 566c78ff4ea5d96d9dbe7f95c819cefe34d87c85 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 16:41:00 +0500 Subject: [PATCH 05/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20next?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib.js b/lib.js index ceb7d78..a245bd3 100644 --- a/lib.js +++ b/lib.js @@ -2,9 +2,7 @@ function getNextCircle(friends) { return friends.reduce(function (nextCircle, friend) { - nextCircle = nextCircle.concat(friend.friends); - - return nextCircle; + return nextCircle.concat(friend.friends); }, []); } function getFriendByName(friendName, friends) { @@ -73,10 +71,13 @@ Iterator.prototype.done = function () { }; Iterator.prototype.next = function () { + if (this.done()) { + return null; + } var friend = this.invitedFriends.shift(); delete friend.level; - return friend || null; + return friend; }; /** From 59844e914d2842ba8cdce2c4113ed442579fb85f Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 18:30:53 +0500 Subject: [PATCH 06/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20getCandidate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/lib.js b/lib.js index a245bd3..2496140 100644 --- a/lib.js +++ b/lib.js @@ -1,9 +1,12 @@ 'use strict'; -function getNextCircle(friends) { - return friends.reduce(function (nextCircle, friend) { +function getNextCircle(oldCircle, friends) { + return oldCircle.reduce(function (nextCircle, friend) { return nextCircle.concat(friend.friends); - }, []); + }, []) + .map(function (name) { + return getFriendByName(name, friends); + }); } function getFriendByName(friendName, friends) { var foundFriend = friends.find(function (friend) { @@ -12,11 +15,11 @@ function getFriendByName(friendName, friends) { return foundFriend; } -function removeExistingFriends(candidates, friendsCircle) { - return friendsCircle.filter(function (friend) { - return candidates.indexOf(friend) === -1; - }); -} +// function removeExistingFriends(candidates, friendsCircle) { +// return friendsCircle.filter(function (friend) { +// return candidates.indexOf(friend) === -1; +// }); +// } function compare(a, b) { if (a > b) { return 1; @@ -32,20 +35,27 @@ function getCandidate(friends) { }); var candidates = []; var currentLevel = 1; - while (currentFriendsCircle.length > 0) { + while (currentFriendsCircle.length !== 0) { currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { return compare(a.name, b.name); }); - currentFriendsCircle = removeExistingFriends(candidates, currentFriendsCircle); + currentFriendsCircle = currentFriendsCircle.filter(function (friend) { + return candidates.indexOf(friend) === -1; + }); + currentFriendsCircle.forEach(function (friend) { + if (candidates.indexOf(friend) === -1) { + candidates.push(friend); + } + }); for (var i = 0; i < currentFriendsCircle.length; i++) { currentFriendsCircle[i].level = currentLevel; } - candidates = candidates.concat(currentFriendsCircle); - currentFriendsCircle = getNextCircle(currentFriendsCircle) - .map(function (name) { - return getFriendByName(name, friends); - }); + currentFriendsCircle = getNextCircle(currentFriendsCircle, friends); currentLevel++; + + // currentFriendsCircle = removeExistingFriends(candidates, currentFriendsCircle); + // candidates = candidates.concat(currentFriendsCircle); + } return candidates; From 1f5c7c651b40c5500ca167f010c0849cf93a1411 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 21:46:42 +0500 Subject: [PATCH 07/24] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20getCandidate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib.js b/lib.js index 2496140..9af0734 100644 --- a/lib.js +++ b/lib.js @@ -38,24 +38,20 @@ function getCandidate(friends) { while (currentFriendsCircle.length !== 0) { currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { return compare(a.name, b.name); - }); - currentFriendsCircle = currentFriendsCircle.filter(function (friend) { - return candidates.indexOf(friend) === -1; - }); - currentFriendsCircle.forEach(function (friend) { - if (candidates.indexOf(friend) === -1) { - candidates.push(friend); - } + }) + .filter(function (friend) { + return !candidates.some(function (candidate) { + return candidate.friend === friend; + }); }); for (var i = 0; i < currentFriendsCircle.length; i++) { - currentFriendsCircle[i].level = currentLevel; + candidates.push({ + friend: currentFriendsCircle[i], + level: currentLevel + }); } currentFriendsCircle = getNextCircle(currentFriendsCircle, friends); currentLevel++; - - // currentFriendsCircle = removeExistingFriends(candidates, currentFriendsCircle); - // candidates = candidates.concat(currentFriendsCircle); - } return candidates; @@ -71,8 +67,8 @@ function Iterator(friends, filter) { if (!(filter instanceof Filter)) { throw new TypeError('Мне нужен ФИЛЬТР, а не ЭТО!!!'); } - this.invitedFriends = getCandidate(friends).filter(function (a) { - return filter.isRight(a); + this.invitedFriends = getCandidate(friends).filter(function (candidate) { + return filter.isRight(candidate.friend); }); } @@ -84,10 +80,9 @@ Iterator.prototype.next = function () { if (this.done()) { return null; } - var friend = this.invitedFriends.shift(); - delete friend.level; + var friendAndLevel = this.invitedFriends.shift(); - return friend; + return friendAndLevel.friend; }; /** From 4dc2920420909dfe13bd7153ce4ba43363ccf00a Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 21:52:17 +0500 Subject: [PATCH 08/24] fixed maxlevel --- lib.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib.js b/lib.js index 9af0734..9d71d1d 100644 --- a/lib.js +++ b/lib.js @@ -95,6 +95,7 @@ Iterator.prototype.next = function () { */ function LimitedIterator(friends, filter, maxLevel) { Iterator.call(this, friends, filter); + maxLevel = maxLevel > 0 ? maxLevel : 0; this.invitedFriends = this.invitedFriends.filter(function (friend) { return friend.level <= maxLevel; }); From e4edbec561125d23baebff40d6f70b973590a428 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 22:00:31 +0500 Subject: [PATCH 09/24] fixed getFriendByName --- lib.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib.js b/lib.js index 9d71d1d..2537ef7 100644 --- a/lib.js +++ b/lib.js @@ -9,11 +9,11 @@ function getNextCircle(oldCircle, friends) { }); } function getFriendByName(friendName, friends) { - var foundFriend = friends.find(function (friend) { + var foundFriends = friends.filter(function (friend) { return friend.name === friendName; }); - return foundFriend; + return foundFriends[0]; } // function removeExistingFriends(candidates, friendsCircle) { // return friendsCircle.filter(function (friend) { @@ -95,7 +95,6 @@ Iterator.prototype.next = function () { */ function LimitedIterator(friends, filter, maxLevel) { Iterator.call(this, friends, filter); - maxLevel = maxLevel > 0 ? maxLevel : 0; this.invitedFriends = this.invitedFriends.filter(function (friend) { return friend.level <= maxLevel; }); From 5f1a6f9ca562ac22baafe1754a2896039c6d4464 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 22:12:30 +0500 Subject: [PATCH 10/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D1=88=D0=B8=D1=85=20=D0=B4=D1=80=D1=83=D0=B7=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib.js b/lib.js index 2537ef7..bd12612 100644 --- a/lib.js +++ b/lib.js @@ -31,7 +31,7 @@ function compare(a, b) { } function getCandidate(friends) { var currentFriendsCircle = friends.filter(function (friend) { - return friend.hasOwnProperty('best'); + return friend.best; }); var candidates = []; var currentLevel = 1; From c2610633514ea14e2d539df70a7295236f9b3c5a Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 22:54:43 +0500 Subject: [PATCH 11/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lib.js b/lib.js index bd12612..4faac9c 100644 --- a/lib.js +++ b/lib.js @@ -20,15 +20,7 @@ function getFriendByName(friendName, friends) { // return candidates.indexOf(friend) === -1; // }); // } -function compare(a, b) { - if (a > b) { - return 1; - } else if (a < b) { - return -1; - } - return 0; -} function getCandidate(friends) { var currentFriendsCircle = friends.filter(function (friend) { return friend.best; @@ -37,11 +29,11 @@ function getCandidate(friends) { var currentLevel = 1; while (currentFriendsCircle.length !== 0) { currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { - return compare(a.name, b.name); + return a.name.localeCompare(b.name); }) - .filter(function (friend) { + .filter(function (currentFriend) { return !candidates.some(function (candidate) { - return candidate.friend === friend; + return candidate.friend === currentFriend; }); }); for (var i = 0; i < currentFriendsCircle.length; i++) { From 3fde07df00a703674ab6c32c1cf9bec94500469d Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 23:06:26 +0500 Subject: [PATCH 12/24] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BC=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B8=D0=BB=20=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib.js b/lib.js index 4faac9c..28f740b 100644 --- a/lib.js +++ b/lib.js @@ -30,11 +30,6 @@ function getCandidate(friends) { while (currentFriendsCircle.length !== 0) { currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { return a.name.localeCompare(b.name); - }) - .filter(function (currentFriend) { - return !candidates.some(function (candidate) { - return candidate.friend === currentFriend; - }); }); for (var i = 0; i < currentFriendsCircle.length; i++) { candidates.push({ @@ -42,7 +37,12 @@ function getCandidate(friends) { level: currentLevel }); } - currentFriendsCircle = getNextCircle(currentFriendsCircle, friends); + currentFriendsCircle = getNextCircle(currentFriendsCircle, friends) + .filter(function (currentFriend) { + return !candidates.some(function (candidate) { + return candidate.friend === currentFriend; + }); + }); currentLevel++; } From 65a29011dc12e84eea50d1cd80c18eeba9db166b Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 23:11:26 +0500 Subject: [PATCH 13/24] =?UTF-8?q?=D0=91=D0=B5=D1=81=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D0=B5=D0=B7=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8?= =?UTF-8?q?=D1=82.=20=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB,=20=D1=82=D0=B0?= =?UTF-8?q?=D0=BA=20=D0=BA=D0=B0=D0=BA=20=D0=B4=D0=BE=D0=BB=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=BF=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib.js b/lib.js index 28f740b..5cbb076 100644 --- a/lib.js +++ b/lib.js @@ -49,6 +49,7 @@ function getCandidate(friends) { return candidates; } + /** * Итератор по друзьям * @constructor From fc1406c16b125de9ffb6c8c1f837be8624b40da8 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 23:19:18 +0500 Subject: [PATCH 14/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=82=D0=B5=D1=81=D0=BA=D1=82=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?TypeError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib.js b/lib.js index 5cbb076..57c9d5a 100644 --- a/lib.js +++ b/lib.js @@ -15,11 +15,6 @@ function getFriendByName(friendName, friends) { return foundFriends[0]; } -// function removeExistingFriends(candidates, friendsCircle) { -// return friendsCircle.filter(function (friend) { -// return candidates.indexOf(friend) === -1; -// }); -// } function getCandidate(friends) { var currentFriendsCircle = friends.filter(function (friend) { @@ -49,7 +44,6 @@ function getCandidate(friends) { return candidates; } - /** * Итератор по друзьям * @constructor @@ -58,7 +52,7 @@ function getCandidate(friends) { */ function Iterator(friends, filter) { if (!(filter instanceof Filter)) { - throw new TypeError('Мне нужен ФИЛЬТР, а не ЭТО!!!'); + throw new TypeError(); } this.invitedFriends = getCandidate(friends).filter(function (candidate) { return filter.isRight(candidate.friend); From 5f880be873f424c7b588e1994f8087846c5acda3 Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 23:23:30 +0500 Subject: [PATCH 15/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=84=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib.js b/lib.js index 57c9d5a..5b83f2b 100644 --- a/lib.js +++ b/lib.js @@ -34,9 +34,9 @@ function getCandidate(friends) { } currentFriendsCircle = getNextCircle(currentFriendsCircle, friends) .filter(function (currentFriend) { - return !candidates.some(function (candidate) { - return candidate.friend === currentFriend; - }); + return candidates.map(function (candidate) { + return candidate.friend; + }).indexOf(currentFriend) === -1; }); currentLevel++; } From 7e0cb965f14899123d9027db3f0c96acf2f6561e Mon Sep 17 00:00:00 2001 From: Pavel Date: Wed, 23 Nov 2016 23:40:05 +0500 Subject: [PATCH 16/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20done=20=D0=B8=20next?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib.js b/lib.js index 5b83f2b..649b45b 100644 --- a/lib.js +++ b/lib.js @@ -34,9 +34,9 @@ function getCandidate(friends) { } currentFriendsCircle = getNextCircle(currentFriendsCircle, friends) .filter(function (currentFriend) { - return candidates.map(function (candidate) { - return candidate.friend; - }).indexOf(currentFriend) === -1; + return !candidates.some(function (candidate) { + return candidate.friend === currentFriend; + }); }); currentLevel++; } @@ -44,7 +44,7 @@ function getCandidate(friends) { return candidates; } -/** + /** * Итератор по друзьям * @constructor * @param {Object[]} friends @@ -57,19 +57,21 @@ function Iterator(friends, filter) { this.invitedFriends = getCandidate(friends).filter(function (candidate) { return filter.isRight(candidate.friend); }); + this.currentIndex = 0; } Iterator.prototype.done = function () { - return this.invitedFriends.length === 0; + return this.invitedFriends.length === this.currentIndex; }; Iterator.prototype.next = function () { if (this.done()) { return null; } - var friendAndLevel = this.invitedFriends.shift(); + // var friendAndLevel = this.invitedFriends.shift(); + // return friendAndLevel.friend; - return friendAndLevel.friend; + return this.invitedFriends[this.currentIndex++].friend; }; /** From 5db6c31ce38c7cc2bde6197a346ddd84f289aa7f Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 01:33:44 +0500 Subject: [PATCH 17/24] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BB=20=D0=B7=D0=B0=D0=BD=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 108 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 58 insertions(+), 50 deletions(-) diff --git a/lib.js b/lib.js index 649b45b..ff70ed0 100644 --- a/lib.js +++ b/lib.js @@ -1,47 +1,27 @@ 'use strict'; -function getNextCircle(oldCircle, friends) { - return oldCircle.reduce(function (nextCircle, friend) { - return nextCircle.concat(friend.friends); - }, []) - .map(function (name) { - return getFriendByName(name, friends); +function removeFriendsNamesMatches(friendNamesAndLevels, potentiallyFriendsNames) { + return potentiallyFriendsNames.filter(function (potentiallyFriendName) { + return !friendNamesAndLevels.some(function (friendNameAndLevel) { + return friendNameAndLevel.name === potentiallyFriendName; + }); }); } -function getFriendByName(friendName, friends) { - var foundFriends = friends.filter(function (friend) { - return friend.name === friendName; - }); - return foundFriends[0]; +function getNextFriendsCircleNames(friends, oldCircleNames) { + return oldCircleNames.reduce(function (acc, oldCircleFriendName) { + var nextCicleNames = friends.find(function (friend) { + return friend.name === oldCircleFriendName; + }).friends; + + return acc.concat(nextCicleNames); + }, []); } -function getCandidate(friends) { - var currentFriendsCircle = friends.filter(function (friend) { - return friend.best; +function getFriendByName(name, friends) { + return friends.find(function (friend) { + return friend.name === name; }); - var candidates = []; - var currentLevel = 1; - while (currentFriendsCircle.length !== 0) { - currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { - return a.name.localeCompare(b.name); - }); - for (var i = 0; i < currentFriendsCircle.length; i++) { - candidates.push({ - friend: currentFriendsCircle[i], - level: currentLevel - }); - } - currentFriendsCircle = getNextCircle(currentFriendsCircle, friends) - .filter(function (currentFriend) { - return !candidates.some(function (candidate) { - return candidate.friend === currentFriend; - }); - }); - currentLevel++; - } - - return candidates; } /** @@ -54,24 +34,51 @@ function Iterator(friends, filter) { if (!(filter instanceof Filter)) { throw new TypeError(); } - this.invitedFriends = getCandidate(friends).filter(function (candidate) { - return filter.isRight(candidate.friend); + + var currentFriendsCircle = friends.filter(function (friend) { + return friend.best; + }) + .map(function (friend) { + return friend.name; + }); + var result = []; + var currentLevel = 1; + while (currentFriendsCircle.length !== 0) { + currentFriendsCircle = removeFriendsNamesMatches(result, currentFriendsCircle) + .sort(function (a, b) { + return a.localeCompare(b); + }); + for (var i = 0; i < currentFriendsCircle.length; i++) { + result.push({ + level: currentLevel, + name: currentFriendsCircle[i] + }); + } + currentFriendsCircle = getNextFriendsCircleNames(friends, currentFriendsCircle); + currentLevel++; + } + + this.invitedFriends = result.map(function (nameAndLevel) { + return { + friend: getFriendByName(nameAndLevel.name, friends), + level: nameAndLevel.level + }; + }) + .filter(function (item) { + return filter.test(item.friend); }); - this.currentIndex = 0; } Iterator.prototype.done = function () { - return this.invitedFriends.length === this.currentIndex; + return !this.invitedFriends.length; }; Iterator.prototype.next = function () { if (this.done()) { return null; } - // var friendAndLevel = this.invitedFriends.shift(); - // return friendAndLevel.friend; - return this.invitedFriends[this.currentIndex++].friend; + return this.invitedFriends.shift().friend; }; /** @@ -84,8 +91,9 @@ Iterator.prototype.next = function () { */ function LimitedIterator(friends, filter, maxLevel) { Iterator.call(this, friends, filter); - this.invitedFriends = this.invitedFriends.filter(function (friend) { - return friend.level <= maxLevel; + + this.invitedFriends = this.invitedFriends.filter(function (item) { + return item.level <= maxLevel; }); } @@ -96,7 +104,7 @@ LimitedIterator.prototype = Object.create(Iterator.prototype); * @constructor */ function Filter() { - this.isRight = function () { + this.test = function () { return true; }; } @@ -107,8 +115,8 @@ function Filter() { * @constructor */ function MaleFilter() { - this.isRight = function (human) { - return human.gender === 'male'; + this.test = function (item) { + return item.gender === 'male'; }; } MaleFilter.prototype = Object.create(Filter.prototype); @@ -119,8 +127,8 @@ MaleFilter.prototype = Object.create(Filter.prototype); * @constructor */ function FemaleFilter() { - this.isRight = function (human) { - return human.gender === 'female'; + this.test = function (item) { + return item.gender === 'female'; }; } FemaleFilter.prototype = Object.create(Filter.prototype); From 41548950bd02bc2fb8320af0fbcae753fba8f854 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 02:33:17 +0500 Subject: [PATCH 18/24] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=86=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D1=81=D0=BB=D0=B5=D0=B4=D1=83=D1=8E=D1=89=D0=B5=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BA=D1=80=D1=83=D0=B3=D0=B0=20=D0=B4=D1=80=D1=83?= =?UTF-8?q?=D0=B7=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib.js b/lib.js index ff70ed0..0bd3103 100644 --- a/lib.js +++ b/lib.js @@ -1,18 +1,21 @@ 'use strict'; -function removeFriendsNamesMatches(friendNamesAndLevels, potentiallyFriendsNames) { - return potentiallyFriendsNames.filter(function (potentiallyFriendName) { - return !friendNamesAndLevels.some(function (friendNameAndLevel) { - return friendNameAndLevel.name === potentiallyFriendName; +// function removeFriendsNamesMatches(friendNamesAndLevels, potentiallyFriendsNames) { +// return potentiallyFriendsNames.filter(function (potentiallyFriendName) { +// return !friendNamesAndLevels.some(function (friendNameAndLevel) { +// return friendNameAndLevel.name === potentiallyFriendName; +// }); +// }); +// } + +function getNextFriendsCircleNames(friends, friendsNames, oldFriendsCircle) { + return oldFriendsCircle.reduce(function (acc, friendName) { + var nextCicleNames = getFriendByName(friendName, friends).friends.filter(function (friend) { + return friendsNames.indexOf(friend) === -1; }); - }); -} - -function getNextFriendsCircleNames(friends, oldCircleNames) { - return oldCircleNames.reduce(function (acc, oldCircleFriendName) { - var nextCicleNames = friends.find(function (friend) { - return friend.name === oldCircleFriendName; - }).friends; + // var nextCicleNames = friends.find(function (friend) { + // return friend.name === oldCircleFriendName; + // }).friends; return acc.concat(nextCicleNames); }, []); @@ -43,18 +46,23 @@ function Iterator(friends, filter) { }); var result = []; var currentLevel = 1; + var friendsNames = []; while (currentFriendsCircle.length !== 0) { - currentFriendsCircle = removeFriendsNamesMatches(result, currentFriendsCircle) - .sort(function (a, b) { + // currentFriendsCircle = removeFriendsNamesMatches(result, currentFriendsCircle) + currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { return a.localeCompare(b); }); + for (var i = 0; i < currentFriendsCircle.length; i++) { + var friendName = currentFriendsCircle[i]; result.push({ level: currentLevel, - name: currentFriendsCircle[i] + name: friendName }); + friendsNames.push(friendName); } - currentFriendsCircle = getNextFriendsCircleNames(friends, currentFriendsCircle); + currentFriendsCircle = getNextFriendsCircleNames(friends, friendsNames, + currentFriendsCircle); currentLevel++; } From 7355d87fbcf60c59343ed1186fe6ebf78c9a5d62 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 02:59:05 +0500 Subject: [PATCH 19/24] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4,=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80?= =?UTF-8?q?=D0=B0=D1=89=D0=B0=D1=8E=D1=89=D0=B8=D0=B9=20=D1=81=D0=BB=D0=B5?= =?UTF-8?q?=D0=B4=D1=83=D1=8E=D1=89=D0=B8=D0=B9=20=D0=BA=D1=80=D1=83=D0=B3?= =?UTF-8?q?=20=D0=B4=D1=80=D1=83=D0=B7=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 58 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/lib.js b/lib.js index 0bd3103..460706f 100644 --- a/lib.js +++ b/lib.js @@ -8,18 +8,16 @@ // }); // } -function getNextFriendsCircleNames(friends, friendsNames, oldFriendsCircle) { - return oldFriendsCircle.reduce(function (acc, friendName) { - var nextCicleNames = getFriendByName(friendName, friends).friends.filter(function (friend) { - return friendsNames.indexOf(friend) === -1; - }); - // var nextCicleNames = friends.find(function (friend) { - // return friend.name === oldCircleFriendName; - // }).friends; - - return acc.concat(nextCicleNames); - }, []); -} +// function getNextFriendsCircleNames(friends, friendsNames, oldFriendsCircle) { + // return oldFriendsCircle.reduce(function (acc, friendName) { + // var nextCicleNames = getFriendByName(friendName, + // friends).friends.filter(function (friend) { + // return friendsNames.indexOf(friend) === -1; + // }); + // + // return acc.concat(nextCicleNames); + // }, []); +// } function getFriendByName(name, friends) { return friends.find(function (friend) { @@ -27,17 +25,7 @@ function getFriendByName(name, friends) { }); } - /** - * Итератор по друзьям - * @constructor - * @param {Object[]} friends - * @param {Filter} filter - */ -function Iterator(friends, filter) { - if (!(filter instanceof Filter)) { - throw new TypeError(); - } - +function getInvitedFriends(friends) { var currentFriendsCircle = friends.filter(function (friend) { return friend.best; }) @@ -48,11 +36,10 @@ function Iterator(friends, filter) { var currentLevel = 1; var friendsNames = []; while (currentFriendsCircle.length !== 0) { - // currentFriendsCircle = removeFriendsNamesMatches(result, currentFriendsCircle) + var nextLevelNames = []; currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { return a.localeCompare(b); }); - for (var i = 0; i < currentFriendsCircle.length; i++) { var friendName = currentFriendsCircle[i]; result.push({ @@ -60,13 +47,28 @@ function Iterator(friends, filter) { name: friendName }); friendsNames.push(friendName); + nextLevelNames = nextLevelNames.concat(getFriendByName(friendName, friends).friends); } - currentFriendsCircle = getNextFriendsCircleNames(friends, friendsNames, - currentFriendsCircle); + currentFriendsCircle = nextLevelNames.filter(function (name) { + return friendsNames.indexOf(name) === -1; + }); currentLevel++; } - this.invitedFriends = result.map(function (nameAndLevel) { + return result; +} + + /** + * Итератор по друзьям + * @constructor + * @param {Object[]} friends + * @param {Filter} filter + */ +function Iterator(friends, filter) { + if (!(filter instanceof Filter)) { + throw new TypeError(); + } + this.invitedFriends = getInvitedFriends(friends).map(function (nameAndLevel) { return { friend: getFriendByName(nameAndLevel.name, friends), level: nameAndLevel.level From ca58f316409e3e2ad7d097424a75785fae393088 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 03:21:12 +0500 Subject: [PATCH 20/24] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=81=D0=BE=20=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 14 +++++++------- lib.js | 23 +++++++++-------------- lib.spec.js | 3 ++- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 0253060..c38efb8 100644 --- a/index.js +++ b/index.js @@ -5,26 +5,26 @@ var lib = require('./lib'); var friends = [ { name: 'Sam', - friends: ['Mat', 'Sharon'], + friends: ['Sharon', 'Mat'], gender: 'male', best: true }, { name: 'Sally', - friends: ['Brad', 'Emily'], + friends: ['Emily', 'Brad'], gender: 'female', best: true }, - { - name: 'Mat', - friends: ['Sam', 'Sharon'], - gender: 'male' - }, { name: 'Sharon', friends: ['Sam', 'Itan', 'Mat'], gender: 'female' }, + { + name: 'Mat', + friends: ['Sam', 'Sharon'], + gender: 'male' + }, { name: 'Brad', friends: ['Sally', 'Emily', 'Julia'], diff --git a/lib.js b/lib.js index 460706f..017716e 100644 --- a/lib.js +++ b/lib.js @@ -28,9 +28,6 @@ function getFriendByName(name, friends) { function getInvitedFriends(friends) { var currentFriendsCircle = friends.filter(function (friend) { return friend.best; - }) - .map(function (friend) { - return friend.name; }); var result = []; var currentLevel = 1; @@ -38,19 +35,22 @@ function getInvitedFriends(friends) { while (currentFriendsCircle.length !== 0) { var nextLevelNames = []; currentFriendsCircle = currentFriendsCircle.sort(function (a, b) { - return a.localeCompare(b); + return a.name.localeCompare(b.name); }); for (var i = 0; i < currentFriendsCircle.length; i++) { - var friendName = currentFriendsCircle[i]; + var friend = currentFriendsCircle[i]; result.push({ level: currentLevel, - name: friendName + friend: friend }); - friendsNames.push(friendName); - nextLevelNames = nextLevelNames.concat(getFriendByName(friendName, friends).friends); + friendsNames.push(friend.name); + nextLevelNames = nextLevelNames.concat(friend.friends); } currentFriendsCircle = nextLevelNames.filter(function (name) { return friendsNames.indexOf(name) === -1; + }) + .map(function (name) { + return getFriendByName(name, friends); }); currentLevel++; } @@ -68,12 +68,7 @@ function Iterator(friends, filter) { if (!(filter instanceof Filter)) { throw new TypeError(); } - this.invitedFriends = getInvitedFriends(friends).map(function (nameAndLevel) { - return { - friend: getFriendByName(nameAndLevel.name, friends), - level: nameAndLevel.level - }; - }) + this.invitedFriends = getInvitedFriends(friends) .filter(function (item) { return filter.test(item.friend); }); diff --git a/lib.spec.js b/lib.spec.js index 91de106..92814e9 100644 --- a/lib.spec.js +++ b/lib.spec.js @@ -67,7 +67,8 @@ describe('Итераторы', function () { while (!femaleIterator.done()) { invitedFriends.push(femaleIterator.next()); } - + // console.info(friends); + // console.info(invitedFriends); assert.deepStrictEqual(invitedFriends, [ [friend('Sam'), friend('Sally')], [friend('Brad'), friend('Emily')], From 129fa193407188df62f6e5dac884f724345861f1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 03:32:55 +0500 Subject: [PATCH 21/24] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20done,=20next=20=D0=B8=20getFriendByName?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/lib.js b/lib.js index 017716e..d9ea4ca 100644 --- a/lib.js +++ b/lib.js @@ -1,28 +1,9 @@ 'use strict'; -// function removeFriendsNamesMatches(friendNamesAndLevels, potentiallyFriendsNames) { -// return potentiallyFriendsNames.filter(function (potentiallyFriendName) { -// return !friendNamesAndLevels.some(function (friendNameAndLevel) { -// return friendNameAndLevel.name === potentiallyFriendName; -// }); -// }); -// } - -// function getNextFriendsCircleNames(friends, friendsNames, oldFriendsCircle) { - // return oldFriendsCircle.reduce(function (acc, friendName) { - // var nextCicleNames = getFriendByName(friendName, - // friends).friends.filter(function (friend) { - // return friendsNames.indexOf(friend) === -1; - // }); - // - // return acc.concat(nextCicleNames); - // }, []); -// } - function getFriendByName(name, friends) { - return friends.find(function (friend) { + return friends.filter(function (friend) { return friend.name === name; - }); + })[0]; } function getInvitedFriends(friends) { @@ -72,10 +53,12 @@ function Iterator(friends, filter) { .filter(function (item) { return filter.test(item.friend); }); + + this.index = 0; } Iterator.prototype.done = function () { - return !this.invitedFriends.length; + return this.invitedFriends.length <= this.index; }; Iterator.prototype.next = function () { @@ -83,7 +66,7 @@ Iterator.prototype.next = function () { return null; } - return this.invitedFriends.shift().friend; + return this.invitedFriends[this.index++].friend; }; /** From d74d5783c861807bfc55672d409bc23f5aba6510 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 03:35:14 +0500 Subject: [PATCH 22/24] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B1=D1=80=D0=BE=D1=81=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BA=D0=B8.=20=D0=9F=D1=80=D0=BE=D1=81=D1=82=D0=BE=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib.js b/lib.js index d9ea4ca..24c403a 100644 --- a/lib.js +++ b/lib.js @@ -46,9 +46,9 @@ function getInvitedFriends(friends) { * @param {Filter} filter */ function Iterator(friends, filter) { - if (!(filter instanceof Filter)) { - throw new TypeError(); - } + // if (!(filter instanceof Filter)) { + // throw new TypeError(); + // } this.invitedFriends = getInvitedFriends(friends) .filter(function (item) { return filter.test(item.friend); From 5097464357d46c56f1f9b1cdbb9fd0a650d4c3f7 Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 03:42:32 +0500 Subject: [PATCH 23/24] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=80=D0=BE=D0=B1=D1=80=D0=BE=D1=81=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib.js b/lib.js index 24c403a..edaf451 100644 --- a/lib.js +++ b/lib.js @@ -46,9 +46,9 @@ function getInvitedFriends(friends) { * @param {Filter} filter */ function Iterator(friends, filter) { - // if (!(filter instanceof Filter)) { - // throw new TypeError(); - // } + if (!(filter instanceof Filter)) { + throw TypeError; + } this.invitedFriends = getInvitedFriends(friends) .filter(function (item) { return filter.test(item.friend); From 0fc1e346414f998acaa63b2fcb0645a3c592f8ce Mon Sep 17 00:00:00 2001 From: Pavel Date: Mon, 28 Nov 2016 03:49:25 +0500 Subject: [PATCH 24/24] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=80=D0=BE=D0=B1=D1=80=D0=BE=D1=81=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib.js b/lib.js index edaf451..9787333 100644 --- a/lib.js +++ b/lib.js @@ -47,7 +47,7 @@ function getInvitedFriends(friends) { */ function Iterator(friends, filter) { if (!(filter instanceof Filter)) { - throw TypeError; + throw new TypeError('no filter'); } this.invitedFriends = getInvitedFriends(friends) .filter(function (item) {