Commit 0c77c866d3c25d684c3175896240810a5895a545

Authored by Adhidarma Hadiwinoto
1 parent 13b04dfe0d
Exists in master

ESLINT on command-parsers/command

Showing 1 changed file with 10 additions and 12 deletions Inline Diff

lib/command-parser.js
1 "use strict";
2
3 const commands = require('./command-group'); 1 const commands = require('./command-group');
4 2
5 function splitToken(msg) { 3 function splitToken(msg) {
6 if (typeof msg !== 'string') { 4 if (typeof msg !== 'string') {
7 return; 5 return null;
8 } 6 }
9 7
10 if (!msg.trim()) { 8 if (!msg.trim()) {
11 return; 9 return null;
12 } 10 }
13 11
14 const delimiters = msg.trim().match(/^\w+(\W)/); 12 const delimiters = msg.trim().match(/^\w+(\W)/);
15 const delimiter = delimiters && delimiters[1] ? delimiters[1] : null; 13 const delimiter = delimiters && delimiters[1] ? delimiters[1] : null;
16 14
17 const delimiterRegex = delimiter ? new RegExp('\\' + delimiter + '+') : new RegExp('[\\s\\.]+'); 15 const delimiterRegex = delimiter ? new RegExp(`\\${delimiter}+`) : new RegExp('[\\s\\.]+');
18 return msg.trim().split(delimiterRegex); 16 return msg.trim().split(delimiterRegex);
19 } 17 }
20 18
21 function _makeSureAsTokensArray(msgOrTokens) { 19 function makeSureAsTokensArray(msgOrTokens) {
22 if (typeof msgOrTokens === 'string') { 20 if (typeof msgOrTokens === 'string') {
23 return splitToken(msgOrTokens); 21 return splitToken(msgOrTokens);
24 } 22 }
25 23
26 return msgOrTokens; 24 return msgOrTokens;
27 } 25 }
28 26
29 function extractCommand(msgOrTokens) { 27 function extractCommand(msgOrTokens) {
30 msgOrTokens = _makeSureAsTokensArray(msgOrTokens); 28 const msgOrTokensArray = makeSureAsTokensArray(msgOrTokens);
31 29
32 if (!msgOrTokens) return; 30 if (!msgOrTokensArray) return null;
33 return msgOrTokens[0]; 31 return msgOrTokensArray[0];
34 } 32 }
35 33
36 function extractCommandGroup(msgOrTokens) { 34 function extractCommandGroup(msgOrTokens) {
37 const cmd = extractCommand(msgOrTokens); 35 const cmd = extractCommand(msgOrTokens);
38 if (!cmd) return; 36 if (!cmd) return null;
39 return commands[ cmd.toLowerCase() ]; 37 return commands[cmd.toLowerCase()];
40 } 38 }
41 39
42 exports.splitToken = splitToken; 40 exports.splitToken = splitToken;
43 exports.extractCommand = extractCommand; 41 exports.extractCommand = extractCommand;
44 exports.extractCommandGroup = extractCommandGroup;