From 605710d2cf20c48b3b8d04fb478c530b31da0a12 Mon Sep 17 00:00:00 2001 From: PavelTrofimov Date: Thu, 24 Nov 2016 00:35:06 +0600 Subject: [PATCH 1/2] init --- lib.js | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 5 deletions(-) diff --git a/lib.js b/lib.js index 60f30ab..12a375e 100644 --- a/lib.js +++ b/lib.js @@ -1,5 +1,65 @@ 'use strict'; +function findNote(friends, name) { + var newFriend; + friends.forEach(function (friend) { + if (friend.name === name) { + newFriend = friend; + } + }); + + return newFriend; +} + +function getFilteredFriends(friendsCircles, filter, maxLevel) { + var filteredFriends = []; + friendsCircles.slice(0, maxLevel).forEach(function (circle) { + circle.forEach(function (friend) { + if (filter.filterFunction(friend)) { + filteredFriends.push(friend); + } + }); + }); + + return filteredFriends; +} + + +function getCirclesOfFriends(friends) { + var circlesOfFriends = [[]]; + var checkedNames = []; + + friends.forEach(function (friend) { + if (friend.best === true) { + circlesOfFriends[0].push(friend); + checkedNames.push(friend.name); + } + }); + + for (var i = 0; i < circlesOfFriends.length; i++) { + createNewCircle(i); + } + + // В отдельной функции тк " Don't make functions within a loop" + function createNewCircle(circleId) { + var newCircle = []; + circlesOfFriends[circleId].forEach(function (friend) { + friend.friends.forEach(function (name) { + if (checkedNames.indexOf(name) === -1) { + checkedNames.push(name); + newCircle.push(findNote(friends, name)); + } + }); + }); + if (newCircle.length) { + circlesOfFriends.push(newCircle); + } + } + + return circlesOfFriends; +} + + /** * Итератор по друзьям * @constructor @@ -7,9 +67,39 @@ * @param {Filter} filter */ function Iterator(friends, filter) { - console.info(friends, filter); + + this.circlesOfFriends = getCirclesOfFriends(friends); + this.circlesOfFriends.forEach(function (circle) { + circle.sort(function (one, another) { + return one.name > another.name ? 1 : -1; + }); + }); + this.sortedCircles = this.circlesOfFriends; + + this.filteredFriends = getFilteredFriends( + this.sortedCircles, + filter, + this.sortedCircles.length + ); + this.currentIndex = 0; + } +Iterator.prototype.next = function () { + if (this.done()) { + return null; + } + + this.currentIndex++; + + return this.filteredFriends[this.currentIndex - 1]; +}; + +Iterator.prototype.done = function () { + return this.currentIndex === this.filteredFriends.length; +}; + + /** * Итератор по друзям с ограничением по кругу * @extends Iterator @@ -19,15 +109,20 @@ function Iterator(friends, filter) { * @param {Number} maxLevel – максимальный круг друзей */ function LimitedIterator(friends, filter, maxLevel) { - console.info(friends, filter, maxLevel); + Iterator.call(this, friends, filter); + this.filteredFriends = getFilteredFriends(this.sortedCircles, filter, maxLevel); } +LimitedIterator.prototype = Object.create(Iterator.prototype); + /** * Фильтр друзей * @constructor */ function Filter() { - console.info('Filter'); + this.filterFunction = function () { + return true; + }; } /** @@ -36,18 +131,26 @@ function Filter() { * @constructor */ function MaleFilter() { - console.info('MaleFilter'); + this.filterFunction = function (friend) { + return friend.gender === 'male'; + }; } +MaleFilter.prototype = Object.create(Filter.prototype); + /** * Фильтр друзей-девушек * @extends Filter * @constructor */ function FemaleFilter() { - console.info('FemaleFilter'); + this.filterFunction = function (friend) { + return friend.gender === 'female'; + }; } +FemaleFilter.prototype = Object.create(Filter.prototype); + exports.Iterator = Iterator; exports.LimitedIterator = LimitedIterator; From 7a4cb8394a59e5bed21fdf2ffc542cde4592075f Mon Sep 17 00:00:00 2001 From: PavelTrofimov Date: Thu, 24 Nov 2016 00:47:29 +0600 Subject: [PATCH 2/2] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20filter=20instanceof=20Filter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib.js b/lib.js index 12a375e..52a33db 100644 --- a/lib.js +++ b/lib.js @@ -1,16 +1,5 @@ 'use strict'; -function findNote(friends, name) { - var newFriend; - friends.forEach(function (friend) { - if (friend.name === name) { - newFriend = friend; - } - }); - - return newFriend; -} - function getFilteredFriends(friendsCircles, filter, maxLevel) { var filteredFriends = []; friendsCircles.slice(0, maxLevel).forEach(function (circle) { @@ -47,7 +36,7 @@ function getCirclesOfFriends(friends) { friend.friends.forEach(function (name) { if (checkedNames.indexOf(name) === -1) { checkedNames.push(name); - newCircle.push(findNote(friends, name)); + newCircle.push(findFriendByName(friends, name)); } }); }); @@ -59,6 +48,17 @@ function getCirclesOfFriends(friends) { return circlesOfFriends; } +function findFriendByName(friends, name) { + var newFriend; + friends.forEach(function (friend) { + if (friend.name === name) { + newFriend = friend; + } + }); + + return newFriend; +} + /** * Итератор по друзьям @@ -67,6 +67,9 @@ function getCirclesOfFriends(friends) { * @param {Filter} filter */ function Iterator(friends, filter) { + if (!(filter instanceof Filter)) { + throw new TypeError(); + } this.circlesOfFriends = getCirclesOfFriends(friends); this.circlesOfFriends.forEach(function (circle) {