Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 95 additions & 5 deletions lib.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,90 @@
'use strict';

function getFriendByName(friends, name) {
return friends.filter(function (friend) {
return friend.name === name;
})[0];
}

function friendsLevel(friends) {

var bestFriends = friends.filter(function (friend) {
return friend.hasOwnProperty('best') && friend.best;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В принципе, можно заменить на Boolean(friend.best)

});

var namesOfFriends = [];
var friendsWithLevel = [];
bestFriends.forEach(function (friend) {
namesOfFriends.push(friend.name);
friendsWithLevel.push(
{
friend: friend,
level: 1
}
);
});

for (var i = 0; i < friendsWithLevel.length; i++) {
var newLevelFriends = friendsWithLevel[i].friend.friends.filter(function (friendName) {
return namesOfFriends.indexOf(friendName) === -1;
});
for (var j = 0; j < newLevelFriends.length; j++) {
friendsWithLevel.push(
{
friend: getFriendByName(friends, newLevelFriends[j]),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

было бы круто избавиться от этой функции, например храня в newLevelFriends не только имена а сразу объект друга

level: friendsWithLevel[i].level + 1
}
);
namesOfFriends.push(newLevelFriends[j]);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ Обычно цикл for используется для предсказуемого числа итераций. Тут явно не тот случай.
Я бы предложил использовать очередь, из которой выталкивать по одному элементу. Имеется фор по friendsWithLevel

}

friendsWithLevel.sort(function (friendOne, friendTwo) {
if (friendOne.level > friendTwo.level) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно красиво сделать через тернарник

return 1;
}
if (friendOne.level < friendTwo.level) {
return -1;
}
if (friendOne.friend.name > friendTwo.friend.name) {
return 1;
}

return -1;
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ А можно без этой сортировки в конце обойтись?


return friendsWithLevel;
}

/**
* Итератор по друзьям
* @constructor
* @param {Object[]} friends
* @param {Filter} filter
*/
function Iterator(friends, filter) {
console.info(friends, filter);
if (!Filter.prototype.isPrototypeOf(filter)) {
throw new TypeError();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше написать здесь ошибку

}
this.friendsWithLevel = friendsLevel(friends).filter(function (friend) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

все методы нужно разместить в прототипах

return filter.condition(friend.friend);
});

this.currentFriend = 0;
}

Iterator.prototype.next = function () {
if (this.currentFriend === this.friendsWithLevel.length) {
return null;
}

return this.friendsWithLevel[this.currentFriend++].friend;
};

Iterator.prototype.done = function () {
return this.currentFriend === this.friendsWithLevel.length;
};

/**
* Итератор по друзям с ограничением по кругу
* @extends Iterator
Expand All @@ -19,15 +94,22 @@ function Iterator(friends, filter) {
* @param {Number} maxLevel – максимальный круг друзей
*/
function LimitedIterator(friends, filter, maxLevel) {
console.info(friends, filter, maxLevel);
Iterator.call(this, friends, filter);
this.friendsWithLevel = this.friendsWithLevel.filter(function (friend) {
return maxLevel >= friend.level;
});
}

LimitedIterator.prototype = Object.create(Iterator.prototype);

/**
* Фильтр друзей
* @constructor
*/
function Filter() {
console.info('Filter');
this.condition = function () {
return true;
};
}

/**
Expand All @@ -36,18 +118,26 @@ function Filter() {
* @constructor
*/
function MaleFilter() {
console.info('MaleFilter');
this.condition = function (friend) {
return friend.gender === 'male';
};
}

MaleFilter.prototype = Object.create(Filter.prototype);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗️ Свйоство (new MaleFilter()).contructor теперь указывает на Filter


/**
* Фильтр друзей-девушек
* @extends Filter
* @constructor
*/
function FemaleFilter() {
console.info('FemaleFilter');
this.condition = function (friend) {
return friend.gender === 'female';
};
}

FemaleFilter.prototype = Object.create(Filter.prototype);

exports.Iterator = Iterator;
exports.LimitedIterator = LimitedIterator;

Expand Down