| Total Complexity | 7 |
| Complexity/F | 3.5 |
| Lines of Code | 43 |
| Function Count | 2 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 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 |