diff --git a/lib/st24.js b/lib/st24.js index d0e54f8..dec96b4 100644 --- a/lib/st24.js +++ b/lib/st24.js @@ -6,15 +6,15 @@ function extractSnFromMessage(msg, custom_rule) { } let pattern; - let pattern_match_idx; + let match_idx; if (custom_rule && custom_rule.pattern) { pattern = custom_rule.pattern; - pattern_match_idx = custom_rule.match_idx; + match_idx = custom_rule.match_idx; } else { pattern = "^SN=(.*?);"; - pattern_match_idx = 1; + match_idx = 1; } const re = new RegExp(pattern); @@ -22,8 +22,8 @@ function extractSnFromMessage(msg, custom_rule) { if (!matches) return; - if (pattern_match_idx < matches.length) { - return matches[pattern_match_idx]; + if (match_idx < matches.length) { + return matches[match_idx] || null; } else { return; } diff --git a/package.json b/package.json index e73e701..fde871d 100644 --- a/package.json +++ b/package.json @@ -24,5 +24,8 @@ "dependencies": { "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git", "xmlrpc": "^1.3.2" + }, + "devDependencies": { + "should": "^13.2.1" } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..2bd9624 --- /dev/null +++ b/test.js @@ -0,0 +1,38 @@ +"use strict"; + +const should = require('should'); + +const st24 = require('./lib/st24'); + +describe('#st24', function() { + + describe('#extractSnFromMessage', function() { + + describe('#using default st24 rule', function() { + it('should return correct sn', function() { + 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'); + }) + }) + + describe('#using custom rule', function() { + const custom_rule = { + pattern: 'SN=(.*?)\\.', + match_idx: 1 + } + + it('should return correct sn', function() { + st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=0041002310111470.HRG=10035.SALDO=54489150', custom_rule).should.equal('0041002310111470'); + + }) + + it('should return null on message not containing sn', function() { + should.not.exists(st24.extractSnFromMessage('ISI Ke 081311084419 GAGAL.TRXID=20180403123020042979', custom_rule)); + }) + + it('should return null on message empty sn', function() { + should.not.exists(st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=.HRG=10035.SALDO=54489150', custom_rule)); + }) + }) + }) + +})