-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
44 lines (37 loc) · 1.07 KB
/
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
const constants = require("./constants");
const isEmptyObject = obj =>
obj && Object.keys(obj).length === 0 && obj.constructor === Object;
const shuffleArray = arr => {
for (
var j, x, i = arr.length;
i;
j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x
);
return arr;
};
const isHashtagInvalid = (str, allHashtags) => {
if (!str || !/(?:\s|^)#[A-Za-z0-9\-\.\_]+(?:\s|$)/g.test(str)) {
return constants.STMT_HASHTAG_INCORRECT_FORMAT;
}
if (
Object.values(allHashtags)
.reduce((acc, currentItem) => [...acc, ...currentItem], [])
.includes(str)
) {
return constants.STMT_HASHTAG_ALREADY_EXISTS;
}
return "";
};
const isPopularityInvalid = popularity => {
if (
!popularity ||
(popularity && !constants.POPULARITY_LEVEL.includes(popularity))
) {
return constants.STMT_HASHTAG_POPULARITY_NOT_EXISTS;
}
return "";
};
exports.isEmptyObject = isEmptyObject;
exports.shuffleArray = shuffleArray;
exports.isHashtagInvalid = isHashtagInvalid;
exports.isPopularityInvalid = isPopularityInvalid;