ofx-data-extractor 1.2.2 → 1.2.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/dist/esm/index.min.js +11 -0
- package/dist/helpers.d.ts +2 -1
- package/dist/index.d.ts +0 -1
- package/dist/main.d.ts +3 -2
- package/dist/types.d.ts +7 -0
- package/dist/umd/bundle.min.js +11 -0
- package/package.json +4 -11
- package/dist/index.js +0 -10
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Ofx-data-extractor v1.2.1 Copyright (c) 2023 Fábio Pereira França
|
|
2
|
+
const h=/<\/\w+>/g,m=/<\w+>/g,u="<STMTTRN>",a="</STMTTRN>";function R(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),o=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:s,d:s,hh:r,h:r,mm:i,m:i,ss:o,s:o,O:l,TZ:p}}function f(n,t){const e=R(n);let s=t;for(const[r,i]of Object.entries(e))s=s.replace(r,i);return s}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 y(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 N(n){return n.startsWith("DT")}function w(n){return n.trim()}function S(n){return n===u?n:n.replace(/[<]/g,`
|
|
7
|
+
`).replace(/[>]/g,":{")}function D(n){return n===a?`
|
|
8
|
+
${n}`:"},"}function b(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(a)+a.length,s=n.substring(t,e),r=new RegExp(u,"g"),i=new RegExp(a,"g"),o=`"STRTTRN":[${s.replace(r,"{").replace(i,"},")}]`;return{oldListText:s,newListText:o}}function E(n,t){const e={};for(const s of n){const[r,i]=s.split(":"),o=r.replace(`
|
|
9
|
+
`,"");e[o]=t&&g(r,i)?Number(i):i}return e}function x(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 M(n){return await new Promise((e,s)=>{import("fs").then(r=>r.readFile(n,(i,o)=>{i?s(i):e(o.toString())}))})}function _(n,t){let e=n;for(const s of t.split(".")){if(!Object.hasOwn(e,s))return;e=e[s]}return e}async function F(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 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 M(t);return new c(e)}static async fromBlob(t){const e=await F(t);return new c(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 E(r,!!this._config.nativeTypes)}getBankTransferList(){const{newListText:t}=d(this.getPartialJsonData()),e=t.slice(10),s=T(e);return JSON.parse(s)}getPartialJsonData(){const[t,e]=this._data.split("<OFX>"),s="<OFX>"+e,{sanitize:r}=this;return s.replace(h,D).replace(m,S).split(`
|
|
11
|
+
`).map(w).filter(Boolean).map(r,this).join("")}sanitizeValue(t,e){let s=e.replace(/[{]/g,"").replace(/(},)/g,"");return t.endsWith("AMT")&&(s=b(s)),N(t)&&(s=this.configDate(s)),t==="FITID"?this.configFinancialInstitutionTransactionId(s):this._config.nativeTypes&&g(t,s)?`${s},`:`"${s}",`}sanitize(t){let e=t;const s=e.slice(0,e.indexOf(":")),r=this.sanitizeValue,i=l=>r.call(this,s,l);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"?y(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:s,STRTTRN:r}=t.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=x(r);return{dateStart:s,dateEnd:e,...i}}customResponse(t,e){const s=new Map;for(const[r,i]of Object.entries(e))s.set(r,_(t,i));return Object.fromEntries(s)}getContent(){const t=this.getPartialJsonData(),{newListText:e,oldListText:s}=d(t),r=t.replace(s,e);return JSON.parse(`{${T(r)}}`)}toJson(t){const e=this.getHeaders(),s=this.getContent(),r={...e,...s};return t?this.customResponse(r,t):r}}export{c as Ofx,F as blobToString,O as bufferToString,M as fileFromPathToString,T as fixJsonProblems,f as formatDate};
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { STRTTRN as STRTTRNType } from './types';
|
|
2
|
+
import { OfxResponse, STRTTRN as STRTTRNType } from './types';
|
|
3
3
|
export declare function formatDate(date: string, format: string): string;
|
|
4
4
|
export declare function fixJsonProblems(content: string): string;
|
|
5
5
|
export declare function extractFinancialInstitutionTransactionId(fitid: string): string;
|
|
@@ -24,4 +24,5 @@ export declare function getTransactionsSummary(STRTTRN: STRTTRNType[]): {
|
|
|
24
24
|
};
|
|
25
25
|
export declare function bufferToString(data: Buffer): string;
|
|
26
26
|
export declare function fileFromPathToString(pathname: string): Promise<string>;
|
|
27
|
+
export declare function getValueByKeys(objectData: OfxResponse, keys: string): OfxResponse | undefined;
|
|
27
28
|
export declare function blobToString(blob: Blob): Promise<string>;
|
package/dist/index.d.ts
CHANGED
package/dist/main.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { OfxConfig, OFXMetaData,
|
|
2
|
+
import { Format, OfxConfig, OFXMetaData, OfxStructure, STRTTRN } from './types';
|
|
3
3
|
export declare class Ofx {
|
|
4
4
|
private _data;
|
|
5
5
|
private _config;
|
|
@@ -23,6 +23,7 @@ export declare class Ofx {
|
|
|
23
23
|
dateStart: import("./types").DateResponse;
|
|
24
24
|
dateEnd: import("./types").DateResponse;
|
|
25
25
|
};
|
|
26
|
+
private customResponse;
|
|
26
27
|
getContent(): OfxStructure;
|
|
27
|
-
toJson():
|
|
28
|
+
toJson<T>(format: Format<T>): any;
|
|
28
29
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -110,5 +110,12 @@ export type OfxStructure = {
|
|
|
110
110
|
};
|
|
111
111
|
};
|
|
112
112
|
};
|
|
113
|
+
type OfxKeysIntermediate<T, P = ''> = {
|
|
114
|
+
[K in keyof T]: T[K] extends object ? `${P & string}${P extends '' ? '' : '.'}${K}` | OfxKeysIntermediate<T[K], `${P & string}${P extends '' ? '' : '.'}${K}`> : `${P & string}${P extends '' ? '' : '.'}${K}`;
|
|
115
|
+
}[keyof T];
|
|
113
116
|
export type OfxResponse = OFXMetaData & OfxStructure;
|
|
117
|
+
export type OfxKeys = OfxKeysIntermediate<OfxResponse>;
|
|
118
|
+
export type Format<T> = {
|
|
119
|
+
[K in keyof T]: OfxKeysIntermediate<OfxResponse>;
|
|
120
|
+
};
|
|
114
121
|
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Ofx-data-extractor v1.2.1 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,N=/<\w+>/g,f="<STMTTRN>",u="</STMTTRN>";function w(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,L]=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:L}}function T(n,t){const e=w(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 S(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 E(n){return n===f?n:n.replace(/[<]/g,`
|
|
7
|
+
`).replace(/[>]/g,":{")}function x(n){return n===u?`
|
|
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(f),e=n.lastIndexOf(u)+u.length,s=n.substring(t,e),r=new RegExp(f,"g"),i=new RegExp(u,"g"),a=`"STRTTRN":[${s.replace(r,"{").replace(i,"},")}]`;return{oldListText:s,newListText:a}}function M(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 _(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())}))})}function F(n,t){let e=n;for(const s of t.split(".")){if(!Object.hasOwn(e,s))return;e=e[s]}return e}async function y(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 y(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 M(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,x).replace(N,E).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"?S(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=_(r);return{dateStart:s,dateEnd:e,...i}}customResponse(t,e){const s=new Map;for(const[r,i]of Object.entries(e))s.set(r,F(t,i));return Object.fromEntries(s)}getContent(){const t=this.getPartialJsonData(),{newListText:e,oldListText:s}=h(t),r=t.replace(s,e);return JSON.parse(`{${d(r)}}`)}toJson(t){const e=this.getHeaders(),s=this.getContent(),r={...e,...s};return t?this.customResponse(r,t):r}}o.Ofx=l,o.blobToString=y,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.
|
|
3
|
+
"version": "1.2.3",
|
|
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/
|
|
39
|
-
"module": "dist/index.
|
|
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;
|