ofx-data-extractor 1.4.1 → 1.4.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.
package/README.md CHANGED
@@ -22,6 +22,7 @@ yarn add ofx-data-extractor
22
22
  ### Methods
23
23
  The `Ofx` class provides the following methods:
24
24
 
25
+ - `getType(): Types`: Used to get type of transactions from ofx (BANK or CREDIT_CARD)
25
26
  - `fromBuffer(data: Buffer)`: Used to read files on the node. Returns the methods
26
27
  - `fromBlob(data: Blob)`: Used to read files in the browser. Returns the methods below.
27
28
  - `config(options: OfxConfig)`: Used for formatting the generated json.`
@@ -46,3 +46,7 @@ export type TransactionsSummary = {
46
46
  dateStart: string;
47
47
  dateEnd: string;
48
48
  };
49
+ export declare enum Types {
50
+ 'BANK' = "BANK",
51
+ 'CREDIT_CARD' = "CREDIT_CARD"
52
+ }
@@ -0,0 +1,4 @@
1
+ import { STRTTRN } from '../@types/ofx';
2
+ import { Types } from '../@types/common';
3
+ export declare const extractType: (content: string) => Types;
4
+ export declare function isDebt(strttrn: STRTTRN): boolean;
@@ -1,11 +1,10 @@
1
- // Ofx-data-extractor v1.4.1 Copyright (c) 2024 Fábio Pereira França
2
- const E=/<\/\w+>/g,x=/<\w+>/g,l="<BANKMSGSRSV1>",f="</BANKMSGSRSV1>",d="<CREDITCARDMSGSRSV1>",g="</CREDITCARDMSGSRSV1>",S="<STMTTRN>",T="</STMTTRN>",N="_#_",M=new RegExp(N,"g"),y=[l,S,d],L=[f,g,T];function b(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 _(e,t){const n=b(e);let r=t;for(const[s,a]of Object.entries(n))r=r.replace(s,a);return r}function u(e){return e.replace(/(\\)/g,"\\\\").replace(E,t=>D(t,!0)).replace(x,t=>w(t,!0)).replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(]")/g,'],"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
3
- "`).replace(/(})/g,`
4
- }`).replace(/(",")/g,`",
5
- "`).replace(/,\s*}/g,`
6
- }`).replace(/(,",)/,",").replace(M,'\\"').slice(0,-1)}const k=e=>JSON.stringify({date:e.slice(0,12),transactionCode:e.slice(12,19),protocol:e.slice(19)}),G=e=>e.startsWith("DT"),p=e=>e.trim();function w(e,t=!1){return y.includes(e)&&!t?e:e.replace(/[<]/g,`
7
- "`).replace(/[>]/g,'":{')}function D(e,t=!1){return L.includes(e)&&!t?`
8
- ${e}`:"},"}function F({fitId:e,fitValue:t}){return e==="separated"?k(t):`"${t}",`}function J(e){return e.search(",")>e.search(".")?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function P({dateString:e,formatDate:t="y-M-d"}){return _(e,t)}function K(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"").replace(/["]/g,N);const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=J(t)),G(n)&&(t=P({dateString:t,formatDate:e.formatDate})),n==="FITID"?F({fitId:e.fitId,fitValue:t}):e.nativeTypes&&A(n,t)?`${Number(t)},`:`"${t}",`}function X(e,t){let n=e;return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,r=>K({field:n.slice(0,n.indexOf(":")),value:r,...t}))),n}function A(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function O(e){const t=e.indexOf(S),n=e.lastIndexOf(T)+T.length,r=e.substring(t,n),s=new RegExp(S,"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(l)>0))return{newBankStatementTransactions:null,oldBankStatementTransactions:null};const n=O(e.substring(e.indexOf(l),e.indexOf(f)+f.length));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=O(n);return{newCreditCardStatementTransactions:r.newListText,oldCreditCardStatementTransactions:r.oldListText}}function $(e){const t=[];for(const n of e)t.push(...n.replace("<?xml"," ").replace("<?OFX"," ").replace("?>"," ").split(" ").filter(Boolean).map(p).map(r=>r.replace("=",":").replace(/["]/g,"")));return t}function V(e,t){const n={};e.join("").search("<?")>-1&&(e=$(e));for(const r of e){const[s,a]=r.split(":"),i=s.replace(`
9
- `,"").toUpperCase();n[i]=t&&A(s,a)?Number(a):a}return n}function z(e){return e.reduce((t,n)=>{const r=Math.abs(+n.TRNAMT);return n.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=r,t):(t.amountOfCredits++,t.credit+=r,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 X(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),s="<OFX>"+r,{sanitizeRow:a}=this;return s.replace(E,i=>D(i)).replace(x,i=>w(i)).split(`
10
- `).map(p).filter(Boolean).map(a,this).join("")}}function h(e){return e.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 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 fromBlob(t){return new o(await I(t))}}class j{configInstance={}}class B extends j{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=m(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${u(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN:[]}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=C(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${u(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}=m(n),{newCreditCardStatementTransactions:a,oldCreditCardStatementTransactions:i}=C(n);let c=n;return r&&(c=c.replace(s,r)),a&&(c=c.replace(i,a)),JSON.parse(`{${u(c)}}`)}}class H{customExtractor;customExtractorInstance;dataReaderInstance=new o("");constructor(t){this.customExtractor=t,this.customExtractorInstance=t||new B,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>");return V(t.split(`
11
- `).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 R{extractor;constructor(t,n){this.extractor=new H(new B),this.extractor.data(new o(t)),this.extractor.config(n||{})}static fromBuffer(t){return new R(o.fromBuffer(t).getData())}static async fromBlob(t){const n=await o.fromBlob(t);return new R(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{H as Extractor,R as Ofx,B as OfxExtractor,o as Reader,I as blobToString,h as bufferToString,u as fixJsonProblems,_ as formatDate};
1
+ // Ofx-data-extractor v1.4.3 Copyright (c) 2024 Fábio Pereira França
2
+ const p=/<\/\w+>/g,N=/<\w+>/g,T="<BANKMSGSRSV1>",d="</BANKMSGSRSV1>",g="<CREDITCARDMSGSRSV1>",S="</CREDITCARDMSGSRSV1>",R="<STMTTRN>",u="</STMTTRN>",x="_#_",M=new RegExp(x,"g"),L=[T,R,g],b=[d,S,u];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=e=>e.includes(T)?l.BANK:l.CREDIT_CARD;function F(e){const t=String(e.TRNTYPE).toLocaleLowerCase();return t==="1"||t.startsWith("deb")}function f(e){const t=e.replace(/(\\)/g,"\\\\").replace(p,n=>A(n,!0)).replace(N,n=>w(n,!0)).replace(`
3
+ `,"").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 K=e=>JSON.stringify({date:e.slice(0,12),transactionCode:e.slice(12,19),protocol:e.slice(19)}),J=e=>e.startsWith("DT"),D=e=>e.trim();function w(e,t=!1){return L.includes(e)&&!t?e:e.replace(/[<]/g,`
4
+ "`).replace(/[>]/g,'":{')}function A(e,t=!1){return b.includes(e)&&!t?`
5
+ ${e}`:`},
6
+ `}function P({fitId:e,fitValue:t}){return e==="separated"?K(t):`"${t}",`}function $(e){return e.search(",")>e.search(".")?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function X({dateString:e,formatDate:t="y-M-d"}){return k(e,t)}function V(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"").replace(/["]/g,x);const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=$(t)),J(n)&&(t=X({dateString:t,formatDate:e.formatDate})),n==="FITID"?P({fitId:e.fitId,fitValue:t}):e.nativeTypes&&B(n,t)?`${Number(t)},`:`"${t}",`}function W(e,t){let n=e;return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,r=>V({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 O(e){const t=e.indexOf(R),n=e.lastIndexOf(u)+u.length,r=e.substring(t,n),s=new RegExp(R,"g"),a=new RegExp(u,"g"),i=`"STRTTRN":[${r.replace(s,"{").replace(a,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function m(e){if(!(e.indexOf(T)>0))return{newBankStatementTransactions:null,oldBankStatementTransactions:null};const n=O(e.substring(e.indexOf(T),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=O(n);return{newCreditCardStatementTransactions:r.newListText,oldCreditCardStatementTransactions:r.oldListText}}function z(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 j(e,t){const n={};e.join("").search("<?")>-1&&(e=z(e));for(const r of e){const[s,a]=r.split(":"),i=s.replace(`
7
+ `,"").toUpperCase();n[i]=t&&B(s,a)?Number(a):String(a).replace(/\?>/,"")}return n}function H(e){return e.reduce((t,n)=>{const r=Math.abs(+n.TRNAMT);return F(n)?(t.amountOfDebits++,t.debit+=r,t):(t.amountOfCredits++,t.credit+=r,t)},{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}class U{config;internConfig={};constructor(t){this.config=t,this.internConfig=t}getConfig(){return this.internConfig}sanitizeRow(t){return W(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),s="<OFX>"+r,{sanitizeRow:a}=this;return s.replace(/<([^\/][^>]*)>/g,`
8
+ <$1>`).replace(p,i=>A(i)).replace(N,i=>w(i)).split(`
9
+ `).map(D).filter(Boolean).map(a,this).join("")}}function I(e){return e.toString()}async function E(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 E(t))}static fromString(t){return new c(t)}static fromBuffer(t){return new c(I(t))}static async fromBlob(t){return new c(await E(t))}}class v{configInstance={}}class y extends v{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,STRTTRN:a}=n.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=H(a);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 Y{customExtractor;customExtractorInstance;dataReaderInstance=new c("");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 U(t)),this}getType(){return G(this.dataReaderInstance.getData())}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>");return j(t.split(`
10
+ `).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 Y(new y),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{Y as Extractor,C as Ofx,y as OfxExtractor,c as Reader,E as blobToString,I as bufferToString,f as fixJsonProblems,k as formatDate};
@@ -10,6 +10,7 @@ export declare class Extractor<T = any> implements IExtractor<T> {
10
10
  constructor(customExtractor?: CustomExtractor | undefined);
11
11
  data(readData: Reader): this;
12
12
  config(config: ExtractorConfig): this;
13
+ getType(): import("../@types/common").Types;
13
14
  getHeaders(): MetaData;
14
15
  getBankTransferList(): STRTTRN[];
15
16
  getCreditCardTransferList(): STRTTRN[];
@@ -1,6 +1,7 @@
1
1
  import { OfxResponse, OfxStructure, STRTTRN } from '../@types/ofx';
2
- import { ExtractorConfig, MetaData, TransactionsSummary } from '../@types/common';
2
+ import { ExtractorConfig, MetaData, TransactionsSummary, Types } from '../@types/common';
3
3
  export interface IExtractor<T> {
4
+ getType(): Types;
4
5
  config(config: ExtractorConfig): this;
5
6
  getHeaders(): MetaData;
6
7
  getBankTransferList(): STRTTRN[];
@@ -4,6 +4,7 @@ import { OfxResponse, OfxStructure, STRTTRN, OfxConfig } from '../@types/ofx';
4
4
  export declare class Ofx {
5
5
  private extractor;
6
6
  constructor(data: string, config?: OfxConfig);
7
+ getType(): import("../@types/common").Types;
7
8
  static fromBuffer(data: Buffer): Ofx;
8
9
  static fromBlob(blob: Blob): Promise<Ofx>;
9
10
  config(config: OfxConfig): this;
@@ -1,11 +1,10 @@
1
- // Ofx-data-extractor v1.4.1 Copyright (c) 2024 Fábio Pereira França
2
- (function(o,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(o=typeof globalThis<"u"?globalThis:o||self,f(o.ofx={}))})(this,function(o){"use strict";const f=/<\/\w+>/g,x=/<\w+>/g,g="<BANKMSGSRSV1>",S="</BANKMSGSRSV1>",m="<CREDITCARDMSGSRSV1>",R="</CREDITCARDMSGSRSV1>",C="<STMTTRN>",T="</STMTTRN>",N="_#_",L=new RegExp(N,"g"),_=[g,C,m],k=[S,R,T];function G(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 p(e,t){const n=G(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(f,t=>O(t,!0)).replace(x,t=>D(t,!0)).replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(]")/g,'],"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
3
- "`).replace(/(})/g,`
4
- }`).replace(/(",")/g,`",
5
- "`).replace(/,\s*}/g,`
6
- }`).replace(/(,",)/,",").replace(L,'\\"').slice(0,-1)}const F=e=>JSON.stringify({date:e.slice(0,12),transactionCode:e.slice(12,19),protocol:e.slice(19)}),J=e=>e.startsWith("DT"),w=e=>e.trim();function D(e,t=!1){return _.includes(e)&&!t?e:e.replace(/[<]/g,`
7
- "`).replace(/[>]/g,'":{')}function O(e,t=!1){return k.includes(e)&&!t?`
8
- ${e}`:"},"}function P({fitId:e,fitValue:t}){return e==="separated"?F(t):`"${t}",`}function K(e){return e.search(",")>e.search(".")?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function X({dateString:e,formatDate:t="y-M-d"}){return p(e,t)}function $(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"").replace(/["]/g,N);const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=K(t)),J(n)&&(t=X({dateString:t,formatDate:e.formatDate})),n==="FITID"?P({fitId:e.fitId,fitValue:t}):e.nativeTypes&&A(n,t)?`${Number(t)},`:`"${t}",`}function V(e,t){let n=e;return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,r=>$({field:n.slice(0,n.indexOf(":")),value:r,...t}))),n}function A(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function B(e){const t=e.indexOf(C),n=e.lastIndexOf(T)+T.length,r=e.substring(t,n),s=new RegExp(C,"g"),a=new RegExp(T,"g"),i=`"STRTTRN":[${r.replace(s,"{").replace(a,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function b(e){if(!(e.indexOf(g)>0))return{newBankStatementTransactions:null,oldBankStatementTransactions:null};const n=B(e.substring(e.indexOf(g),e.indexOf(S)+S.length));return{newBankStatementTransactions:n.newListText,oldBankStatementTransactions:n.oldListText}}function y(e){if(!(e.indexOf(m)>0))return{newCreditCardStatementTransactions:null,oldCreditCardStatementTransactions:null};const n=e.substring(e.indexOf(m),e.indexOf(R)+R.length),r=B(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(w).map(r=>r.replace("=",":").replace(/["]/g,"")));return t}function z(e,t){const n={};e.join("").search("<?")>-1&&(e=j(e));for(const r of e){const[s,a]=r.split(":"),i=s.replace(`
9
- `,"").toUpperCase();n[i]=t&&A(s,a)?Number(a):a}return n}function W(e){return e.reduce((t,n)=>{const r=Math.abs(+n.TRNAMT);return n.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=r,t):(t.amountOfCredits++,t.credit+=r,t)},{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}class H{config;internConfig={};constructor(t){this.config=t,this.internConfig=t}getConfig(){return this.internConfig}sanitizeRow(t){return V(t,this.internConfig)}getPartialJsonData(t){const[n,r]=t.split("<OFX>"),s="<OFX>"+r,{sanitizeRow:a}=this;return s.replace(f,i=>O(i)).replace(x,i=>D(i)).split(`
10
- `).map(w).filter(Boolean).map(a,this).join("")}}function h(e){return e.toString()}async function E(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(h(t))}async fromBlob(t){return new c(await E(t))}static fromString(t){return new c(t)}static fromBuffer(t){return new c(h(t))}static async fromBlob(t){return new c(await E(t))}}class U{configInstance={}}class I extends U{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=b(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${l(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN:[]}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=y(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=W(a);return{dateStart:s,dateEnd:r,...i}}getContent(t){const n=this.configInstance.getPartialJsonData(t),{newBankStatementTransactions:r,oldBankStatementTransactions:s}=b(n),{newCreditCardStatementTransactions:a,oldCreditCardStatementTransactions:i}=y(n);let u=n;return r&&(u=u.replace(s,r)),a&&(u=u.replace(i,a)),JSON.parse(`{${l(u)}}`)}}class M{customExtractor;customExtractorInstance;dataReaderInstance=new c("");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 H(t)),this}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>");return z(t.split(`
11
- `).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 d{extractor;constructor(t,n){this.extractor=new M(new I),this.extractor.data(new c(t)),this.extractor.config(n||{})}static fromBuffer(t){return new d(c.fromBuffer(t).getData())}static async fromBlob(t){const n=await c.fromBlob(t);return new d(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()}}o.Extractor=M,o.Ofx=d,o.OfxExtractor=I,o.Reader=c,o.blobToString=E,o.bufferToString=h,o.fixJsonProblems=l,o.formatDate=p});
1
+ // Ofx-data-extractor v1.4.3 Copyright (c) 2024 Fábio Pereira França
2
+ (function(o,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(o=typeof globalThis<"u"?globalThis:o||self,u(o.ofx={}))})(this,function(o){"use strict";const u=/<\/\w+>/g,N=/<\w+>/g,f="<BANKMSGSRSV1>",R="</BANKMSGSRSV1>",m="<CREDITCARDMSGSRSV1>",C="</CREDITCARDMSGSRSV1>",h="<STMTTRN>",l="</STMTTRN>",p="_#_",_=new RegExp(p,"g"),k=[f,h,m],G=[R,C,l];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 D(e,t){const n=F(e);let r=t;for(const[s,a]of Object.entries(n))r=r.replace(s,a);return r}var d;(function(e){e.BANK="BANK",e.CREDIT_CARD="CREDIT_CARD"})(d||(d={}));const J=e=>e.includes(f)?d.BANK:d.CREDIT_CARD;function K(e){const t=String(e.TRNTYPE).toLocaleLowerCase();return t==="1"||t.startsWith("deb")}function g(e){const t=e.replace(/(\\)/g,"\\\\").replace(u,n=>B(n,!0)).replace(N,n=>A(n,!0)).replace(`
3
+ `,"").replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(]")/g,'],"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/,\s*}/g,"}").replace(/(,",)/,",").replace(_,'\\"').slice(0,-1);return t.endsWith(",")?t.slice(0,-1):t}const P=e=>JSON.stringify({date:e.slice(0,12),transactionCode:e.slice(12,19),protocol:e.slice(19)}),$=e=>e.startsWith("DT"),w=e=>e.trim();function A(e,t=!1){return k.includes(e)&&!t?e:e.replace(/[<]/g,`
4
+ "`).replace(/[>]/g,'":{')}function B(e,t=!1){return G.includes(e)&&!t?`
5
+ ${e}`:`},
6
+ `}function X({fitId:e,fitValue:t}){return e==="separated"?P(t):`"${t}",`}function V(e){return e.search(",")>e.search(".")?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function W({dateString:e,formatDate:t="y-M-d"}){return D(e,t)}function j(e){let t=e.value.replace(/[{]/g,"").replace(/(},)/g,"").replace(/["]/g,p);const n=e.field.replace(/['"]/g,"");return n.endsWith("AMT")&&(t=V(t)),$(n)&&(t=W({dateString:t,formatDate:e.formatDate})),n==="FITID"?X({fitId:e.fitId,fitValue:t}):e.nativeTypes&&O(n,t)?`${Number(t)},`:`"${t}",`}function z(e,t){let n=e;return e.match(/{(\w|\W)+/)&&(n=n.replace(/({(\w|\W)+)$/,r=>j({field:n.slice(0,n.indexOf(":")),value:r,...t}))),n}function O(e,t){return e.endsWith("ID")||e.endsWith("NUM")?!1:!isNaN(Number(t))}function y(e){const t=e.indexOf(h),n=e.lastIndexOf(l)+l.length,r=e.substring(t,n),s=new RegExp(h,"g"),a=new RegExp(l,"g"),i=`"STRTTRN":[${r.replace(s,"{").replace(a,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function b(e){if(!(e.indexOf(f)>0))return{newBankStatementTransactions:null,oldBankStatementTransactions:null};const n=y(e.substring(e.indexOf(f),e.indexOf(R)+R.length));return{newBankStatementTransactions:n.newListText,oldBankStatementTransactions:n.oldListText}}function M(e){if(!(e.indexOf(m)>0))return{newCreditCardStatementTransactions:null,oldCreditCardStatementTransactions:null};const n=e.substring(e.indexOf(m),e.indexOf(C)+C.length),r=y(n);return{newCreditCardStatementTransactions:r.newListText,oldCreditCardStatementTransactions:r.oldListText}}function H(e){const t=[];for(const n of e)t.push(...n.replace("<?xml"," ").replace("<?OFX"," ").replace("?>"," ").split(" ").filter(Boolean).map(w).map(r=>r.replace("=",":").replace(/["]/g,"")));return t}function U(e,t){const n={};e.join("").search("<?")>-1&&(e=H(e));for(const r of e){const[s,a]=r.split(":"),i=s.replace(`
7
+ `,"").toUpperCase();n[i]=t&&O(s,a)?Number(a):String(a).replace(/\?>/,"")}return n}function v(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 Y{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,`
8
+ <$1>`).replace(u,i=>B(i)).replace(N,i=>A(i)).split(`
9
+ `).map(w).filter(Boolean).map(a,this).join("")}}function E(e){return e.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 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(E(t))}async fromBlob(t){return new c(await I(t))}static fromString(t){return new c(t)}static fromBuffer(t){return new c(E(t))}static async fromBlob(t){return new c(await I(t))}}class Q{configInstance={}}class x extends Q{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=b(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${g(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN:[]}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=M(this.configInstance.getPartialJsonData(t));return n?JSON.parse(`{${g(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}=b(n),{newCreditCardStatementTransactions:a,oldCreditCardStatementTransactions:i}=M(n);let T=n;return r&&(T=T.replace(s,r)),a&&(T=T.replace(i,a)),JSON.parse(`{${g(T)}}`)}}class L{customExtractor;customExtractorInstance;dataReaderInstance=new c("");constructor(t){this.customExtractor=t,this.customExtractorInstance=t||new x,this.config({})}data(t){return this.dataReaderInstance=t,this}config(t){return this.customExtractorInstance.setConfig(new Y(t)),this}getType(){return J(this.dataReaderInstance.getData())}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>");return U(t.split(`
10
+ `).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 S{extractor;constructor(t,n){this.extractor=new L(new x),this.extractor.data(new c(t)),this.extractor.config(n||{})}getType(){return this.extractor.getType()}static fromBuffer(t){return new S(c.fromBuffer(t).getData())}static async fromBlob(t){const n=await c.fromBlob(t);return new S(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()}}o.Extractor=L,o.Ofx=S,o.OfxExtractor=x,o.Reader=c,o.blobToString=I,o.bufferToString=E,o.fixJsonProblems=g,o.formatDate=D});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ofx-data-extractor",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "keywords": [
5
5
  "ofx",
6
6
  "financial",