-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
44 lines (38 loc) · 926 Bytes
/
helpers.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
/**
* Существует ли юзер с таким именем
*
* @param {String} name
* @param {Object} data
*/
function userExists(name, data) {
const users = data.filter(({ nameComponents }) => {
const formatName = name.split(/\s+/gm).reduce((prev, el) => {
if (!el) {
return prev;
}
prev.push(el.toLowerCase());
return prev;
}, []);
return formatName.every((el) => ~nameComponents.indexOf(el));
});
if (users.length === 1) {
return users.pop();
} if (users.length > 1) {
throw new Error('Найдено несколько участников. Уточните запрос');
} else {
throw new Error('Участник не найден');
}
}
/**
*
* @param {String} name
* @param {Object} ctx
* @return {Object}
*/
function getFriendDataByName(name, ctx) {
return userExists(name, ctx.session.friends);
}
module.exports = {
userExists,
getFriendDataByName,
};