New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ofx-data-extractor

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ofx-data-extractor - npm Package Compare versions

Comparing version 1.0.8 to 1.2.0

13

dist/index.js

@@ -1,9 +0,10 @@

"use strict";const p=/<\/\w+>/g,h=/<\w+>/g,l="<STMTTRN>",o="</STMTTRN>";function m(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),c=n.slice(12,14),[a,g]=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:c,s:c,O:a,TZ:g}}function u(n,t){const e=m(n);let s=t;for(const[r,i]of Object.entries(e))s=s.replace(r,i);return s}function f(n){const t=n.replace(/(},})/g,"}}").replace(/(}")/g,'},"').replace(/(},])/g,"}]").replace(/(,})/g,"}").replace(/({")/g,`{
"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,`{
"`).replace(/(})/g,`
}`).replace(/(",")/g,`",
"`).replace(/,\s*}/g,`
}`);return t.at(-1)===","?t.slice(0,-1):t}function N(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 R(n){return n.startsWith("DT")}function x(n){return n.trim()}function D(n){return n===l?n:n.replace("<","").replace(">",":{")}function S(n){return n===o?n:"},"}function d(n,t){return n.endsWith("ID")||n.endsWith("NUM")?!1:!isNaN(Number(t))}function T(n){const t=n.indexOf(l),e=n.lastIndexOf(o)+o.length,s=n.substring(t,e),r=new RegExp(l,"g"),i=new RegExp(o,"g"),c=`"STRTTRN":[${s.replace(r,"{").replace(i,"},")}]`;return{oldListText:s,newListText:c}}function E(n,t){const e={};for(const s of n){const[r,i]=s.split(":"),c=r.replace(`
`,"");e[c]=t&&d(r,i)?Number(i):i}return e}function O(n){return n.reduce((t,e)=>e.TRNTYPE.toLocaleLowerCase().startsWith("deb")?(t.amountOfDebits++,t.debit+=Number(e.TRNAMT.replace("-","")),t):(t.amountOfCredits++,t.debit+=Number(e.TRNAMT),t),{credit:0,debit:0,amountOfCredits:0,amountOfDebits:0})}class y{data;config={};constructor(t,e){e&&(this.config=e),t instanceof Buffer?this.data=t.toString():this.data=t}getHeaders(){const[t]=this.data.split("<OFX>"),e=t.split(`
`),s=i=>!!i.trim().length,r=e.filter(s);return E(r,!!this.config.nativeTypes)}getBankTransferList(){const{newListText:t}=T(this.getPartialJsonData()),e=t.slice(10),s=f(e);return JSON.parse(s)}getPartialJsonData(){const[t,e]=this.data.split("<OFX>"),s="<OFX>"+e,{sanitize:r}=this;return s.replace(p,S).replace(h,D).split(`
`).map(x).filter(Boolean).map(r,this).join("")}sanitizeValue(t,e){let s=e.replace(/[{},]/g,"");if(R(t))s=this.configDate(s);else{if(t==="FITID")return this.configFinancialInstitutionTransactionId(s);if(this.config.nativeTypes&&d(t,s))return`${s},`}return`"${s}",`}sanitize(t){let e=t;const s=e.slice(0,e.indexOf(":")),r=this.sanitizeValue,i=a=>r.call(this,s,a);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"?N(t):`"${t}",`}configDate(t){const{formatDate:e}=this.config;return e?u(t,e):u(t,"y-M-d")}getTransactionsSummary(){const t=this.getContent(),{DTEND:e,DTSTART:s,STRTTRN:r}=t.OFX.BANKMSGSRSV1.STMTTRNRS.STMTRS.BANKTRANLIST,i=O(r);return{dateStart:s,dateEnd:e,...i}}getContent(){const t=this.getPartialJsonData(),{newListText:e,oldListText:s}=T(t),r=t.replace(s,e);return JSON.parse(`{${f(r)}}`)}toJson(){const t=this.getHeaders(),e=this.getContent();return{...t,...e}}}exports.Ofx=y;exports.fixJsonProblems=f;exports.formatDate=u;
//# sourceMappingURL=index.js.map
}`).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,`
`).replace(/[>]/g,":{")}function A(n){return n===c?`
${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(`
`,"");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(`
`),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(`
`).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;
{
"name": "ofx-data-extractor",
"version": "1.0.8",
"version": "1.2.0",
"keywords": [

@@ -41,3 +41,4 @@ "ofx",

"scripts": {
"test": "node tests/index.js",
"test": "jest tests/*.spec.ts --config jest.config.js",
"test:coverage": "jest tests/*.spec.ts --coverage --config jest.config.js",
"build": "rollup --config",

@@ -49,2 +50,4 @@ "start": "node dist/index.js",

"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-typescript": "^10.0.1",

@@ -51,0 +54,0 @@ "@types/jest": "^29.2.3",

@@ -1,4 +0,8 @@

# ofx-data-extractor
# [ofx-data-extractor](https://www.npmjs.com/package/ofx-data-extractor)
[![npm version](https://badge.fury.io/js/ofx-data-extractor.svg)](https://badge.fury.io/js/ofx-data-extractor) [![MIT License][license-image]][license-url]
[![codecov](https://codecov.io/gh/Fabiopf02/ofx-data-extractor/branch/main/graph/badge.svg?token=L4A7E4H8IN)](https://codecov.io/gh/Fabiopf02/ofx-data-extractor)
## Ofx Data Extractor and Formatter
This is a `Node.js` module written in `TypeScript` that provides a utility for extracting data from an `OFX` file. The module can also format some of the data and retrieve specific parts of the file if requested.
This is a `Node.js` and `Browser` module written in `TypeScript` that provides a utility for extracting data from an `OFX` file. The module can also format some of the data and retrieve specific parts of the file if requested.

@@ -8,25 +12,44 @@ ### Installation

**npm**
```bash
npm install ofx-ts
npm install ofx-data-extractor
```
Usage
**yarn**
```bash
yarn add ofx-data-extractor
```
### Methods
The `Ofx` class provides the following methods:
- `async fromBuffer(data: Buffer)`: Used to read files on the node. Returns the methods
- `async fromFilePath(path: string)`: Used to read files on the node. Returns the methods below.
- `fromBlob(data: Blob)`: Used to read files in the browser. Returns the methods below.
- `config(options: OfxConfig)`: Used for formatting the generated json.`
- `getHeaders(): OFXMetaData`: Returns the metadata section of the OFX file as an object.
- `getBankTransferList(): Pick<BankTransferList, 'STRTTRN'>`: Returns a list of bank transfer transactions as an object.
- `getTransactionsSummary()`: Object: Returns a summary of transactions for a bank statement as an object.
- `getContent(): OfxStructure`: Returns the OFX file content as an object.
- `toJson(): OfxResponse`: Returns the entire OFX file content as a JSON object.
### Usage
The module provides a class called Ofx that can be used to extract and format data from an `OFX` file. Here's an example of how to use it:
```typescript
import { Ofx } from 'ofx-data-extractor';
import { Ofx } from 'ofx-data-extractor'
const data = 'OFXHEADER:100\nDATA:OFXSGML\nVERSION:102\n...';
const ofx = new Ofx(data);
const data = 'OFXHEADER:100\nDATA:OFXSGML\nVERSION:102\n...'
const ofx = new Ofx(data) // works in node.js and browser
const headers = ofx.getHeaders();
console.log(headers);
const headers = ofx.getHeaders()
console.log(headers)
const transactionsSummary = ofx.getTransactionsSummary();
console.log(transactionsSummary);
const transactionsSummary = ofx.getTransactionsSummary()
console.log(transactionsSummary)
const bankTransferList = ofx.getBankTransferList();
console.log(bankTransferList);
const bankTransferList = ofx.getBankTransferList()
console.log(bankTransferList)
const ofxResponse = ofx.toJson();
console.log(ofxResponse);
const ofxResponse = ofx.toJson()
console.log(ofxResponse)
```

@@ -38,13 +61,34 @@ ### Constructor

`config`: Optional configuration options for formatting the OFX data.
### Methods
The `Ofx` class provides the following methods:
- `getHeaders(): OFXMetaData`: Returns the metadata section of the OFX file as an object.
- `getBankTransferList(): Pick<BankTransferList, 'STRTTRN'>`: Returns a list of bank transfer transactions as an object.
- `getTransactionsSummary()`: Object: Returns a summary of transactions for a bank statement as an object.
- `getContent(): OfxStructure`: Returns the OFX file content as an object.
- `toJson(): OfxResponse`: Returns the entire OFX file content as a JSON object.
### Read data - static methods (`Node.js`)
```typescript
import { Ofx } from 'ofx-data-extractor'
import fs from 'fs'
const file = await fs.readFile('/path/to/file')
const ofx = await Ofx.fromBuffer(file)
// OR
const ofx = await Ofx.fromFilePath('/path/to/file')
const ofxResponse = ofx.toJson()
console.log(ofxResponse)
```
### Read data - static methods (`Browser`)
```typescript
import { Ofx } from 'ofx-data-extractor'
function handleFile(event) {
const ofx = Ofx.fromBlob(event.target.files[0])
const ofxResponse = ofx.toJson()
console.log(ofxResponse)
}
// tsx/jsx/html
<input type="file" onChange={handleFile} />
```
### Configuration
The `Ofx` class can be configured with the following options:
The `Ofx` (`constructor` and `config` method) class can be configured with the following options:

@@ -54,1 +98,4 @@ - `formatDate`: A function that takes in a date string and returns a formatted date string.

- `nativeTypes`: A boolean value that determines whether numeric fields should be represented as numbers or strings in the resulting JSON object.
[license-image]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat
[license-url]: LICENSE
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc