Commit 8a677923a6462567787a05b6898e1d9985fc0493

Authored by Adhidarma Hadiwinoto
1 parent afc1f8f3e2
Exists in master

Perbaikan parsing harga menjadi number

Showing 2 changed files with 18 additions and 1 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 return 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;
46 return price;
45 } 47 }
46 48
47 function extractBalanceFromMsg(msg, custom_rule) { 49 function extractBalanceFromMsg(msg, custom_rule) {
48 const default_pattern = "SAL=([\\d\\.]+)"; 50 const default_pattern = "SAL=([\\d\\.]+)";
49 const default_match_idx = 1; 51 const default_match_idx = 1;
50 52
51 let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); 53 let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
52 if (!balance || typeof balance !== 'string') { 54 if (!balance || typeof balance !== 'string') {
53 return; 55 return;
54 } 56 }
55 57
56 return Number(balance.replace(/[^\d]/g, '')); 58 return Number(balance.replace(/[^\d]/g, ''));
57 } 59 }
58 60
59 61
60 exports.extractSnFromMessage = extractSnFromMessage; 62 exports.extractSnFromMessage = extractSnFromMessage;
61 exports.extractPriceFromMsg = extractPriceFromMsg; 63 exports.extractPriceFromMsg = extractPriceFromMsg;
62 exports.extractBalanceFromMsg = extractBalanceFromMsg; 64 exports.extractBalanceFromMsg = extractBalanceFromMsg;
63 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', 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 }) 15 })
16 16
17 describe('#using custom rule', function() { 17 describe('#using custom rule', function() {
18 const custom_rule = { 18 const custom_rule = {
19 pattern: 'SN=(.*?)\\.', 19 pattern: 'SN=(.*?)\\.',
20 match_idx: 1 20 match_idx: 1
21 } 21 }
22 22
23 it('should return correct sn', function() { 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'); 24 st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=0041002310111470.HRG=10035.SALDO=54489150', custom_rule).should.equal('0041002310111470');
25 25
26 }) 26 })
27 27
28 it('should return null on message not containing sn', function() { 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)); 29 should.not.exists(st24.extractSnFromMessage('ISI Ke 081311084419 GAGAL.TRXID=20180403123020042979', custom_rule));
30 }) 30 })
31 31
32 it('should return null on message empty sn', function() { 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)); 33 should.not.exists(st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=.HRG=10035.SALDO=54489150', custom_rule));
34 }) 34 })
35 }) 35 })
36 }) 36 })
37 37
38 describe('#extractBalanceFromMsg', function() { 38 describe('#extractBalanceFromMsg', function() {
39 describe('using native ST24 response', function() { 39 describe('using native ST24 response', function() {
40 it('should return correct balance', function() { 40 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); 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);
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) 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)
43 }) 43 })
44 44
45 it('should return null if there is no balance info', function() { 45 it('should return null if there is no balance info', function() {
46 should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL')); 46 should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL'));
47 }) 47 })
48 }) 48 })
49 49
50 describe('using custom rule', function() { 50 describe('using custom rule', function() {
51 const custom_rule = { 51 const custom_rule = {
52 pattern: "SALDO=(\\d+)", 52 pattern: "SALDO=(\\d+)",
53 match_idx: 1 53 match_idx: 1
54 } 54 }
55 55
56 it('should return correct balance', function() { 56 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); 57 st24.extractBalanceFromMsg('ISI Telkomsel 10 ke 082139822309 BERHASIL.SN=0041002442595407.HRG=10400.SALDO=104911920', custom_rule).should.equal(104911920);
58 }) 58 })
59 59
60 it('should return null if there is no balance info', function() { 60 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)) 61 should.not.exists(st24.extractBalanceFromMsg('ISI Ke 08523548915 GAGAL.TRXID=20180516123010017371', custom_rule))
62 }) 62 })
63 }) 63 })
64 }) 64 })
65 65
66 describe('#extractPriceFromMsg', function() {
67 describe('using native ST24 topUpRequest direct response', function() {
68 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);
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);
71 });
72 })
73
74 describe('using native ST24 topUpInquiry response', function() {
75 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);
77 })
78 })
79 })
80
66 }) 81 })
67 82