src/helpers/utils.ts   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 3.5

Size

Lines of Code 43
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 24
mnd 5
bc 5
fnc 2
dl 0
loc 43
rs 10
bpm 2.5
cpm 3.5
noi 0
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A utils.ts ➔ buildOptionsFromMessage 0 18 3
A utils.ts ➔ splitMessage 0 20 4
1
import {PollConfig} from './interfaces';
2
3
/**
4
 * Split string/message to topic and choice
5
 * reference: https://stackoverflow.com/a/18647776/2671470
6
 *
7
 * @param {string} message - the new option name
8
 * @returns {array} card
9
 */
10
export function splitMessage(message: string) {
11
  const expression = /[^\s"]+|"([^"]*)"/gi;
12
  const result = [];
13
  let match;
14
  do {
15
    match = expression.exec(message);
16
    if (match != null) {
17
      result.push(match[1] ? match[1] : match[0]);
18
    }
19
  } while (match != null);
20
21
  return result;
22
}
23
24
/**
25
 * Build poll options from message sent by user.
26
 *
27
 * @param {string} message - message or text after poll command
28
 * @returns {object} option
29
 */
30
export function buildOptionsFromMessage(message: string): PollConfig {
31
  const explodedMesage = splitMessage(message);
32
  const topic = explodedMesage[0] !== 'undefined' && explodedMesage[0] ?
33
    explodedMesage[0] :
34
    '';
35
  if (explodedMesage.length > 0) {
36
    explodedMesage.shift();
37
  }
38
  return {
39
    topic,
40
    choices: explodedMesage,
41
  };
42
}
43