ofx-data-extractor 1.3.3 → 1.3.4
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
|
@@ -21,8 +21,7 @@ yarn add ofx-data-extractor
|
|
|
21
21
|
### Methods
|
|
22
22
|
The `Ofx` class provides the following methods:
|
|
23
23
|
|
|
24
|
-
- `
|
|
25
|
-
- `async fromFilePath(path: string)`: Used to read files on the node. Returns the methods below.
|
|
24
|
+
- `fromBuffer(data: Buffer)`: Used to read files on the node. Returns the methods
|
|
26
25
|
- `fromBlob(data: Blob)`: Used to read files in the browser. Returns the methods below.
|
|
27
26
|
- `config(options: OfxConfig)`: Used for formatting the generated json.`
|
|
28
27
|
- `getHeaders(): OFXMetaData`: Returns the metadata section of the OFX file as an object.
|
|
@@ -65,8 +64,6 @@ import fs from 'fs'
|
|
|
65
64
|
|
|
66
65
|
const file = await fs.readFile('/path/to/file')
|
|
67
66
|
const ofx = await Ofx.fromBuffer(file)
|
|
68
|
-
// OR
|
|
69
|
-
const ofx = await Ofx.fromFilePath('/path/to/file')
|
|
70
67
|
|
|
71
68
|
const ofxResponse = ofx.toJson()
|
|
72
69
|
console.log(ofxResponse)
|
package/dist/common/reader.d.ts
CHANGED
package/dist/esm/index.min.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// Ofx-data-extractor v1.3.
|
|
2
|
-
const
|
|
1
|
+
// Ofx-data-extractor v1.3.3 Copyright (c) 2024 Fábio Pereira França
|
|
2
|
+
const N=/<\/\w+>/g,x=/<\w+>/g,E="<BANKMSGSRSV1>",f="</BANKMSGSRSV1>",d="<CREDITCARDMSGSRSV1>",l="</CREDITCARDMSGSRSV1>",g="<STMTTRN>",c="</STMTTRN>",O=[E,g,d],b=[f,l,c];function M(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 R(e,t){const n=M(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(N,t=>w(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
|
|
7
|
-
"`).replace(/[>]/g,'":{')}function
|
|
8
|
-
${e}`:"},"}function
|
|
9
|
-
`,"");n[i]=t&&
|
|
10
|
-
`).map(
|
|
11
|
-
`),r=
|
|
6
|
+
}`).replace(/(,",)/,",").slice(0,-1)}function y(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 B(e){return e.startsWith("DT")}function _(e){return e.trim()}function D(e,t=!1){return O.includes(e)&&!t?e:e.replace(/[<]/g,`
|
|
7
|
+
"`).replace(/[>]/g,'":{')}function w(e,t=!1){return b.includes(e)&&!t?`
|
|
8
|
+
${e}`:"},"}function G({fitId:e,fitValue:t}){return e==="separated"?y(t):`"${t}",`}function k(e){const t=e.search(","),n=e.search(".");return t>n?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function F({dateString:e,formatDate:t}){return t?R(e,t):R(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)),B(n)&&(t=F({dateString:t,formatDate:e.formatDate})),n==="FITID"?G({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(g),n=e.lastIndexOf(c)+c.length,r=e.substring(t,n),a=new RegExp(g,"g"),s=new RegExp(c,"g"),i=`"STRTTRN":[${r.replace(a,"{").replace(s,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function m(e){const t=e.substring(e.indexOf(E),e.indexOf(f)+f.length),n=A(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(l)+l.length),r=A(n);return{newCreditCardStatementTransactions:r.newListText,oldCreditCardStatementTransactions:r.oldListText}}function P(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 $(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 z{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(N,i=>w(i)).replace(x,i=>D(i)).split(`
|
|
10
|
+
`).map(_).filter(Boolean).map(s,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 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(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 V{configInstance={}}class L extends V{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=m(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${T(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=C(this.configInstance.getPartialJsonData(t));return n?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=$(s);return{dateStart:a,dateEnd:r,...i}}getContent(t){const n=this.configInstance.getPartialJsonData(t),{newBankStatementTransactions:r,oldBankStatementTransactions:a}=m(n),{newCreditCardStatementTransactions:s,oldCreditCardStatementTransactions:i}=C(n);let u=n.replace(a,r);return s&&(u=u.replace(i,s)),JSON.parse(`{${T(u)}}`)}}class W{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 z(t)),this}getHeaders(){const[t]=this.dataReaderInstance.getData().split("<OFX>"),n=t.split(`
|
|
11
|
+
`),r=s=>!!s.trim().length,a=n.filter(r);return P(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 S{extractor;constructor(t,n){this.extractor=new W(new L),this.extractor.data(new o(t)),this.extractor.config(n||{})}static fromBuffer(t){return new S(o.fromBuffer(t).getData())}static async fromBlob(t){const n=await o.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()}getTransactionsSummary(){return this.extractor.getTransactionsSummary()}getContent(){return this.extractor.getContent()}toJson(){return this.extractor.toJson()}}export{W as Extractor,S as Ofx,L as OfxExtractor,o as Reader,I as blobToString,h as bufferToString,T as fixJsonProblems,R as formatDate};
|
|
@@ -6,10 +6,8 @@ export declare class Reader {
|
|
|
6
6
|
getData(): string;
|
|
7
7
|
fromString(data: string): Reader;
|
|
8
8
|
fromBuffer(data: Buffer): Reader;
|
|
9
|
-
fromFilePath(pathname: string): Promise<Reader>;
|
|
10
9
|
fromBlob(blob: Blob): Promise<Reader>;
|
|
11
10
|
static fromString(data: string): Reader;
|
|
12
11
|
static fromBuffer(data: Buffer): Reader;
|
|
13
|
-
static fromFilePath(pathname: string): Promise<Reader>;
|
|
14
12
|
static fromBlob(blob: Blob): Promise<Reader>;
|
|
15
13
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Ofx } from './older-implementation/main';
|
|
2
|
-
export { blobToString, bufferToString
|
|
2
|
+
export { blobToString, bufferToString } from './common/reader';
|
|
3
3
|
export { fixJsonProblems } from './common/parse';
|
|
4
4
|
export { formatDate } from './common/date';
|
|
5
5
|
export type { IExtractor } from './interfaces/extractor.interface';
|
|
@@ -5,7 +5,6 @@ export declare class Ofx {
|
|
|
5
5
|
private extractor;
|
|
6
6
|
constructor(data: string, config?: OfxConfig);
|
|
7
7
|
static fromBuffer(data: Buffer): Ofx;
|
|
8
|
-
static fromFilePath(pathname: string): Promise<Ofx>;
|
|
9
8
|
static fromBlob(blob: Blob): Promise<Ofx>;
|
|
10
9
|
config(config: OfxConfig): this;
|
|
11
10
|
getHeaders(): MetaData;
|
package/dist/umd/bundle.min.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
// Ofx-data-extractor v1.3.
|
|
2
|
-
(function(
|
|
1
|
+
// Ofx-data-extractor v1.3.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,x=/<\w+>/g,E="<BANKMSGSRSV1>",l="</BANKMSGSRSV1>",g="<CREDITCARDMSGSRSV1>",S="</CREDITCARDMSGSRSV1>",m="<STMTTRN>",f="</STMTTRN>",y=[E,m,g],M=[l,S,f];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 R(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 T(e){return e.replace(/(\\)/g,"\\\\").replace(u,t=>w(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
|
|
7
|
-
"`).replace(/[>]/g,'":{')}function
|
|
8
|
-
${e}`:"},"}function
|
|
9
|
-
`,"");n[i]=t&&
|
|
10
|
-
`).map(k).filter(Boolean).map(
|
|
11
|
-
`),r=
|
|
6
|
+
}`).replace(/(,",)/,",").slice(0,-1)}function _(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 G(e){return e.startsWith("DT")}function k(e){return e.trim()}function D(e,t=!1){return y.includes(e)&&!t?e:e.replace(/[<]/g,`
|
|
7
|
+
"`).replace(/[>]/g,'":{')}function w(e,t=!1){return M.includes(e)&&!t?`
|
|
8
|
+
${e}`:"},"}function J({fitId:e,fitValue:t}){return e==="separated"?_(t):`"${t}",`}function F(e){const t=e.search(","),n=e.search(".");return t>n?e.replace(/[.]/g,"").replace(/[,]/g,"."):e.replace(/[,]/g,"")}function P({dateString:e,formatDate:t}){return t?R(e,t):R(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=F(t)),G(n)&&(t=P({dateString:t,formatDate:e.formatDate})),n==="FITID"?J({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(m),n=e.lastIndexOf(f)+f.length,r=e.substring(t,n),a=new RegExp(m,"g"),s=new RegExp(f,"g"),i=`"STRTTRN":[${r.replace(a,"{").replace(s,"},")}]`;return{oldListText:e,newListText:e.replace(r,i)}}function A(e){const t=e.substring(e.indexOf(E),e.indexOf(l)+l.length),n=b(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=b(n);return{newCreditCardStatementTransactions:r.newListText,oldCreditCardStatementTransactions:r.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=>w(i)).replace(x,i=>D(i)).split(`
|
|
10
|
+
`).map(k).filter(Boolean).map(s,this).join("")}}function C(e){return e.toString()}async function h(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 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(C(t))}async fromBlob(t){return new c(await h(t))}static fromString(t){return new c(t)}static fromBuffer(t){return new c(C(t))}static async fromBlob(t){return new c(await h(t))}}class W{configInstance={}}class I extends W{setConfig(t){this.configInstance=t}getBankTransferList(t){const{newBankStatementTransactions:n}=A(this.configInstance.getPartialJsonData(t));return JSON.parse(`{${T(n)}}`)?.BANKMSGSRSV1?.STMTTRNRS?.STMTRS?.BANKTRANLIST?.STRTTRN}getCreditCardTransferList(t){const{newCreditCardStatementTransactions:n}=O(this.configInstance.getPartialJsonData(t));return n?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}=A(n),{newCreditCardStatementTransactions:s,oldCreditCardStatementTransactions:i}=O(n);let N=n.replace(a,r);return s&&(N=N.replace(i,s)),JSON.parse(`{${T(N)}}`)}}class L{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 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 d{extractor;constructor(t,n){this.extractor=new L(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()}getTransactionsSummary(){return this.extractor.getTransactionsSummary()}getContent(){return this.extractor.getContent()}toJson(){return this.extractor.toJson()}}o.Extractor=L,o.Ofx=d,o.OfxExtractor=I,o.Reader=c,o.blobToString=h,o.bufferToString=C,o.fixJsonProblems=T,o.formatDate=R});
|