-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlib.js
More file actions
150 lines (129 loc) · 3.77 KB
/
lib.js
File metadata and controls
150 lines (129 loc) · 3.77 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
function compareAlphabetically(person1, person2) {
if (person1.name > person2.name) {
return 1;
}
if (person1.name < person2.name) {
return -1;
}
return 0;
}
function getFirstCircle(friends) {
return friends.filter(function (person) {
return person.best === true;
}).sort(compareAlphabetically);
}
function getNextCircle(friends, currFriends, listGuests) {
currFriends.sort(compareAlphabetically).forEach(function (person) {
if (listGuests.indexOf(person) === -1) {
listGuests.push(person);
person.friends.forEach(function (name) {
var guest = friends.filter(function (friend) {
return friend.name === name;
})[0];
currFriends.push(guest);
});
}
});
return [currFriends, listGuests];
}
function getGuests(friends, filter, maxLevel) {
var currFriends = [].concat(getFirstCircle(friends));
var listGuests = [];
var level = maxLevel;
while (level > 0 && currFriends.length !== 0) {
var countFriends = currFriends.length;
var resultNextCircle = getNextCircle (friends, currFriends, listGuests);
currFriends = resultNextCircle[0];
listGuests = resultNextCircle[1];
currFriends.splice(0, countFriends);
level--;
}
return listGuests.filter(function (friend) {
return filter.filterFriends(friend);
});
}
/**
* Итератор по друзьям
* @constructor
* @param {Object[]} friends
* @param {Filter} filter
*/
function Iterator(friends, filter) {
if (!(filter instanceof Filter)) {
throw new TypeError('Incorrect data type Filter');
}
this.index = 0;
this.listGuests = getGuests(friends, filter, Infinity);
// this.listGuests.reverse();
}
Iterator.prototype.done = function () {
// return this.listGuests.length <= 0;
return this.index === this.listGuests.length;
};
Iterator.prototype.next = function () {
if (this.done()) {
return null;
}
var guest = this.listGuests[this.index];
this.index++;
return guest;
// return this.listGuests.pop();
};
/**
* Итератор по друзям с ограничением по кругу
* @extends Iterator
* @constructor
* @param {Object[]} friends
* @param {Filter} filter
* @param {Number} maxLevel – максимальный круг друзей
*/
function LimitedIterator(friends, filter, maxLevel) {
maxLevel = (typeof maxLevel === 'undefined') ? 0 : maxLevel;
if (!(filter instanceof Filter)) {
throw new TypeError('Incorrect data type Filter');
}
this.index = 0;
this.listGuests = getGuests(friends, filter, maxLevel);
// this.listGuests.reverse();
}
LimitedIterator.prototype = Object.create(Iterator.prototype);
LimitedIterator.prototype.constructor = LimitedIterator;
/**
* Фильтр друзей
* @constructor
*/
function Filter() {
this.filter = function () {
return true;
};
}
/**
* Фильтр друзей
* @extends Filter
* @constructor
*/
function MaleFilter() {
this.filterFriends = function (friend) {
return friend.gender === 'male';
};
}
MaleFilter.prototype = Object.create(Filter.prototype);
MaleFilter.prototype.constructor = MaleFilter;
/**
* Фильтр друзей-девушек
* @extends Filter
* @constructor
*/
function FemaleFilter() {
this.filterFriends = function (friend) {
return friend.gender === 'female';
};
}
FemaleFilter.prototype = Object.create(Filter.prototype);
FemaleFilter.prototype.constructor = FemaleFilter;
exports.Iterator = Iterator;
exports.LimitedIterator = LimitedIterator;
exports.Filter = Filter;
exports.MaleFilter = MaleFilter;
exports.FemaleFilter = FemaleFilter;