UNPKG

sms-transaction-parser

Version:

Parse SMS for transactions to extract relevant information.

3 lines (2 loc) 5.34 kB
"use strict";function e(){e=function(e,t){return new o(e,void 0,t)};var t=RegExp.prototype,n=new WeakMap;function o(e,t,r){var i=new RegExp(e,t);return n.set(i,r||n.get(e)),a(i,o.prototype)}function i(e,t){var r=n.get(t);return Object.keys(r).reduce((function(t,a){var n=r[a];if("number"==typeof n)t[a]=e[n];else{for(var o=0;void 0===e[n[o]]&&o+1<n.length;)o++;t[a]=e[n[o]]}return t}),Object.create(null))}return r(o,RegExp),o.prototype.exec=function(e){var r=t.exec.call(this,e);if(r){r.groups=i(r,this);var a=r.indices;a&&(a.groups=i(a,this))}return r},o.prototype[Symbol.replace]=function(e,r){if("string"==typeof r){var a=n.get(this);return t[Symbol.replace].call(this,e,r.replace(/\$<([^>]+)>/g,(function(e,t){var r=a[t];return"$"+(Array.isArray(r)?r.join("$"):r)})))}if("function"==typeof r){var o=this;return t[Symbol.replace].call(this,e,(function(){var e=arguments;return"object"!=typeof e[e.length-1]&&(e=[].slice.call(e)).push(i(e,o)),r.apply(this,e)}))}return t[Symbol.replace].call(this,e,r)},e.apply(this,arguments)}function t(){return(t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a])}return e}).apply(this,arguments)}function r(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&a(e,t)}function a(e,t){return(a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e})(e,t)}Object.defineProperty(exports,"__esModule",{value:!0});var n={MPesaSentTo:"M-PESA-SENT",MPesaPaidTo:"M-PESA-PAID",MShwariDeposit:"M-SHWARI-DEPOSIT",MShwariWithdraw:"M-SHWARI-WITHDRAW",MPesaWithdraw:"M-PESA-WITHDRAW",MPesaDeposit:"M-PESA-DEPOSIT"},o={NoMatch:"NO-MATCH",NoTransactionType:"NO-TRANSACTION-TYPE",NoResult:"NO-RESULT"},i=[{type:n.MPesaSentTo,keyPhrase:"sent to",regex:e(/([A-Z0-9]+)(?: Confirmed\.)? Ksh([\d,]+.\d{2}) (sent) to (.+) on (\d{1,2}\/\d{1,2}\/\d{2}) at (\d{1,2}:\d{2} [AP]M).+M\x2DPESA balance is Ksh([\d,]+.\d{2}).+ [Tt]ransaction cost, Ksh([\d,]+.\d{2})/,{reference:1,amount:2,transactionType:3,recipient:4,date:5,time:6,balance:7,transactionCost:8})},{type:n.MPesaPaidTo,keyPhrase:"paid to",regex:e(/([A-Z0-9]+)(?: Confirmed\.)? Ksh([\d,]+.\d{2}) (paid) to (.+) on (\d{1,2}\/\d{1,2}\/\d{2}) at (\d{1,2}:\d{2} [AP]M).+M\x2DPESA balance is Ksh([\d,]+.\d{2}).+ [Tt]ransaction cost, Ksh([\d,]+.\d{2})/,{reference:1,amount:2,transactionType:3,recipient:4,date:5,time:6,balance:7,transactionCost:8})},{type:n.MPesaWithdraw,keyPhrase:"Withdraw",regex:e(/([A-Z0-9]+)(?: Confirmed\.)on (\d{1,2}\/\d{1,2}\/\d{2}) at (\d{1,2}:\d{2} [AP]M)(Withdraw) Ksh([\d,]+.\d{2}) from (.+) New M\x2DPESA balance is Ksh([\d,]+.\d{2}).+Transaction cost(?:, )?Ksh(?:\s|\S)([\d,]+.\d{2})/,{reference:1,date:2,time:3,transactionType:4,amount:5,agent:6,balance:7,transactionCost:8})},{type:n.MShwariDeposit,keyPhrase:"transferred to M-Shwari",regex:e(/([A-Z0-9]+)(?: Confirmed\.)Ksh([\d,]+.\d{2}) (transferred) to (.+) on (\d{1,2}\/\d{1,2}\/\d{2}) at (\d{1,2}:\d{2} [AP]M).+M\x2DPESA balance is Ksh([\d,]+.\d{2}).+ M\x2DShwari saving account balance is Ksh([\d,]+.\d{2}).+Transaction cost Ksh(?:\s|\S)([\d,]+.\d{2})/,{reference:1,amount:2,transactionType:3,account:4,date:5,time:6,balance:7,mShwariBalance:8,transactionCost:9})},{type:n.MShwariWithdraw,keyPhrase:"transferred from M-Shwari",regex:e(/([A-Z0-9]+)(?: Confirmed\.)Ksh([\d,]+.\d{2}) (transferred) from (.+) on (\d{1,2}\/\d{1,2}\/\d{2}) at (\d{1,2}:\d{2} [AP]M). M\x2DShwari balance is Ksh([\d,]+.\d{2}).+M\x2DPESA balance is Ksh([\d,]+.\d{2}).+Transaction cost Ksh(?:\s|\S)([\d,]+.\d{2})/,{reference:1,amount:2,transactionType:3,account:4,date:5,time:6,mShwariBalance:7,balance:8,transactionCost:9})},{type:n.MPesaDeposit,keyPhrase:"You have received",regex:e(/([A-Z0-9]+)(?: Confirmed\.)(?:.+) (received)? Ksh([\d,]+.\d{2}) from (.+) on (\d{1,2}\/\d{1,2}\/\d{2}) at (\d{1,2}:\d{2} [AP]M).+M\x2DPESA balance is Ksh([\d,]+.\d{2}).+/,{reference:1,transactionType:2,amount:3,sender:4,date:5,time:6,balance:7})}],s=function(e){return parseFloat(e.replace(/,/g,""))},c=["date","time","balance","amount"];function d(e){for(var t=0;t<i.length;t++){var r=i[t];if(e.indexOf(r.keyPhrase)>-1)return r}return null}exports.FailedParsing=o,exports.TransactionType=n,exports.getTransactionType=d,exports.parseMessage=function(e){var r=d(e);if(!r)return{type:o.NoTransactionType};var a=r.regex.exec(e);if(!a)return{type:o.NoMatch};var n=a.groups;if(0===Object.keys(n).length)return{type:o.NoResult};var i=n.date,p=n.time,u=n.balance,l=n.amount,f=function(e,t){if(null==e)return{};var r,a,n={},o=Object.keys(e);for(a=0;a<o.length;a++)t.indexOf(r=o[a])>=0||(n[r]=e[r]);return n}(n,c),h=function(e,t){var r=e.split("/").map((function(e){return+e})),a=new Date(r[2]+2e3,r[1],r[0]),n=t.split(" "),o=n[0].split(":"),i=parseInt(o[0]),s=parseInt(o[1]),c=n[1];return"PM"===c&&12!==i&&(i+=12),"AM"===c&&12===i&&(i=0),a.setHours(i),a.setMinutes(s),a.valueOf()}(i,p);return t({},f,{balance:s(u),amount:s(l),dateTime:h.valueOf(),type:r.type})},exports.transactionTypeWithPattern=i; //# sourceMappingURL=sms-transaction-parser.cjs.production.min.js.map