-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlib.js
More file actions
129 lines (113 loc) · 3.12 KB
/
lib.js
File metadata and controls
129 lines (113 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'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) {
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
* @constructor
* @param {Object[]} friends
* @param {Filter} filter
* @param {Number} maxLevel – максимальный круг друзей
*/
function LimitedIterator(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() {
this.test = function () {
return true;
};
}
/**
* Фильтр друзей
* @extends Filter
* @constructor
*/
function MaleFilter() {
this.test = function (item) {
return item.gender === 'male';
};
}
MaleFilter.prototype = Object.create(Filter.prototype);
/**
* Фильтр друзей-девушек
* @extends Filter
* @constructor
*/
function FemaleFilter() {
this.test = function (item) {
return item.gender === 'female';
};
}
FemaleFilter.prototype = Object.create(Filter.prototype);
exports.Iterator = Iterator;
exports.LimitedIterator = LimitedIterator;
exports.Filter = Filter;
exports.MaleFilter = MaleFilter;
exports.FemaleFilter = FemaleFilter;