diff --git a/package.json b/package.json index 31853b7..b54cc07 100644 --- a/package.json +++ b/package.json @@ -41,5 +41,8 @@ "xmlrpc": "~1.3.1", "xmlserializer": "~0.3.3", "xpath": "0.0.21" + }, + "devDependencies": { + "should": "^11.2.0" } } diff --git a/reverse-parser.js b/reverse-parser.js new file mode 100644 index 0000000..85a3e81 --- /dev/null +++ b/reverse-parser.js @@ -0,0 +1,77 @@ +"use strict"; + +function parseMessage(msg) { + const splited = msg.split(','); + + return { + namapel: _getNamaPel(splited), + tarifdaya: _getTarifDaya(splited), + tarif: _getTarif(splited), + daya: _getDaya(splited), + jumlahkwh: _getJumlahKwh(splited) + } +} + +function _getNamaPel(splited) { + try { + return splited[2]; + } + catch(e) { + console.trace('Exception on _getNamaPel:', e); + return; + } +} + +function _getTarifDaya(splited) { + try { + let value = splited[5]; + + if (value.indexOf('VA') < 0) { + value += 'VA'; + } + + return value; + } + catch(e) { + console.trace('Exception on _getTarifDaya:', e); + return; + } +} + +function _getJumlahKwh(splited) { + try { + let value = splited[7]; + value = value.replace(/^kWh\s*/, ''); + return value; + } + catch(e) { + console.trace('Exception on _getJumlahKwh:', e); + return; + } +} + +function _getTarif(splited) { + const tarifdaya = _getTarifDaya(splited); + + try { + return tarifdaya.split('/')[0]; + } + catch(e) { + console.trace('Exception on _getTarif:', e); + return; + } +} + +function _getDaya(splited) { + const tarifdaya = _getTarifDaya(splited); + + try { + return tarifdaya.split('/')[1]; + } + catch(e) { + console.trace('Exception on _getDaya:', e); + return; + } +} + +exports.parseMessage = parseMessage; diff --git a/test.js b/test.js index 5c0a9d4..3a1b028 100644 --- a/test.js +++ b/test.js @@ -1,49 +1,59 @@ -var assert = require("assert"); +"use strict"; +const should = require("should"); +const reverseParser = require('./reverse-parser'); +describe('#reverse-parser', function() { + describe('#parseMessage', function() { -describe('aaa', function() { - var aaa = require('sate24/aaa'); + describe('test with valid message', function () { + const msg = 'IDPEL:2017-01-30 16:58:19,1BNS2550C539F5B40545,ERRY-FEBRIANTO,32024560198,32024560198,R1M/900,Rp ,kWh 24.40,Stroom/Token 4260-5779-4249-2925-9618,SUKSES'; + const data = reverseParser.parseMessage(msg); - describe("#unaliasResponseCode()", function() { - it('should return 68', function() { - assert.equal('68', aaa.unaliasResponseCode('01', '01:68')); - }); + it('should to have correct nama pelanggan', function() { + data.namapel.should.equal('ERRY-FEBRIANTO'); + }) - it('should return 68', function() { - assert.equal('68', aaa.unaliasResponseCode('68', '01:68')); - }); + it('should to have correct tarif daya', function() { + data.tarifdaya.should.equal('R1M/900VA'); + }) - it('should return 00', function() { - assert.equal('00', aaa.unaliasResponseCode('00', '01:68')); - }); + it('should to have correct tarif', function() { + data.tarif.should.equal('R1M'); + }) - it('should return 40', function() { - assert.equal('40', aaa.unaliasResponseCode('40', '')); - }); + it('should to have correct daya', function() { + data.daya.should.equal('900VA'); + }) - it('should return 40', function() { - assert.equal('40', aaa.unaliasResponseCode('40', '')); - }); + it('should to have correct jumlah kwh', function() { + data.jumlahkwh.should.equal('24.40'); + }) + }) - it('should return 40', function() { - assert.equal('40', aaa.unaliasResponseCode('40')); - }); + describe('test with invalid message', function () { + const msg = ''; + const data = reverseParser.parseMessage(msg); - }); -}); + it('should to have correct nama pelanggan', function() { + should(data.namapel).not.be.ok(); + }) -describe('aaa', function() { - var partner = require('./httppulsakita'); + it('should to have correct tarif daya', function() { + should(data.tarifdaya).not.be.ok(); + }) - describe("#parseResult()", function() { - message = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><respon><tanggal>2015/6/16 15:43:35</tanggal><idagen>P0039</idagen><refid>AEE15B32987941D89FFF4BC7EF676C13</refid><produk>PLN20</produk><tujuan>14204279369</tujuan><rc>0000</rc><data> </data><token> </token><pesan>#14836 PLN20 ke:14204279369 SUKSES. SN:3520-2887-6627-6699-4826/TestDummyPanjang6955555/P1/7000VA/32,4. \ -Sisa saldo Rp. 5,000,000 - Rp. 18,700 = Rp. 4,981,300</pesan></respon>'; + it('should to have correct tarif', function() { + should(data.tarif).not.be.ok(); + }) - data = partner.parseResult(message); - console.log(data); + it('should to have correct daya', function() { + should(data.daya).not.be.ok(); + }) - it('should return 2015/6/16 15:43:35', function() { - assert.equal('2015/6/16 15:43:35', data.respon.tanggal); - }); - }); -}); + it('should to have correct jumlah kwh', function() { + should(data.jumlahkwh).not.be.ok(); + }) + }) + + }) +})