ofx-data-extractor 1.2.2 → 1.2.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.
@@ -0,0 +1,11 @@
1
+ // Ofx-data-extractor v1.2.4 Copyright (c) 2023 Fábio Pereira França
2
+ const h=/<\/\w+>/g,m=/<\w+>/g,u="<STMTTRN>",o="</STMTTRN>";function R(n){const t=n.slice(0,4),e=n.slice(4,6),r=n.slice(6,8),s=n.slice(8,10),i=n.slice(10,12),a=n.slice(12,14),[l,p]=n.slice(14).replace("[","").replace("]","").split(":");return{yyyy:t,yy:t.slice(2),y:t,MM:e,M:e,dd:r,d:r,hh:s,h:s,mm:i,m:i,ss:a,s:a,O:l,TZ:p}}function f(n,t){const e=R(n);let r=t;for(const[s,i]of Object.entries(e))r=r.replace(s,i);return r}function T(n){const t=n.replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
3
+ "`).replace(/(})/g,`
4
+ }`).replace(/(",")/g,`",
5
+ "`).replace(/,\s*}/g,`
6
+ }`).replace(/(,",)/,",");return t.at(-1)===","?t.slice(0,-1):t}function N(n){const t=n.slice(0,12),e=n.slice(12,19),r=n.slice(19),s={date:t,transactionCode:e,protocol:r};return JSON.stringify(s)}function w(n){return n.startsWith("DT")}function S(n){return n.trim()}function y(n){return n===u?n:n.replace(/[<]/g,`
7
+ `).replace(/[>]/g,":{")}function D(n){return n===o?`
8
+ ${n}`:"},"}function x(n){const t=n.search(","),e=n.search(".");return t>e?n.replace(/[.]/g,"").replace(/[,]/g,"."):n.replace(/[,]/g,"")}function g(n,t){return n.endsWith("ID")||n.endsWith("NUM")?!1:!isNaN(Number(t))}function d(n){const t=n.indexOf(u),e=n.lastIndexOf(o)+o.length,r=n.substring(t,e),s=new RegExp(u,"g"),i=new RegExp(o,"g"),a=`"STRTTRN":[${r.replace(s,"{").replace(i,"},")}]`;return{oldListText:r,newListText:a}}function E(n,t){const e={};for(const r of n){const[s,i]=r.split(":"),a=s.replace(`
9
+ `,"");e[a]=t&&g(s,i)?Number(i):i}return e}function b(n){return n.reduce((t,e)=>e.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(e.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.credit+=Number(e.TRNAMT),t),{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}function O(n){return n.toString()}async function _(n){return await new Promise((e,r)=>{import("fs").then(s=>s.readFile(n,(i,a)=>{i?r(i):e(a.toString())}))})}async function M(n){return await new Promise((e,r)=>{if(typeof window<"u"&&window.FileReader){const s=new window.FileReader;s.onload=i=>e(i.target.result),s.onerror=i=>r(i.target.error),s.readAsText(n)}else r(new Error("FileReader is not available in this environment."))})}class c{_data;_config={};constructor(t,e){e&&(this._config=e),this._data=t}static fromBuffer(t){return new c(O(t))}static async fromFilePath(t){const e=await _(t);return new c(e)}static async fromBlob(t){const e=await M(t);return new c(e)}config(t){return this._config=t,this}getHeaders(){const[t]=this._data.split("<OFX>"),e=t.split(`
10
+ `),r=i=>!!i.trim().length,s=e.filter(r);return E(s,!!this._config.nativeTypes)}getBankTransferList(){const{newListText:t}=d(this.getPartialJsonData()),e=t.slice(10),r=T(e);return JSON.parse(r)}getPartialJsonData(){const[t,e]=this._data.split("<OFX>"),r="<OFX>"+e,{sanitize:s}=this;return r.replace(h,D).replace(m,y).split(`
11
+ `).map(S).filter(Boolean).map(s,this).join("")}sanitizeValue(t,e){let r=e.replace(/[{]/g,"").replace(/(},)/g,"");return t.endsWith("AMT")&&(r=x(r)),w(t)&&(r=this.configDate(r)),t==="FITID"?this.configFinancialInstitutionTransactionId(r):this._config.nativeTypes&&g(t,r)?`${r},`:`"${r}",`}sanitize(t){let e=t;const r=e.slice(0,e.indexOf(":")),s=this.sanitizeValue,i=l=>s.call(this,r,l);return t.match(/{(\w|\W)+/)&&(e=e.replace(/({(\w|\W)+)$/,i)),e.search(/(^\w+:)/)<0?e:e.replace(r+":",`"${r}":`)}configFinancialInstitutionTransactionId(t){const{fitId:e}=this._config;return e==="separated"?N(t):`"${t}",`}configDate(t){const{formatDate:e}=this._config;return e?f(t,e):f(t,"y-M-d")}getTransactionsSummary(){const t=this.getContent(),{DTEND:e,DTSTART:r,STRTTRN:s}=t.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=b(s);return{dateStart:r,dateEnd:e,...i}}getContent(){const t=this.getPartialJsonData(),{newListText:e,oldListText:r}=d(t),s=t.replace(r,e);return JSON.parse(`{${T(s)}}`)}toJson(){const t=this.getHeaders(),e=this.getContent();return{...t,...e}}}export{c as Ofx,M as blobToString,O as bufferToString,_ as fileFromPathToString,T as fixJsonProblems,f as formatDate};
@@ -0,0 +1,11 @@
1
+ // Ofx-data-extractor v1.2.4 Copyright (c) 2023 Fábio Pereira França
2
+ (function(o,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(o=typeof globalThis<"u"?globalThis:o||self,c(o.ofx={}))})(this,function(o){"use strict";const c=/<\/\w+>/g,S=/<\w+>/g,u="<STMTTRN>",f="</STMTTRN>";function y(n){const t=n.slice(0,4),e=n.slice(4,6),s=n.slice(6,8),r=n.slice(8,10),i=n.slice(10,12),a=n.slice(12,14),[g,M]=n.slice(14).replace("[","").replace("]","").split(":");return{yyyy:t,yy:t.slice(2),y:t,MM:e,M:e,dd:s,d:s,hh:r,h:r,mm:i,m:i,ss:a,s:a,O:g,TZ:M}}function T(n,t){const e=y(n);let s=t;for(const[r,i]of Object.entries(e))s=s.replace(r,i);return s}function d(n){const t=n.replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
3
+ "`).replace(/(})/g,`
4
+ }`).replace(/(",")/g,`",
5
+ "`).replace(/,\s*}/g,`
6
+ }`).replace(/(,",)/,",");return t.at(-1)===","?t.slice(0,-1):t}function w(n){const t=n.slice(0,12),e=n.slice(12,19),s=n.slice(19),r={date:t,transactionCode:e,protocol:s};return JSON.stringify(r)}function b(n){return n.startsWith("DT")}function D(n){return n.trim()}function x(n){return n===u?n:n.replace(/[<]/g,`
7
+ `).replace(/[>]/g,":{")}function E(n){return n===f?`
8
+ ${n}`:"},"}function O(n){const t=n.search(","),e=n.search(".");return t>e?n.replace(/[.]/g,"").replace(/[,]/g,"."):n.replace(/[,]/g,"")}function p(n,t){return n.endsWith("ID")||n.endsWith("NUM")?!1:!isNaN(Number(t))}function h(n){const t=n.indexOf(u),e=n.lastIndexOf(f)+f.length,s=n.substring(t,e),r=new RegExp(u,"g"),i=new RegExp(f,"g"),a=`"STRTTRN":[${s.replace(r,"{").replace(i,"},")}]`;return{oldListText:s,newListText:a}}function _(n,t){const e={};for(const s of n){const[r,i]=s.split(":"),a=r.replace(`
9
+ `,"");e[a]=t&&p(r,i)?Number(i):i}return e}function F(n){return n.reduce((t,e)=>e.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(e.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.credit+=Number(e.TRNAMT),t),{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}function m(n){return n.toString()}async function R(n){return await new Promise((e,s)=>{import("fs").then(r=>r.readFile(n,(i,a)=>{i?s(i):e(a.toString())}))})}async function N(n){return await new Promise((e,s)=>{if(typeof window<"u"&&window.FileReader){const r=new window.FileReader;r.onload=i=>e(i.target.result),r.onerror=i=>s(i.target.error),r.readAsText(n)}else s(new Error("FileReader is not available in this environment."))})}class l{_data;_config={};constructor(t,e){e&&(this._config=e),this._data=t}static fromBuffer(t){return new l(m(t))}static async fromFilePath(t){const e=await R(t);return new l(e)}static async fromBlob(t){const e=await N(t);return new l(e)}config(t){return this._config=t,this}getHeaders(){const[t]=this._data.split("<OFX>"),e=t.split(`
10
+ `),s=i=>!!i.trim().length,r=e.filter(s);return _(r,!!this._config.nativeTypes)}getBankTransferList(){const{newListText:t}=h(this.getPartialJsonData()),e=t.slice(10),s=d(e);return JSON.parse(s)}getPartialJsonData(){const[t,e]=this._data.split("<OFX>"),s="<OFX>"+e,{sanitize:r}=this;return s.replace(c,E).replace(S,x).split(`
11
+ `).map(D).filter(Boolean).map(r,this).join("")}sanitizeValue(t,e){let s=e.replace(/[{]/g,"").replace(/(},)/g,"");return t.endsWith("AMT")&&(s=O(s)),b(t)&&(s=this.configDate(s)),t==="FITID"?this.configFinancialInstitutionTransactionId(s):this._config.nativeTypes&&p(t,s)?`${s},`:`"${s}",`}sanitize(t){let e=t;const s=e.slice(0,e.indexOf(":")),r=this.sanitizeValue,i=g=>r.call(this,s,g);return t.match(/{(\w|\W)+/)&&(e=e.replace(/({(\w|\W)+)$/,i)),e.search(/(^\w+:)/)<0?e:e.replace(s+":",`"${s}":`)}configFinancialInstitutionTransactionId(t){const{fitId:e}=this._config;return e==="separated"?w(t):`"${t}",`}configDate(t){const{formatDate:e}=this._config;return e?T(t,e):T(t,"y-M-d")}getTransactionsSummary(){const t=this.getContent(),{DTEND:e,DTSTART:s,STRTTRN:r}=t.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=F(r);return{dateStart:s,dateEnd:e,...i}}getContent(){const t=this.getPartialJsonData(),{newListText:e,oldListText:s}=h(t),r=t.replace(s,e);return JSON.parse(`{${d(r)}}`)}toJson(){const t=this.getHeaders(),e=this.getContent();return{...t,...e}}}o.Ofx=l,o.blobToString=N,o.bufferToString=m,o.fileFromPathToString=R,o.fixJsonProblems=d,o.formatDate=T});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ofx-data-extractor",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "keywords": [
5
5
  "ofx",
6
6
  "financial",
@@ -34,20 +34,13 @@
34
34
  "url": "https://github.com/Fabiopf02/ofx-ts/issues"
35
35
  },
36
36
  "license": "MIT",
37
- "description": "",
38
- "main": "dist/index.js",
39
- "module": "dist/index.mjs",
37
+ "description": "A module written in TypeScript that provides a utility to extract data from an OFX file in Node.js and Browser",
38
+ "main": "dist/umd/bundle.min.js",
39
+ "module": "dist/esm/index.min.js",
40
40
  "typings": "dist/index.d.ts",
41
41
  "files": [
42
42
  "dist/**/*"
43
43
  ],
44
- "exports": {
45
- ".": {
46
- "types": "./dist/index.d.ts",
47
- "import": "./dist/index.js",
48
- "require": "./dist/index.esm.js"
49
- }
50
- },
51
44
  "scripts": {
52
45
  "test": "jest tests/*.spec.ts --config jest.config.js",
53
46
  "test:coverage": "jest tests/*.spec.ts --coverage --config jest.config.js",
package/dist/index.js DELETED
@@ -1,10 +0,0 @@
1
- "use strict";var S=Object.create;var d=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var D=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty;var x=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of y(t))!b.call(n,s)&&s!==e&&d(n,s,{get:()=>t[s],enumerable:!(r=w(t,s))||r.enumerable});return n};var E=(n,t,e)=>(e=n!=null?S(D(n)):{},x(t||!n||!n.__esModule?d(e,"default",{value:n,enumerable:!0}):e,n));const O=/<\/\w+>/g,_=/<\w+>/g,u="<STMTTRN>",c="</STMTTRN>";function F(n){const t=n.slice(0,4),e=n.slice(4,6),r=n.slice(6,8),s=n.slice(8,10),i=n.slice(10,12),a=n.slice(12,14),[l,N]=n.slice(14).replace("[","").replace("]","").split(":");return{yyyy:t,yy:t.slice(2),y:t,MM:e,M:e,dd:r,d:r,hh:s,h:s,mm:i,m:i,ss:a,s:a,O:l,TZ:N}}function f(n,t){const e=F(n);let r=t;for(const[s,i]of Object.entries(e))r=r.replace(s,i);return r}function T(n){const t=n.replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
2
- "`).replace(/(})/g,`
3
- }`).replace(/(",")/g,`",
4
- "`).replace(/,\s*}/g,`
5
- }`).replace(/(,",)/,",");return t.at(-1)===","?t.slice(0,-1):t}function M(n){const t=n.slice(0,12),e=n.slice(12,19),r=n.slice(19),s={date:t,transactionCode:e,protocol:r};return JSON.stringify(s)}function L(n){return n.startsWith("DT")}function I(n){return n.trim()}function P(n){return n===u?n:n.replace(/[<]/g,`
6
- `).replace(/[>]/g,":{")}function A(n){return n===c?`
7
- ${n}`:"},"}function C(n){const t=n.search(","),e=n.search(".");return t>e?n.replace(/[.]/g,"").replace(/[,]/g,"."):n.replace(/[,]/g,"")}function p(n,t){return n.endsWith("ID")||n.endsWith("NUM")?!1:!isNaN(Number(t))}function g(n){const t=n.indexOf(u),e=n.lastIndexOf(c)+c.length,r=n.substring(t,e),s=new RegExp(u,"g"),i=new RegExp(c,"g"),a=`"STRTTRN":[${r.replace(s,"{").replace(i,"},")}]`;return{oldListText:r,newListText:a}}function z(n,t){const e={};for(const r of n){const[s,i]=r.split(":"),a=s.replace(`
8
- `,"");e[a]=t&&p(s,i)?Number(i):i}return e}function B(n){return n.reduce((t,e)=>e.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(e.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.credit+=Number(e.TRNAMT),t),{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}function m(n){return n.toString()}async function h(n){return await new Promise((e,r)=>{import("fs").then(s=>s.readFile(n,(i,a)=>{i?r(i):e(a.toString())}))})}async function R(n){return await new Promise((e,r)=>{if(typeof window<"u"&&window.FileReader){const s=new window.FileReader;s.onload=i=>e(i.target.result),s.onerror=i=>r(i.target.error),s.readAsText(n)}else r(new Error("FileReader is not available in this environment."))})}class o{_data;_config={};constructor(t,e){e&&(this._config=e),this._data=t}static fromBuffer(t){return new o(m(t))}static async fromFilePath(t){const e=await h(t);return new o(e)}static async fromBlob(t){const e=await R(t);return new o(e)}config(t){return this._config=t,this}getHeaders(){const[t]=this._data.split("<OFX>"),e=t.split(`
9
- `),r=i=>!!i.trim().length,s=e.filter(r);return z(s,!!this._config.nativeTypes)}getBankTransferList(){const{newListText:t}=g(this.getPartialJsonData()),e=t.slice(10),r=T(e);return JSON.parse(r)}getPartialJsonData(){const[t,e]=this._data.split("<OFX>"),r="<OFX>"+e,{sanitize:s}=this;return r.replace(O,A).replace(_,P).split(`
10
- `).map(I).filter(Boolean).map(s,this).join("")}sanitizeValue(t,e){let r=e.replace(/[{]/g,"").replace(/(},)/g,"");return t.endsWith("AMT")&&(r=C(r)),L(t)&&(r=this.configDate(r)),t==="FITID"?this.configFinancialInstitutionTransactionId(r):this._config.nativeTypes&&p(t,r)?`${r},`:`"${r}",`}sanitize(t){let e=t;const r=e.slice(0,e.indexOf(":")),s=this.sanitizeValue,i=l=>s.call(this,r,l);return t.match(/{(\w|\W)+/)&&(e=e.replace(/({(\w|\W)+)$/,i)),e.search(/(^\w+:)/)<0?e:e.replace(r+":",`"${r}":`)}configFinancialInstitutionTransactionId(t){const{fitId:e}=this._config;return e==="separated"?M(t):`"${t}",`}configDate(t){const{formatDate:e}=this._config;return e?f(t,e):f(t,"y-M-d")}getTransactionsSummary(){const t=this.getContent(),{DTEND:e,DTSTART:r,STRTTRN:s}=t.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=B(s);return{dateStart:r,dateEnd:e,...i}}getContent(){const t=this.getPartialJsonData(),{newListText:e,oldListText:r}=g(t),s=t.replace(r,e);return JSON.parse(`{${T(s)}}`)}toJson(){const t=this.getHeaders(),e=this.getContent();return{...t,...e}}}exports.Ofx=o;exports.blobToString=R;exports.bufferToString=m;exports.fileFromPathToString=h;exports.fixJsonProblems=T;exports.formatDate=f;