Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Мозалов Павел #20

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
893f87d
Сделал задание
MozalovPavel Nov 22, 2016
a71224b
Добавил сортировку для лучших друзей
MozalovPavel Nov 23, 2016
fe435d4
Изменил сортировку
MozalovPavel Nov 23, 2016
6ff1e70
Убрал клонирование
MozalovPavel Nov 23, 2016
566c78f
Изменил next
MozalovPavel Nov 23, 2016
59844e9
Изменил метод getCandidate
MozalovPavel Nov 23, 2016
1f5c7c6
Исправил метод getCandidate
MozalovPavel Nov 23, 2016
4dc2920
fixed maxlevel
MozalovPavel Nov 23, 2016
e4edbec
fixed getFriendByName
MozalovPavel Nov 23, 2016
5f1a6f9
Изменил поиск лучших друзей
MozalovPavel Nov 23, 2016
c261063
Изменил сортировку
MozalovPavel Nov 23, 2016
3fde07d
Переместил сортировку
MozalovPavel Nov 23, 2016
65a2901
Бесполезный коммит. Сделал, так как долго не проходили тесты
MozalovPavel Nov 23, 2016
fc1406c
Изменил тескт для TypeError
MozalovPavel Nov 23, 2016
5f880be
Изменил фильтрацию
MozalovPavel Nov 23, 2016
7e0cb96
Изменил done и next
MozalovPavel Nov 23, 2016
5db6c31
Переписал заново
MozalovPavel Nov 27, 2016
4154895
Исправил функцию возврацения следующего круга друзей
MozalovPavel Nov 27, 2016
7355d87
Убрал метод, возвращающий следующий круг друзей
MozalovPavel Nov 27, 2016
ca58f31
Убрал работу со строками
MozalovPavel Nov 27, 2016
129fa19
Изменил done, next и getFriendByName
MozalovPavel Nov 27, 2016
d74d578
Убрал проброс ошибки. Просто проверка
MozalovPavel Nov 27, 2016
5097464
Добавил проброс исключения
MozalovPavel Nov 27, 2016
0fc1e34
Добавил проброс исключения
MozalovPavel Nov 27, 2016
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
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
85 changes: 79 additions & 6 deletions lib.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,74 @@
'use strict';

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

function getInvitedFriends(friends) {
var currentFriendsCircle = friends.filter(function (friend) {
return friend.best;
});
var result = [];
var currentLevel = 1;
var friendsNames = [];
while (currentFriendsCircle.length !== 0) {
var nextLevelNames = [];
currentFriendsCircle = currentFriendsCircle.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
for (var i = 0; i < currentFriendsCircle.length; i++) {
var friend = currentFriendsCircle[i];
result.push({
level: currentLevel,
friend: friend
});
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++;
}

return result;
}

/**
* Итератор по друзьям
* @constructor
* @param {Object[]} friends
* @param {Filter} filter
*/
function Iterator(friends, filter) {
console.info(friends, filter);
if (!(filter instanceof Filter)) {
throw new TypeError('no filter');
}
this.invitedFriends = getInvitedFriends(friends)
.filter(function (item) {
return filter.test(item.friend);
});

this.index = 0;
}

Iterator.prototype.done = function () {
return this.invitedFriends.length <= this.index;
};

Iterator.prototype.next = function () {
if (this.done()) {
return null;
}

return this.invitedFriends[this.index++].friend;
};

/**
* Итератор по друзям с ограничением по кругу
* @extends Iterator
Expand All @@ -19,15 +78,23 @@ 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 (item) {
return item.level <= maxLevel;
});
}

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

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

/**
Expand All @@ -36,17 +103,23 @@ function Filter() {
* @constructor
*/
function MaleFilter() {
console.info('MaleFilter');
this.test = function (item) {
return item.gender === 'male';
};
}
MaleFilter.prototype = Object.create(Filter.prototype);

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

exports.Iterator = Iterator;
exports.LimitedIterator = LimitedIterator;
Expand Down
3 changes: 2 additions & 1 deletion lib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
Expand Down