Commit d1dad71515c29ffd2c552b68947b27bbb22c1563
1 parent
2326273027
Exists in
master
Smart delimiter
Showing 2 changed files with 9 additions and 1 deletions Inline Diff
lib/command-parser.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const commands = require('./command-group'); | 3 | const commands = require('./command-group'); |
4 | 4 | ||
5 | function splitToken(msg) { | 5 | function splitToken(msg) { |
6 | if (typeof msg !== 'string') { | 6 | if (typeof msg !== 'string') { |
7 | return; | 7 | return; |
8 | } | 8 | } |
9 | 9 | ||
10 | if (!msg.trim()) { | 10 | if (!msg.trim()) { |
11 | return; | 11 | return; |
12 | } | 12 | } |
13 | 13 | ||
14 | return msg.trim().split(/[\s\.]+/); // eslint-disable-line no-useless-escape | 14 | const delimiters = msg.trim().match(/^\w+(\W)/); |
15 | const delimiter = delimiters && delimiters[1] ? delimiters[1] : null; | ||
16 | |||
17 | const delimiterRegex = delimiter ? new RegExp('\\' + delimiter + '+') : new RegExp('[\\s\\.]+'); | ||
18 | return msg.trim().split(delimiterRegex); | ||
15 | } | 19 | } |
16 | 20 | ||
17 | function _makeSureAsTokensArray(msgOrTokens) { | 21 | function _makeSureAsTokensArray(msgOrTokens) { |
18 | if (typeof msgOrTokens === 'string') { | 22 | if (typeof msgOrTokens === 'string') { |
19 | return splitToken(msgOrTokens); | 23 | return splitToken(msgOrTokens); |
20 | } | 24 | } |
21 | 25 | ||
22 | return msgOrTokens; | 26 | return msgOrTokens; |
23 | } | 27 | } |
24 | 28 | ||
25 | function extractCommand(msgOrTokens) { | 29 | function extractCommand(msgOrTokens) { |
26 | msgOrTokens = _makeSureAsTokensArray(msgOrTokens); | 30 | msgOrTokens = _makeSureAsTokensArray(msgOrTokens); |
27 | 31 | ||
28 | if (!msgOrTokens) return; | 32 | if (!msgOrTokens) return; |
29 | return msgOrTokens[0]; | 33 | return msgOrTokens[0]; |
30 | } | 34 | } |
31 | 35 | ||
32 | function extractCommandGroup(msgOrTokens) { | 36 | function extractCommandGroup(msgOrTokens) { |
33 | const cmd = extractCommand(msgOrTokens); | 37 | const cmd = extractCommand(msgOrTokens); |
34 | if (!cmd) return; | 38 | if (!cmd) return; |
35 | return commands[ cmd.toLowerCase() ]; | 39 | return commands[ cmd.toLowerCase() ]; |
36 | } | 40 | } |
37 | 41 | ||
38 | exports.splitToken = splitToken; | 42 | exports.splitToken = splitToken; |
39 | exports.extractCommand = extractCommand; | 43 | exports.extractCommand = extractCommand; |
40 | exports.extractCommandGroup = extractCommandGroup; | 44 | exports.extractCommandGroup = extractCommandGroup; |
test/command-parser.js
1 | "use strict"; | 1 | "use strict"; |
2 | /* global describe it */ | 2 | /* global describe it */ |
3 | 3 | ||
4 | const should = require('should'); | 4 | const should = require('should'); |
5 | 5 | ||
6 | const commandParser = require('../lib/command-parser'); | 6 | const commandParser = require('../lib/command-parser'); |
7 | describe('#command-parser', function() { | 7 | describe('#command-parser', function() { |
8 | 8 | ||
9 | describe('#splitToken', function() { | 9 | describe('#splitToken', function() { |
10 | it('should return correct tokens', function() { | 10 | it('should return correct tokens', function() { |
11 | commandParser.splitToken('SAL.1234')[0].should.equal('SAL'); | 11 | commandParser.splitToken('SAL.1234')[0].should.equal('SAL'); |
12 | commandParser.splitToken('SAL')[0].should.equal('SAL'); | 12 | commandParser.splitToken('SAL')[0].should.equal('SAL'); |
13 | 13 | ||
14 | commandParser.splitToken('SAL.1234')[1].should.equal('1234'); | 14 | commandParser.splitToken('SAL.1234')[1].should.equal('1234'); |
15 | commandParser.splitToken('SAL 1234')[1].should.equal('1234'); | 15 | commandParser.splitToken('SAL 1234')[1].should.equal('1234'); |
16 | |||
17 | }) | ||
18 | |||
19 | it('should handle repeating splitter', function() { | ||
16 | commandParser.splitToken('SAL 1234')[1].should.equal('1234'); | 20 | commandParser.splitToken('SAL 1234')[1].should.equal('1234'); |
17 | }) | 21 | }) |
18 | 22 | ||
19 | it('should handle empty message', function() { | 23 | it('should handle empty message', function() { |
20 | should.not.exists(commandParser.splitToken('')); | 24 | should.not.exists(commandParser.splitToken('')); |
21 | should.not.exists(commandParser.splitToken()); | 25 | should.not.exists(commandParser.splitToken()); |
22 | }) | 26 | }) |
23 | }) | 27 | }) |
24 | 28 | ||
25 | describe('#extractCommand', function() { | 29 | describe('#extractCommand', function() { |
26 | it('should handle array of token as input', function() { | 30 | it('should handle array of token as input', function() { |
27 | commandParser.extractCommand(['SAL', '1234']).should.equal('SAL'); | 31 | commandParser.extractCommand(['SAL', '1234']).should.equal('SAL'); |
28 | }) | 32 | }) |
29 | 33 | ||
30 | it('should handle message string as input', function() { | 34 | it('should handle message string as input', function() { |
31 | commandParser.extractCommand('SAL.1234').should.equal('SAL'); | 35 | commandParser.extractCommand('SAL.1234').should.equal('SAL'); |
32 | commandParser.extractCommand('SAL 1234').should.equal('SAL'); | 36 | commandParser.extractCommand('SAL 1234').should.equal('SAL'); |
33 | commandParser.extractCommand('SAL 1234').should.equal('SAL'); | 37 | commandParser.extractCommand('SAL 1234').should.equal('SAL'); |
34 | }) | 38 | }) |
35 | 39 | ||
36 | it('should handle empty message', function() { | 40 | it('should handle empty message', function() { |
37 | should.not.exists(commandParser.extractCommand('')); | 41 | should.not.exists(commandParser.extractCommand('')); |
38 | should.not.exists(commandParser.extractCommand()); | 42 | should.not.exists(commandParser.extractCommand()); |
39 | }) | 43 | }) |
40 | }) | 44 | }) |
41 | 45 | ||
42 | describe('#extractCommandGroup', function() { | 46 | describe('#extractCommandGroup', function() { |
43 | it('should return correct command group from a message string', function() { | 47 | it('should return correct command group from a message string', function() { |
44 | commandParser.extractCommandGroup('SAL.1234').should.equal('balance'); | 48 | commandParser.extractCommandGroup('SAL.1234').should.equal('balance'); |
45 | }) | 49 | }) |
46 | 50 | ||
47 | it('should return correct command group from an array of tokens', function() { | 51 | it('should return correct command group from an array of tokens', function() { |
48 | commandParser.extractCommandGroup(['SAL', '1234']).should.equal('balance'); | 52 | commandParser.extractCommandGroup(['SAL', '1234']).should.equal('balance'); |
49 | }) | 53 | }) |
50 | 54 | ||
51 | it('should handle invalid message', function() { | 55 | it('should handle invalid message', function() { |
52 | should.not.exists(commandParser.extractCommandGroup()); | 56 | should.not.exists(commandParser.extractCommandGroup()); |
53 | should.not.exists(commandParser.extractCommandGroup('')); | 57 | should.not.exists(commandParser.extractCommandGroup('')); |
54 | }) | 58 | }) |
55 | }) | 59 | }) |
56 | 60 | ||
57 | }) | 61 | }) |