Commit 60289367aeb13034bbac106340f38dfebbdf3ce0

Authored by Adhidarma Hadiwinoto
1 parent 8a677923a6
Exists in master

Perbaikan extractSnFromMessage agar bisa parsing hasil topUpInquiry

Showing 2 changed files with 6 additions and 2 deletions Inline Diff

1 "use strict"; 1 "use strict";
2 2
3 function extractFromMessage(msg, default_pattern, default_match_idx, custom_rule) { 3 function extractFromMessage(msg, default_pattern, default_match_idx, custom_rule) {
4 if (!msg || typeof msg !== 'string') { 4 if (!msg || typeof msg !== 'string') {
5 return; 5 return;
6 } 6 }
7 7
8 let pattern; 8 let pattern;
9 let match_idx; 9 let match_idx;
10 10
11 if (custom_rule && custom_rule.pattern) { 11 if (custom_rule && custom_rule.pattern) {
12 pattern = custom_rule.pattern; 12 pattern = custom_rule.pattern;
13 match_idx = custom_rule.match_idx; 13 match_idx = custom_rule.match_idx;
14 } 14 }
15 else { 15 else {
16 pattern = default_pattern; 16 pattern = default_pattern;
17 match_idx = default_match_idx; 17 match_idx = default_match_idx;
18 } 18 }
19 19
20 const re = new RegExp(pattern); 20 const re = new RegExp(pattern);
21 const matches = msg.match(re); 21 const matches = msg.match(re);
22 22
23 if (!matches) return; 23 if (!matches) return;
24 24
25 if (match_idx < matches.length) { 25 if (match_idx < matches.length) {
26 return matches[match_idx] || null; 26 return matches[match_idx] || null;
27 } else { 27 } else {
28 return; 28 return;
29 } 29 }
30 30
31 } 31 }
32 32
33 function extractSnFromMessage(msg, custom_rule) { 33 function extractSnFromMessage(msg, custom_rule) {
34 const default_pattern = "^SN=(.*?);"; 34 const default_pattern = "SN=(.*?);";
35 const default_match_idx = 1; 35 const default_match_idx = 1;
36 36
37 return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); 37 return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
38 } 38 }
39 39
40 function extractPriceFromMsg(msg, custom_rule) { 40 function extractPriceFromMsg(msg, custom_rule) {
41 const default_pattern = "\\d,HRG=(.*?),ID="; 41 const default_pattern = "\\d,HRG=(.*?),ID=";
42 const default_match_idx = 1; 42 const default_match_idx = 1;
43 43
44 let price = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); 44 let price = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
45 price = (typeof price === 'string') ? Number(price.replace(/\./g, '')) : null; 45 price = (typeof price === 'string') ? Number(price.replace(/\./g, '')) : null;
46 return price; 46 return price;
47 } 47 }
48 48
49 function extractBalanceFromMsg(msg, custom_rule) { 49 function extractBalanceFromMsg(msg, custom_rule) {
50 const default_pattern = "SAL=([\\d\\.]+)"; 50 const default_pattern = "SAL=([\\d\\.]+)";
51 const default_match_idx = 1; 51 const default_match_idx = 1;
52 52
53 let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); 53 let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
54 if (!balance || typeof balance !== 'string') { 54 if (!balance || typeof balance !== 'string') {
55 return; 55 return;
56 } 56 }
57 57
58 return Number(balance.replace(/[^\d]/g, '')); 58 return Number(balance.replace(/[^\d]/g, ''));
59 } 59 }
60 60
61 61
62 exports.extractSnFromMessage = extractSnFromMessage; 62 exports.extractSnFromMessage = extractSnFromMessage;
63 exports.extractPriceFromMsg = extractPriceFromMsg; 63 exports.extractPriceFromMsg = extractPriceFromMsg;
64 exports.extractBalanceFromMsg = extractBalanceFromMsg; 64 exports.extractBalanceFromMsg = extractBalanceFromMsg;
65 65
1 "use strict"; 1 "use strict";
2 2
3 const should = require('should'); 3 const should = require('should');
4 4
5 const st24 = require('./lib/st24'); 5 const st24 = require('./lib/st24');
6 6
7 describe('#st24', function() { 7 describe('#st24', function() {
8 8
9 describe('#extractSnFromMessage', function() { 9 describe('#extractSnFromMessage', function() {
10 10
11 describe('#using default st24 rule', function() { 11 describe('#using default st24 rule', function() {
12 it('should return correct sn', function() { 12 it('should return correct sn from topUpRequest response', 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'); 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 }) 14 })
15
16 it('should return correct sn from topUpInquiry response', function() {
17 st24.extractSnFromMessage('19/07/18 20:53 ISI SPT20 KE 081264858057, SUKSES.SAL=828.425,HRG=19.700,ID=48761021,SN=0041002635369521;').should.equal('0041002635369521');
18 })
15 }) 19 })
16 20
17 describe('#using custom rule', function() { 21 describe('#using custom rule', function() {
18 const custom_rule = { 22 const custom_rule = {
19 pattern: 'SN=(.*?)\\.', 23 pattern: 'SN=(.*?)\\.',
20 match_idx: 1 24 match_idx: 1
21 } 25 }
22 26
23 it('should return correct sn', function() { 27 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'); 28 st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=0041002310111470.HRG=10035.SALDO=54489150', custom_rule).should.equal('0041002310111470');
25 29
26 }) 30 })
27 31
28 it('should return null on message not containing sn', function() { 32 it('should return null on message not containing sn', function() {
29 should.not.exists(st24.extractSnFromMessage('ISI Ke 081311084419 GAGAL.TRXID=20180403123020042979', custom_rule)); 33 should.not.exists(st24.extractSnFromMessage('ISI Ke 081311084419 GAGAL.TRXID=20180403123020042979', custom_rule));
30 }) 34 })
31 35
32 it('should return null on message empty sn', function() { 36 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)); 37 should.not.exists(st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=.HRG=10035.SALDO=54489150', custom_rule));
34 }) 38 })
35 }) 39 })
36 }) 40 })
37 41
38 describe('#extractBalanceFromMsg', function() { 42 describe('#extractBalanceFromMsg', function() {
39 describe('using native ST24 response', function() { 43 describe('using native ST24 response', function() {
40 it('should return correct balance', function() { 44 it('should return correct balance', function() {
41 st24.extractBalanceFromMsg('SN=0516150344145563101; 16/05/18 15:03 ISI TR5 KE 0895350249796, SUKSES.SAL=426.078,HRG=5.250,ID=47285513,SN=0516150344145563101; ..trx lancar').should.equal(426078); 45 st24.extractBalanceFromMsg('SN=0516150344145563101; 16/05/18 15:03 ISI TR5 KE 0895350249796, SUKSES.SAL=426.078,HRG=5.250,ID=47285513,SN=0516150344145563101; ..trx lancar').should.equal(426078);
42 st24.extractBalanceFromMsg('15/05/18 17:19 ISI SAN10 KE 08535686667, NOMOR YANG ANDA MASUKKAN SALAH, MOHON TELITI KEMBALI..SAL=1.144.578,ID=47250459, ..trx lancar').should.equal(1144578) 46 st24.extractBalanceFromMsg('15/05/18 17:19 ISI SAN10 KE 08535686667, NOMOR YANG ANDA MASUKKAN SALAH, MOHON TELITI KEMBALI..SAL=1.144.578,ID=47250459, ..trx lancar').should.equal(1144578)
43 }) 47 })
44 48
45 it('should return null if there is no balance info', function() { 49 it('should return null if there is no balance info', function() {
46 should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL')); 50 should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL'));
47 }) 51 })
48 }) 52 })
49 53
50 describe('using custom rule', function() { 54 describe('using custom rule', function() {
51 const custom_rule = { 55 const custom_rule = {
52 pattern: "SALDO=(\\d+)", 56 pattern: "SALDO=(\\d+)",
53 match_idx: 1 57 match_idx: 1
54 } 58 }
55 59
56 it('should return correct balance', function() { 60 it('should return correct balance', function() {
57 st24.extractBalanceFromMsg('ISI Telkomsel 10 ke 082139822309 BERHASIL.SN=0041002442595407.HRG=10400.SALDO=104911920', custom_rule).should.equal(104911920); 61 st24.extractBalanceFromMsg('ISI Telkomsel 10 ke 082139822309 BERHASIL.SN=0041002442595407.HRG=10400.SALDO=104911920', custom_rule).should.equal(104911920);
58 }) 62 })
59 63
60 it('should return null if there is no balance info', function() { 64 it('should return null if there is no balance info', function() {
61 should.not.exists(st24.extractBalanceFromMsg('ISI Ke 08523548915 GAGAL.TRXID=20180516123010017371', custom_rule)) 65 should.not.exists(st24.extractBalanceFromMsg('ISI Ke 08523548915 GAGAL.TRXID=20180516123010017371', custom_rule))
62 }) 66 })
63 }) 67 })
64 }) 68 })
65 69
66 describe('#extractPriceFromMsg', function() { 70 describe('#extractPriceFromMsg', function() {
67 describe('using native ST24 topUpRequest direct response', function() { 71 describe('using native ST24 topUpRequest direct response', function() {
68 it('should return correct price', function() { 72 it('should return correct price', function() {
69 st24.extractPriceFromMsg('SN=0041002635395450;;19/07/18 21:01 ISI SPT20 KE 08125100091, SUKSES. SAL=798.500,HRG=19.700,ID=48761075,SN=0041002635395450;; ..trx lancar').should.equal(19700); 73 st24.extractPriceFromMsg('SN=0041002635395450;;19/07/18 21:01 ISI SPT20 KE 08125100091, SUKSES. SAL=798.500,HRG=19.700,ID=48761075,SN=0041002635395450;; ..trx lancar').should.equal(19700);
70 st24.extractPriceFromMsg('SN=0516150344145563101; 16/05/18 15:03 ISI TR5 KE 0895350249796, SUKSES.SAL=426.078,HRG=5.250,ID=47285513,SN=0516150344145563101; ..trx lancar').should.equal(5250); 74 st24.extractPriceFromMsg('SN=0516150344145563101; 16/05/18 15:03 ISI TR5 KE 0895350249796, SUKSES.SAL=426.078,HRG=5.250,ID=47285513,SN=0516150344145563101; ..trx lancar').should.equal(5250);
71 }); 75 });
72 }) 76 })
73 77
74 describe('using native ST24 topUpInquiry response', function() { 78 describe('using native ST24 topUpInquiry response', function() {
75 it('should return correct price', function() { 79 it('should return correct price', function() {
76 st24.extractPriceFromMsg('19/07/18 20:53 ISI SPT20 KE 081264858057, SUKSES.SAL=828.425,HRG=19.700,ID=48761021,SN=0041002635369521;').should.equal(19700); 80 st24.extractPriceFromMsg('19/07/18 20:53 ISI SPT20 KE 081264858057, SUKSES.SAL=828.425,HRG=19.700,ID=48761021,SN=0041002635369521;').should.equal(19700);
77 }) 81 })
78 }) 82 })
79 }) 83 })
80 84
81 }) 85 })
82 86