ofx-data-extractor 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,9 @@ export declare function getBankStatementTransactionsText(ofxContent: string): {
25
25
  oldBankStatementTransactions: string;
26
26
  };
27
27
  export declare function getCreditCardStatementTransactionsText(ofxContent: string): {
28
+ newCreditCardStatementTransactions: null;
29
+ oldCreditCardStatementTransactions: null;
30
+ } | {
28
31
  newCreditCardStatementTransactions: string;
29
32
  oldCreditCardStatementTransactions: string;
30
33
  };
@@ -1,11 +1,11 @@
1
- // Ofx-data-extractor v1.3.0 Copyright (c) 2023 Fábio Pereira França
2
- const I=/<\/\w+>/g,N=/<\w+>/g,x="<BANKMSGSRSV1>",f="</BANKMSGSRSV1>",E="<CREDITCARDMSGSRSV1>",l="</CREDITCARDMSGSRSV1>",d="<STMTTRN>",c="</STMTTRN>",b=[x,d,E],M=[f,l,c];function O(e){const t=e.slice(0,4),n=e.slice(4,6),r=e.slice(6,8),a=e.slice(8,10),s=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:a,h:a,mm:s,m:s,ss:i,s:i}}function g(e,t){const n=O(e);let r=t;for(const[a,s]of Object.entries(n))r=r.replace(a,s);return r}function T(e){return e.replace(/(\\)/g,"\\\\").replace(I,t=>D(t,!0)).replace(N,t=>w(t,!0)).replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
1
+ // Ofx-data-extractor v1.3.2 Copyright (c) 2024 Fábio Pereira França
2
+ const w=/<\/\w+>/g,x=/<\w+>/g,E="<BANKMSGSRSV1>",l="</BANKMSGSRSV1>",d="<CREDITCARDMSGSRSV1>",g="</CREDITCARDMSGSRSV1>",S="<STMTTRN>",c="</STMTTRN>",O=[E,S,d],b=[l,g,c];function M(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 m(e,t){const n=M(e);let r=t;for(const[s,a]of Object.entries(n))r=r.replace(s,a);return r}function f(e){return e.replace(/(\\)/g,"\\\\").replace(w,t=>p(t,!0)).replace(x,t=>D(t,!0)).replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
3
3
  "`).replace(/(})/g,`
4
4
  }`).replace(/(",")/g,`",
5
5
  "`).replace(/,\s*}/g,`
6
- }`).replace(/(,",)/,",").slice(0,-1)}function B(e){const t=e.slice(0,12),n=e.slice(12,19),r=e.slice(19),a={date:t,transactionCode:n,protocol:r};return JSON.stringify(a)}function F(e){return e.startsWith("DT")}function G(e){return e.trim()}function w(e,t=!1){return b.includes(e)&&!t?e:e.replace(/[<]/g,`
7
- "`).replace(/[>]/g,'":{')}function D(e,t=!1){return M.includes(e)&&!t?`
8
- ${e}`:"},"}function P({fitId:e,fitValue:t}){return e==="separated"?B(t):`"${t}",`}function k(e){const t=e.search(","),n=e.search(".");return t>n?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function _({dateString:e,formatDate:t}){return t?g(e,t):g(e,"y-M-d")}function J(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"");const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=k(t)),F(n)&&(t=_({dateString:t,formatDate:e.formatDate})),n==="FITID"?P({fitId:e.fitId,fitValue:t}):e.nativeTypes&&p(n,t)?`${Number(t)},`:`"${t}",`}function K(e,t){let n=e;const r=n.slice(0,n.indexOf(":")),a=s=>J({field:r,value:s,...t});return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,a)),n}function p(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function A(e){const t=e.indexOf(d),n=e.lastIndexOf(c)+c.length,r=e.substring(t,n),a=new RegExp(d,"g"),s=new RegExp(c,"g"),i=`"STRTTRN":[${r.replace(a,"{").replace(s,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function S(e){const t=e.substring(e.indexOf(x),e.indexOf(f)+f.length),n=A(t);return{newBankStatementTransactions:n.newListText,oldBankStatementTransactions:n.oldListText}}function m(e){const t=e.substring(e.indexOf(E),e.indexOf(l)+l.length),n=A(t);return{newCreditCardStatementTransactions:n.newListText,oldCreditCardStatementTransactions:n.oldListText}}function $(e,t){const n={};for(const r of e){const[a,s]=r.split(":"),i=a.replace(`
9
- `,"");n[i]=t&&p(a,s)?Number(s):s}return n}function z(e){return e.reduce((t,n)=>n.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(n.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.credit+=Number(n.TRNAMT),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 K(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),a="<OFX>"+r,{sanitizeRow:s}=this;return a.replace(I,i=>D(i)).replace(N,i=>w(i)).split(`
10
- `).map(G).filter(Boolean).map(s,this).join("")}}function R(e){return e.toString()}async function h(e){return await new Promise((n,r)=>{import("fs").then(a=>a.readFile(e,(s,i)=>{s?r(s):n(i.toString())}))})}async function C(e){return await new Promise((n,r)=>{if(typeof window<"u"&&window.FileReader){const a=new window.FileReader;a.onload=s=>n(s.target.result),a.onerror=s=>r(s.target.error),a.readAsText(e)}else r(new Error("FileReader is not available in this environment."))})}class o{data;dataRead="";constructor(t){this.data=t,t&&(this.dataRead=t)}getData(){return this.dataRead}fromString(t){return new o(t)}fromBuffer(t){return new o(R(t))}async fromFilePath(t){return new o(await h(t))}async fromBlob(t){return new o(await C(t))}static fromString(t){return new o(t)}static fromBuffer(t){return new o(R(t))}static async fromFilePath(t){return new o(await h(t))}static async fromBlob(t){return new o(await C(t))}}class W{configInstance={}}class y extends W{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=S(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${T(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=m(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${T(n)}}`)?.CREDITCARDMSGSRSV1?.CCSTMTTRNRS?.CCSTMTRS?.BANKTRANLIST?.STRTTRN}getTransactionsSummary(t){const n=this.getContent(t),{DTEND:r,DTSTART:a,STRTTRN:s}=n.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=z(s);return{dateStart:a,dateEnd:r,...i}}getContent(t){const n=this.configInstance.getPartialJsonData(t),{newBankStatementTransactions:r,oldBankStatementTransactions:a}=S(n),{newCreditCardStatementTransactions:s,oldCreditCardStatementTransactions:i}=m(n),L=n.replace(i,s).replace(a,r);return JSON.parse(`{${T(L)}}`)}}class j{customExtractor;customExtractorInstance;dataReaderInstance=new o("");constructor(t){this.customExtractor=t,this.customExtractorInstance=t||new y,this.config({})}data(t){return this.dataReaderInstance=t,this}config(t){return this.customExtractorInstance.setConfig(new V(t)),this}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>"),n=t.split(`
11
- `),r=s=>!!s.trim().length,a=n.filter(r);return $(a,!!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 u{extractor;constructor(t,n){this.extractor=new j(new y),this.extractor.data(new o(t)),this.extractor.config(n||{})}static fromBuffer(t){return new u(o.fromBuffer(t).getData())}static async fromFilePath(t){const n=await o.fromFilePath(t);return new u(n.getData())}static async fromBlob(t){const n=await o.fromBlob(t);return new u(n.getData())}config(t){return this.extractor.config(t),this}getHeaders(){return this.extractor.getHeaders()}getBankTransferList(){return this.extractor.getBankTransferList()}getTransactionsSummary(){return this.extractor.getTransactionsSummary()}getContent(){return this.extractor.getContent()}toJson(){return this.extractor.toJson()}}export{j as Extractor,u as Ofx,y as OfxExtractor,o as Reader,C as blobToString,R as bufferToString,h as fileFromPathToString,T as fixJsonProblems,g as formatDate};
6
+ }`).replace(/(,",)/,",").slice(0,-1)}function B(e){const t=e.slice(0,12),n=e.slice(12,19),r=e.slice(19),s={date:t,transactionCode:n,protocol:r};return JSON.stringify(s)}function F(e){return e.startsWith("DT")}function G(e){return e.trim()}function D(e,t=!1){return O.includes(e)&&!t?e:e.replace(/[<]/g,`
7
+ "`).replace(/[>]/g,'":{')}function p(e,t=!1){return b.includes(e)&&!t?`
8
+ ${e}`:"},"}function P({fitId:e,fitValue:t}){return e==="separated"?B(t):`"${t}",`}function k(e){const t=e.search(","),n=e.search(".");return t>n?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function _({dateString:e,formatDate:t}){return t?m(e,t):m(e,"y-M-d")}function J(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"");const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=k(t)),F(n)&&(t=_({dateString:t,formatDate:e.formatDate})),n==="FITID"?P({fitId:e.fitId,fitValue:t}):e.nativeTypes&&A(n,t)?`${Number(t)},`:`"${t}",`}function K(e,t){let n=e;const r=n.slice(0,n.indexOf(":")),s=a=>J({field:r,value:a,...t});return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,s)),n}function A(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function y(e){const t=e.indexOf(S),n=e.lastIndexOf(c)+c.length,r=e.substring(t,n),s=new RegExp(S,"g"),a=new RegExp(c,"g"),i=`"STRTTRN":[${r.replace(s,"{").replace(a,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function R(e){const t=e.substring(e.indexOf(E),e.indexOf(l)+l.length),n=y(t);return{newBankStatementTransactions:n.newListText,oldBankStatementTransactions:n.oldListText}}function C(e){if(!(e.indexOf(d)>0))return{newCreditCardStatementTransactions:null,oldCreditCardStatementTransactions:null};const n=e.substring(e.indexOf(d),e.indexOf(g)+g.length),r=y(n);return{newCreditCardStatementTransactions:r.newListText,oldCreditCardStatementTransactions:r.oldListText}}function $(e,t){const n={};for(const r of e){const[s,a]=r.split(":"),i=s.replace(`
9
+ `,"");n[i]=t&&A(s,a)?Number(a):a}return n}function z(e){return e.reduce((t,n)=>n.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(n.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.credit+=Number(n.TRNAMT),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 K(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),s="<OFX>"+r,{sanitizeRow:a}=this;return s.replace(w,i=>p(i)).replace(x,i=>D(i)).split(`
10
+ `).map(G).filter(Boolean).map(a,this).join("")}}function h(e){return e.toString()}async function I(e){return await new Promise((n,r)=>{import("fs").then(s=>s.readFile(e,(a,i)=>{a?r(a):n(i.toString())}))})}async function N(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 o{data;dataRead="";constructor(t){this.data=t,t&&(this.dataRead=t)}getData(){return this.dataRead}fromString(t){return new o(t)}fromBuffer(t){return new o(h(t))}async fromFilePath(t){return new o(await I(t))}async fromBlob(t){return new o(await N(t))}static fromString(t){return new o(t)}static fromBuffer(t){return new o(h(t))}static async fromFilePath(t){return new o(await I(t))}static async fromBlob(t){return new o(await N(t))}}class W{configInstance={}}class L extends W{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=R(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${f(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=C(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,STRTTRN:a}=n.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=z(a);return{dateStart:s,dateEnd:r,...i}}getContent(t){const n=this.configInstance.getPartialJsonData(t),{newBankStatementTransactions:r,oldBankStatementTransactions:s}=R(n),{newCreditCardStatementTransactions:a,oldCreditCardStatementTransactions:i}=C(n);let T=n.replace(s,r);return a&&(T=T.replace(i,a)),JSON.parse(`{${f(T)}}`)}}class j{customExtractor;customExtractorInstance;dataReaderInstance=new o("");constructor(t){this.customExtractor=t,this.customExtractorInstance=t||new L,this.config({})}data(t){return this.dataReaderInstance=t,this}config(t){return this.customExtractorInstance.setConfig(new V(t)),this}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>"),n=t.split(`
11
+ `),r=a=>!!a.trim().length,s=n.filter(r);return $(s,!!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 u{extractor;constructor(t,n){this.extractor=new j(new L),this.extractor.data(new o(t)),this.extractor.config(n||{})}static fromBuffer(t){return new u(o.fromBuffer(t).getData())}static async fromFilePath(t){const n=await o.fromFilePath(t);return new u(n.getData())}static async fromBlob(t){const n=await o.fromBlob(t);return new u(n.getData())}config(t){return this.extractor.config(t),this}getHeaders(){return this.extractor.getHeaders()}getBankTransferList(){return this.extractor.getBankTransferList()}getTransactionsSummary(){return this.extractor.getTransactionsSummary()}getContent(){return this.extractor.getContent()}toJson(){return this.extractor.toJson()}}export{j as Extractor,u as Ofx,L as OfxExtractor,o as Reader,N as blobToString,h as bufferToString,I as fileFromPathToString,f as fixJsonProblems,m as formatDate};
@@ -1,11 +1,11 @@
1
- // Ofx-data-extractor v1.3.0 Copyright (c) 2023 Fábio Pereira França
2
- (function(c,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(c=typeof globalThis<"u"?globalThis:c||self,u(c.ofx={}))})(this,function(c){"use strict";const u=/<\/\w+>/g,N=/<\w+>/g,E="<BANKMSGSRSV1>",d="</BANKMSGSRSV1>",x="<CREDITCARDMSGSRSV1>",g="</CREDITCARDMSGSRSV1>",S="<STMTTRN>",T="</STMTTRN>",O=[E,S,x],M=[d,g,T];function B(e){const t=e.slice(0,4),n=e.slice(4,6),r=e.slice(6,8),a=e.slice(8,10),s=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:a,h:a,mm:s,m:s,ss:i,s:i}}function m(e,t){const n=B(e);let r=t;for(const[a,s]of Object.entries(n))r=r.replace(a,s);return r}function l(e){return e.replace(/(\\)/g,"\\\\").replace(u,t=>D(t,!0)).replace(N,t=>w(t,!0)).replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
1
+ // Ofx-data-extractor v1.3.2 Copyright (c) 2024 Fábio Pereira França
2
+ (function(c,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(c=typeof globalThis<"u"?globalThis:c||self,u(c.ofx={}))})(this,function(c){"use strict";const u=/<\/\w+>/g,w=/<\w+>/g,x="<BANKMSGSRSV1>",d="</BANKMSGSRSV1>",g="<CREDITCARDMSGSRSV1>",S="</CREDITCARDMSGSRSV1>",m="<STMTTRN>",T="</STMTTRN>",M=[x,m,g],B=[d,S,T];function F(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 R(e,t){const n=F(e);let r=t;for(const[s,a]of Object.entries(n))r=r.replace(s,a);return r}function l(e){return e.replace(/(\\)/g,"\\\\").replace(u,t=>p(t,!0)).replace(w,t=>D(t,!0)).replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
3
3
  "`).replace(/(})/g,`
4
4
  }`).replace(/(",")/g,`",
5
5
  "`).replace(/,\s*}/g,`
6
- }`).replace(/(,",)/,",").slice(0,-1)}function F(e){const t=e.slice(0,12),n=e.slice(12,19),r=e.slice(19),a={date:t,transactionCode:n,protocol:r};return JSON.stringify(a)}function P(e){return e.startsWith("DT")}function G(e){return e.trim()}function w(e,t=!1){return O.includes(e)&&!t?e:e.replace(/[<]/g,`
7
- "`).replace(/[>]/g,'":{')}function D(e,t=!1){return M.includes(e)&&!t?`
8
- ${e}`:"},"}function k({fitId:e,fitValue:t}){return e==="separated"?F(t):`"${t}",`}function _(e){const t=e.search(","),n=e.search(".");return t>n?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function J({dateString:e,formatDate:t}){return t?m(e,t):m(e,"y-M-d")}function K(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"");const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=_(t)),P(n)&&(t=J({dateString:t,formatDate:e.formatDate})),n==="FITID"?k({fitId:e.fitId,fitValue:t}):e.nativeTypes&&p(n,t)?`${Number(t)},`:`"${t}",`}function $(e,t){let n=e;const r=n.slice(0,n.indexOf(":")),a=s=>K({field:r,value:s,...t});return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,a)),n}function p(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function b(e){const t=e.indexOf(S),n=e.lastIndexOf(T)+T.length,r=e.substring(t,n),a=new RegExp(S,"g"),s=new RegExp(T,"g"),i=`"STRTTRN":[${r.replace(a,"{").replace(s,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function y(e){const t=e.substring(e.indexOf(E),e.indexOf(d)+d.length),n=b(t);return{newBankStatementTransactions:n.newListText,oldBankStatementTransactions:n.oldListText}}function A(e){const t=e.substring(e.indexOf(x),e.indexOf(g)+g.length),n=b(t);return{newCreditCardStatementTransactions:n.newListText,oldCreditCardStatementTransactions:n.oldListText}}function j(e,t){const n={};for(const r of e){const[a,s]=r.split(":"),i=a.replace(`
9
- `,"");n[i]=t&&p(a,s)?Number(s):s}return n}function z(e){return e.reduce((t,n)=>n.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(n.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.credit+=Number(n.TRNAMT),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 $(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),a="<OFX>"+r,{sanitizeRow:s}=this;return a.replace(u,i=>D(i)).replace(N,i=>w(i)).split(`
10
- `).map(G).filter(Boolean).map(s,this).join("")}}function R(e){return e.toString()}async function h(e){return await new Promise((n,r)=>{import("fs").then(a=>a.readFile(e,(s,i)=>{s?r(s):n(i.toString())}))})}async function C(e){return await new Promise((n,r)=>{if(typeof window<"u"&&window.FileReader){const a=new window.FileReader;a.onload=s=>n(s.target.result),a.onerror=s=>r(s.target.error),a.readAsText(e)}else r(new Error("FileReader is not available in this environment."))})}class o{data;dataRead="";constructor(t){this.data=t,t&&(this.dataRead=t)}getData(){return this.dataRead}fromString(t){return new o(t)}fromBuffer(t){return new o(R(t))}async fromFilePath(t){return new o(await h(t))}async fromBlob(t){return new o(await C(t))}static fromString(t){return new o(t)}static fromBuffer(t){return new o(R(t))}static async fromFilePath(t){return new o(await h(t))}static async fromBlob(t){return new o(await C(t))}}class W{configInstance={}}class I extends W{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=y(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${l(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=A(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${l(n)}}`)?.CREDITCARDMSGSRSV1?.CCSTMTTRNRS?.CCSTMTRS?.BANKTRANLIST?.STRTTRN}getTransactionsSummary(t){const n=this.getContent(t),{DTEND:r,DTSTART:a,STRTTRN:s}=n.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=z(s);return{dateStart:a,dateEnd:r,...i}}getContent(t){const n=this.configInstance.getPartialJsonData(t),{newBankStatementTransactions:r,oldBankStatementTransactions:a}=y(n),{newCreditCardStatementTransactions:s,oldCreditCardStatementTransactions:i}=A(n),X=n.replace(i,s).replace(a,r);return JSON.parse(`{${l(X)}}`)}}class L{customExtractor;customExtractorInstance;dataReaderInstance=new o("");constructor(t){this.customExtractor=t,this.customExtractorInstance=t||new I,this.config({})}data(t){return this.dataReaderInstance=t,this}config(t){return this.customExtractorInstance.setConfig(new V(t)),this}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>"),n=t.split(`
11
- `),r=s=>!!s.trim().length,a=n.filter(r);return j(a,!!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 f{extractor;constructor(t,n){this.extractor=new L(new I),this.extractor.data(new o(t)),this.extractor.config(n||{})}static fromBuffer(t){return new f(o.fromBuffer(t).getData())}static async fromFilePath(t){const n=await o.fromFilePath(t);return new f(n.getData())}static async fromBlob(t){const n=await o.fromBlob(t);return new f(n.getData())}config(t){return this.extractor.config(t),this}getHeaders(){return this.extractor.getHeaders()}getBankTransferList(){return this.extractor.getBankTransferList()}getTransactionsSummary(){return this.extractor.getTransactionsSummary()}getContent(){return this.extractor.getContent()}toJson(){return this.extractor.toJson()}}c.Extractor=L,c.Ofx=f,c.OfxExtractor=I,c.Reader=o,c.blobToString=C,c.bufferToString=R,c.fileFromPathToString=h,c.fixJsonProblems=l,c.formatDate=m});
6
+ }`).replace(/(,",)/,",").slice(0,-1)}function P(e){const t=e.slice(0,12),n=e.slice(12,19),r=e.slice(19),s={date:t,transactionCode:n,protocol:r};return JSON.stringify(s)}function G(e){return e.startsWith("DT")}function k(e){return e.trim()}function D(e,t=!1){return M.includes(e)&&!t?e:e.replace(/[<]/g,`
7
+ "`).replace(/[>]/g,'":{')}function p(e,t=!1){return B.includes(e)&&!t?`
8
+ ${e}`:"},"}function _({fitId:e,fitValue:t}){return e==="separated"?P(t):`"${t}",`}function J(e){const t=e.search(","),n=e.search(".");return t>n?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function K({dateString:e,formatDate:t}){return t?R(e,t):R(e,"y-M-d")}function $(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"");const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=J(t)),G(n)&&(t=K({dateString:t,formatDate:e.formatDate})),n==="FITID"?_({fitId:e.fitId,fitValue:t}):e.nativeTypes&&b(n,t)?`${Number(t)},`:`"${t}",`}function j(e,t){let n=e;const r=n.slice(0,n.indexOf(":")),s=a=>$({field:r,value:a,...t});return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,s)),n}function b(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function y(e){const t=e.indexOf(m),n=e.lastIndexOf(T)+T.length,r=e.substring(t,n),s=new RegExp(m,"g"),a=new RegExp(T,"g"),i=`"STRTTRN":[${r.replace(s,"{").replace(a,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function A(e){const t=e.substring(e.indexOf(x),e.indexOf(d)+d.length),n=y(t);return{newBankStatementTransactions:n.newListText,oldBankStatementTransactions:n.oldListText}}function O(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 z(e,t){const n={};for(const r of e){const[s,a]=r.split(":"),i=s.replace(`
9
+ `,"");n[i]=t&&b(s,a)?Number(a):a}return n}function V(e){return e.reduce((t,n)=>n.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(n.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.credit+=Number(n.TRNAMT),t),{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}class W{config;internConfig={};constructor(t){this.config=t,this.internConfig=t}getConfig(){return this.internConfig}sanitizeRow(t){return j(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),s="<OFX>"+r,{sanitizeRow:a}=this;return s.replace(u,i=>p(i)).replace(w,i=>D(i)).split(`
10
+ `).map(k).filter(Boolean).map(a,this).join("")}}function h(e){return e.toString()}async function C(e){return await new Promise((n,r)=>{import("fs").then(s=>s.readFile(e,(a,i)=>{a?r(a):n(i.toString())}))})}async function I(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 o{data;dataRead="";constructor(t){this.data=t,t&&(this.dataRead=t)}getData(){return this.dataRead}fromString(t){return new o(t)}fromBuffer(t){return new o(h(t))}async fromFilePath(t){return new o(await C(t))}async fromBlob(t){return new o(await I(t))}static fromString(t){return new o(t)}static fromBuffer(t){return new o(h(t))}static async fromFilePath(t){return new o(await C(t))}static async fromBlob(t){return new o(await I(t))}}class X{configInstance={}}class N extends X{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=A(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${l(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=O(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${l(n)}}`)?.CREDITCARDMSGSRSV1?.CCSTMTTRNRS?.CCSTMTRS?.BANKTRANLIST?.STRTTRN:[]}getTransactionsSummary(t){const n=this.getContent(t),{DTEND:r,DTSTART:s,STRTTRN:a}=n.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=V(a);return{dateStart:s,dateEnd:r,...i}}getContent(t){const n=this.configInstance.getPartialJsonData(t),{newBankStatementTransactions:r,oldBankStatementTransactions:s}=A(n),{newCreditCardStatementTransactions:a,oldCreditCardStatementTransactions:i}=O(n);let E=n.replace(s,r);return a&&(E=E.replace(i,a)),JSON.parse(`{${l(E)}}`)}}class L{customExtractor;customExtractorInstance;dataReaderInstance=new o("");constructor(t){this.customExtractor=t,this.customExtractorInstance=t||new N,this.config({})}data(t){return this.dataReaderInstance=t,this}config(t){return this.customExtractorInstance.setConfig(new W(t)),this}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>"),n=t.split(`
11
+ `),r=a=>!!a.trim().length,s=n.filter(r);return z(s,!!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 f{extractor;constructor(t,n){this.extractor=new L(new N),this.extractor.data(new o(t)),this.extractor.config(n||{})}static fromBuffer(t){return new f(o.fromBuffer(t).getData())}static async fromFilePath(t){const n=await o.fromFilePath(t);return new f(n.getData())}static async fromBlob(t){const n=await o.fromBlob(t);return new f(n.getData())}config(t){return this.extractor.config(t),this}getHeaders(){return this.extractor.getHeaders()}getBankTransferList(){return this.extractor.getBankTransferList()}getTransactionsSummary(){return this.extractor.getTransactionsSummary()}getContent(){return this.extractor.getContent()}toJson(){return this.extractor.toJson()}}c.Extractor=L,c.Ofx=f,c.OfxExtractor=N,c.Reader=o,c.blobToString=I,c.bufferToString=h,c.fileFromPathToString=C,c.fixJsonProblems=l,c.formatDate=R});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ofx-data-extractor",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "keywords": [
5
5
  "ofx",
6
6
  "financial",