-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
35 lines (32 loc) · 1.01 KB
/
utils.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
module.exports = {
convertMentionToId: mentionString => {
// Remove first two characters '<' and '@'
return mentionString.substring(2, mentionString.length - 1);
},
shuffle: arr => {
const copy = [...arr];
let currentIndex = copy.length;
let temporaryValue;
let randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = copy[currentIndex];
copy[currentIndex] = copy[randomIndex];
copy[randomIndex] = temporaryValue;
}
return copy;
},
getMatching: arr => {
const matching = [];
if (arr.length <= 1) return [...arr];
arr.forEach((element, index) => {
if (index % 2 === 1) matching.push([arr[index - 1], element]);
if (index % 2 === 0 && index === arr.length - 1) matching.push([element]);
});
return matching;
},
};