Commit 1fd905ddde07c0d19d88672840fc484e4027ef3c
1 parent
4e65106777
Exists in
master
custom sn tested
Showing 3 changed files with 46 additions and 5 deletions Side-by-side Diff
lib/st24.js
... | ... | @@ -6,15 +6,15 @@ function extractSnFromMessage(msg, custom_rule) { |
6 | 6 | } |
7 | 7 | |
8 | 8 | let pattern; |
9 | - let pattern_match_idx; | |
9 | + let match_idx; | |
10 | 10 | |
11 | 11 | if (custom_rule && custom_rule.pattern) { |
12 | 12 | pattern = custom_rule.pattern; |
13 | - pattern_match_idx = custom_rule.match_idx; | |
13 | + match_idx = custom_rule.match_idx; | |
14 | 14 | } |
15 | 15 | else { |
16 | 16 | pattern = "^SN=(.*?);"; |
17 | - pattern_match_idx = 1; | |
17 | + match_idx = 1; | |
18 | 18 | } |
19 | 19 | |
20 | 20 | const re = new RegExp(pattern); |
... | ... | @@ -22,8 +22,8 @@ function extractSnFromMessage(msg, custom_rule) { |
22 | 22 | |
23 | 23 | if (!matches) return; |
24 | 24 | |
25 | - if (pattern_match_idx < matches.length) { | |
26 | - return matches[pattern_match_idx]; | |
25 | + if (match_idx < matches.length) { | |
26 | + return matches[match_idx] || null; | |
27 | 27 | } else { |
28 | 28 | return; |
29 | 29 | } |
package.json
test.js
... | ... | @@ -0,0 +1,38 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const should = require('should'); | |
4 | + | |
5 | +const st24 = require('./lib/st24'); | |
6 | + | |
7 | +describe('#st24', function() { | |
8 | + | |
9 | + describe('#extractSnFromMessage', function() { | |
10 | + | |
11 | + describe('#using default st24 rule', function() { | |
12 | + it('should return correct sn', function() { | |
13 | + st24.extractSnFromMessage('SN=0419165234155980102;19/04/18 16:52 ISI TR5 KE 0895621829255, SUKSES.SAL=1.323.934,HRG=5.250,ID=46398092,SN=0419165234155980102; ..trx lancar').should.equal('0419165234155980102'); | |
14 | + }) | |
15 | + }) | |
16 | + | |
17 | + describe('#using custom rule', function() { | |
18 | + const custom_rule = { | |
19 | + pattern: 'SN=(.*?)\\.', | |
20 | + match_idx: 1 | |
21 | + } | |
22 | + | |
23 | + it('should return correct sn', function() { | |
24 | + st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=0041002310111470.HRG=10035.SALDO=54489150', custom_rule).should.equal('0041002310111470'); | |
25 | + | |
26 | + }) | |
27 | + | |
28 | + it('should return null on message not containing sn', function() { | |
29 | + should.not.exists(st24.extractSnFromMessage('ISI Ke 081311084419 GAGAL.TRXID=20180403123020042979', custom_rule)); | |
30 | + }) | |
31 | + | |
32 | + it('should return null on message empty sn', function() { | |
33 | + should.not.exists(st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=.HRG=10035.SALDO=54489150', custom_rule)); | |
34 | + }) | |
35 | + }) | |
36 | + }) | |
37 | + | |
38 | +}) |