UNPKG

ofx-data-extractor

Version:

A module written in TypeScript that provides a utility to extract data from an OFX file in Node.js and Browser

10 lines (9 loc) 7.69 kB
// Ofx-data-extractor v1.4.8 Copyright (c) 2025 Fábio Pereira França const E=/<\/[\w.]+>/g,N=/<[\w.]+>/g,u="<BANKMSGSRSV1>",d="</BANKMSGSRSV1>",g="<CREDITCARDMSGSRSV1>",S="</CREDITCARDMSGSRSV1>",R="<STMTTRN>",T="</STMTTRN>",x="_#_",M=new RegExp(x,"g"),L=[u,R,g],b=[d,S,T];function _(e){const t=e.slice(0,4),n=e.slice(4,6),r=e.slice(6,8),s=e.slice(8,10),a=e.slice(10,12),i=e.slice(12,14);return{yyyy:t,yy:t.slice(2),y:t,MM:n,M:n,dd:r,d:r,hh:s,h:s,mm:a,m:a,ss:i,s:i}}function k(e,t){const n=_(e);let r=t;for(const[s,a]of Object.entries(n))r=r.replace(s,a);return r}var l;(function(e){e.BANK="BANK",e.CREDIT_CARD="CREDIT_CARD"})(l||(l={}));const G=["debit","fee","srvchg","atm","pos","check","payment","directdebit","cash","repeatpmt"],F=e=>e.includes(u)?l.BANK:l.CREDIT_CARD;function K(e){if(String(e.TRNAMT).startsWith("-"))return!0;const t=String(e.TRNTYPE).toLocaleLowerCase();return t==="1"||G.includes(t)}function f(e){const t=e.replace(/(\\)/g,"\\\\").replace(E,n=>A(n,!0)).replace(N,n=>w(n,!0)).replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(]")/g,'],"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/,\s*}/g,"}").replace(/(,",)/,'",').replace(M,'\\"').slice(0,-1);return t.endsWith(",")?t.slice(0,-1):t}const J=e=>JSON.stringify({date:e.slice(0,12),transactionCode:e.slice(12,19),protocol:e.slice(19)}),P=e=>e.startsWith("DT"),D=e=>e.trim();function w(e,t=!1){return L.includes(e)&&!t?e:e.replace(/[<]/g,` "`).replace(/[>]/g,'":{')}function A(e,t=!1){return b.includes(e)&&!t?` ${e}`:`}, `}function X({fitId:e,fitValue:t}){return e==="separated"?J(t):`"${t}",`}function $(e){return e.search(",")>e.search(".")?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function V({dateString:e,formatDate:t="y-M-d"}){return k(e,t)}function W(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"").replace(/["]/g,x);const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=$(t)),P(n)&&(t=V({dateString:t,formatDate:e.formatDate})),n==="FITID"?X({fitId:e.fitId,fitValue:t}):e.nativeTypes&&B(n,t)?`${Number(t)},`:`"${t}",`}function z(e,t){let n=e;return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,r=>W({field:n.slice(0,n.indexOf(":")),value:r,...t}))),n}function B(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function y(e){const t=e.indexOf(R),n=e.lastIndexOf(T)+T.length;if(t<0||n<0||t>=n)return{oldListText:e,newListText:e};const r=e.substring(t,n),s=new RegExp(R,"g"),a=new RegExp(T,"g"),i=`"STRTTRN":[${r.replace(s,"{").replace(a,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function m(e){if(!(e.indexOf(u)>0))return{newBankStatementTransactions:null,oldBankStatementTransactions:null};const n=y(e.substring(e.indexOf(u),e.indexOf(d)+d.length));return{newBankStatementTransactions:n.newListText,oldBankStatementTransactions:n.oldListText}}function h(e){if(!(e.indexOf(g)>0))return{newCreditCardStatementTransactions:null,oldCreditCardStatementTransactions:null};const n=e.substring(e.indexOf(g),e.indexOf(S)+S.length),r=y(n);return{newCreditCardStatementTransactions:r.newListText,oldCreditCardStatementTransactions:r.oldListText}}function j(e){const t=[];for(const n of e)t.push(...n.replace("<?xml"," ").replace("<?OFX"," ").replace("?>"," ").split(" ").filter(Boolean).map(D).map(r=>r.replace("=",":").replace(/["]/g,"")));return t}function H(e,t){const n={};e.join("").search("<?")>-1&&(e=j(e));for(const r of e){const[s,a]=r.split(":"),i=s.replace(` `,"").toUpperCase();n[i]=t&&B(s,a)?Number(a):String(a).replace(/\?>/,"")}return n}function U(e){return e.reduce((t,n)=>{const r=Math.abs(+n.TRNAMT);return K(n)?(t.amountOfDebits++,t.debit+=r,t):(t.amountOfCredits++,t.credit+=r,t)},{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}class v{config;internConfig={};constructor(t){this.config=t,this.internConfig=t}getConfig(){return this.internConfig}sanitizeRow(t){return z(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),s="<OFX>"+r,{sanitizeRow:a}=this;return s.replace(/<(?=[^\/])/g,` <`).replace(E,i=>A(i)).replace(N,i=>w(i)).split(` `).map(D).filter(Boolean).map(a,this).join("")}}function I(e){return e.toString()}async function p(e){return await new Promise((n,r)=>{if(typeof window<"u"&&window.FileReader){const s=new window.FileReader;s.onload=a=>n(a.target.result),s.onerror=a=>r(a.target.error),s.readAsText(e)}else r(new Error("FileReader is not available in this environment."))})}class c{data;dataRead="";constructor(t){this.data=t,t&&(this.dataRead=t)}getData(){return this.dataRead}fromString(t){return new c(t)}fromBuffer(t){return new c(I(t))}async fromBlob(t){return new c(await p(t))}static fromString(t){return new c(t)}static fromBuffer(t){return new c(I(t))}static async fromBlob(t){return new c(await p(t))}}class Y{configInstance={}}class O extends Y{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=m(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${f(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN:[]}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=h(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${f(n)}}`)?.CREDITCARDMSGSRSV1?.CCSTMTTRNRS?.CCSTMTRS?.BANKTRANLIST?.STRTTRN:[]}getTransactionsSummary(t){const n=this.getContent(t),{DTEND:r,DTSTART:s,...a}=n.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=U(a.STMTTRN||a.STRTTRN||[]);return{dateStart:s,dateEnd:r,...i}}getContent(t){const n=this.configInstance.getPartialJsonData(t),{newBankStatementTransactions:r,oldBankStatementTransactions:s}=m(n),{newCreditCardStatementTransactions:a,oldCreditCardStatementTransactions:i}=h(n);let o=n;return r&&(o=o.replace(s,r)),a&&(o=o.replace(i,a)),JSON.parse(`{${f(o)}}`)}}class Q{customExtractor;customExtractorInstance;dataReaderInstance=new c("");constructor(t){this.customExtractor=t,this.customExtractorInstance=t||new O,this.config({})}data(t){return this.dataReaderInstance=t,this}config(t){return this.customExtractorInstance.setConfig(new v(t)),this}getType(){return F(this.dataReaderInstance.getData())}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>");return H(t.split(` `).filter(n=>!!n.trim().length),!!this.customExtractorInstance.configInstance.getConfig().nativeTypes)}getBankTransferList(){return this.customExtractorInstance.getBankTransferList(this.dataReaderInstance.getData())}getCreditCardTransferList(){return this.customExtractorInstance.getCreditCardTransferList(this.dataReaderInstance.getData())}getTransactionsSummary(){return this.customExtractorInstance.getTransactionsSummary(this.dataReaderInstance.getData())}getContent(){return this.customExtractorInstance.getContent(this.dataReaderInstance.getData())}toJson(){const t=this.getHeaders(),n=this.customExtractorInstance.getContent(this.dataReaderInstance.getData());return{...t,...n}}}class C{extractor;constructor(t,n){this.extractor=new Q(new O),this.extractor.data(new c(t)),this.extractor.config(n||{})}getType(){return this.extractor.getType()}static fromBuffer(t){return new C(c.fromBuffer(t).getData())}static async fromBlob(t){const n=await c.fromBlob(t);return new C(n.getData())}config(t){return this.extractor.config(t),this}getHeaders(){return this.extractor.getHeaders()}getBankTransferList(){return this.extractor.getBankTransferList()}getCreditCardTransferList(){return this.extractor.getCreditCardTransferList()}getTransactionsSummary(){return this.extractor.getTransactionsSummary()}getContent(){return this.extractor.getContent()}toJson(){return this.extractor.toJson()}}export{Q as Extractor,C as Ofx,O as OfxExtractor,c as Reader,p as blobToString,I as bufferToString,f as fixJsonProblems,k as formatDate};