forked from urfu-2015/javascript-tasks-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.js
120 lines (119 loc) · 3.71 KB
/
iterator.js
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
'use strict';
module.exports.get = function (collection, startPoint, depth) {
var friendsArray = getFriends(collection, startPoint, depth);
return {
currentFriend: -1,
next: function (name) {
if (friendsArray[this.currentFriend + 1] === undefined) {
return null;
}
this.currentFriend++;
if (!name) {
return this.current();
}
var index = 0;
friendsArray.forEach(function (item) {
if (item == name) {
index = friendsArray.indexOf(item);
}
});
this.currentFriend = index;
return this.current();
},
nextMale: function (name) {
if (startPoint === undefined) {
return null;
}
if (friendsArray[this.currentFriend + 1] === undefined) {
return null;
}
var currentfriend = this.next(name);
while (collection[currentfriend.name]) {
if (collection[currentfriend.name]['gender'] === 'Мужской') {
return currentfriend;
}
return this.next(name);
}
},
prevMale: function (name) {
if (startPoint === undefined) {
return null;
}
if (friendsArray[this.currentFriend + 1] === undefined) {
return null;
}
var currentfriend = this.prev();
while (collection[currentfriend.name]) {
if (collection[currentfriend.name]['gender'] === 'Мужской') {
return currentfriend;
}
return this.prev();
}
},
prev: function () {
if (startPoint === undefined) {
return null;
}
if (friendsArray[this.currentFriend - 1] === undefined) {
return null;
}
this.currentFriend --;
return this.current();
},
current: function () {
var friend = friendsArray[this.currentFriend];
return {name: friend['name'], phone: friend['phone'] };
}
};
};
function getFriends(collection, startPoint, depth) {
if (depth === undefined) {
depth = Object.keys(collection).length - 1;
}
if (collection[startPoint] === undefined) {
return [];
}
if (depth > Object.keys(collection).length) {
depth = Object.keys(collection).length - 1;
}
var friends = [];
var names = [startPoint];
friends.push(collection[startPoint]['friends']);
function singleName(name) {
var flag = false;
for (var k = 0; k < names.length; k++) {
if (name == names[k]) {
flag = true;
}
}
return flag;
}
function getFriend(name) {
if (!singleName(name)) {
friends.push(collection[name]['friends'].sort());
names.push(name);
}
return names;
}
for (var i = 0; i < depth - 1; i++) {
for (var j = 0; j < friends[i].length; j++) {
getFriend(friends[i][j]);
}
}
var result = [];
var names = [startPoint];
for (var i = 0; i < friends.length; i++) {
for (var j = 0; j < friends[i].length; j++) {
if (!singleName(friends[i][j])) {
//var friend = { name: }
result.push(friends[i][j]);
names.push(friends[i][j]);
}
}
}
return result.map(function (elem) {
var data = collection[elem];
data['name'] = elem;
return data;
});
}