-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathPoll.js
42 lines (30 loc) · 1.04 KB
/
Poll.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
/*
* Copyright (c) 2018-present, Evgeny Nadymov
*
* This source code is licensed under the GPL v.3.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { POLL_OPTION_LENGTH, POLL_QUESTION_LENGTH } from '../Constants';
export function isValidPoll(poll) {
if (!poll) return false;
const { question, options } = poll;
if (!isValidQuestion(question)) return false;
const noneEmptyOptions = options.filter(x => Boolean(x.text));
if (noneEmptyOptions.length <= 1) return false;
return noneEmptyOptions.every(isValidOption);
}
function isValidQuestion(question) {
if (!question) return false;
return question.length <= POLL_QUESTION_LENGTH;
}
function isValidOption(option) {
if (!option) return false;
if (!option.text) return false;
return option.text.length <= POLL_OPTION_LENGTH;
}
export function hasPollData(poll) {
if (!poll) return false;
const { question, options } = poll;
if (question) return true;
return options.some(x => Boolean(x.text));
}