@f-o-t/ofx 2.0.0 → 2.1.0

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.
Files changed (2) hide show
  1. package/README.md +67 -3
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -11,11 +11,17 @@ bun add @fot/ofx
11
11
  ## Quick Start
12
12
 
13
13
  ```typescript
14
- import { parse, getTransactions, getBalance } from "@fot/ofx";
14
+ import { parse, parseBuffer, getTransactions, getBalance } from "@fot/ofx";
15
+ import { readFileSync } from "node:fs";
15
16
 
16
- const ofxContent = fs.readFileSync("statement.ofx", "utf-8");
17
+ // For files with known UTF-8 encoding
18
+ const ofxContent = readFileSync("statement.ofx", "utf-8");
17
19
  const result = parse(ofxContent);
18
20
 
21
+ // For files with unknown encoding (recommended for Brazilian banks)
22
+ const buffer = readFileSync("statement.ofx");
23
+ const result = parseBuffer(new Uint8Array(buffer));
24
+
19
25
  if (result.success) {
20
26
  const transactions = getTransactions(result.data);
21
27
  const balances = getBalance(result.data);
@@ -56,6 +62,31 @@ try {
56
62
  }
57
63
  ```
58
64
 
65
+ #### `parseBuffer(buffer: Uint8Array): ParseResult<OFXDocument>`
66
+
67
+ Parses an OFX file from binary data with automatic encoding detection. This is the recommended method for files from Brazilian banks or any file with non-UTF-8 encoding.
68
+
69
+ ```typescript
70
+ import { readFileSync } from "node:fs";
71
+
72
+ const buffer = readFileSync("extrato.ofx");
73
+ const result = parseBuffer(new Uint8Array(buffer));
74
+
75
+ if (result.success) {
76
+ // Portuguese characters like "Cartão" are correctly preserved
77
+ console.log(result.data);
78
+ }
79
+ ```
80
+
81
+ #### `parseBufferOrThrow(buffer: Uint8Array): OFXDocument`
82
+
83
+ Parses an OFX file from binary data and throws on validation errors.
84
+
85
+ ```typescript
86
+ const buffer = readFileSync("extrato.ofx");
87
+ const doc = parseBufferOrThrow(new Uint8Array(buffer));
88
+ ```
89
+
59
90
  ### Extraction Functions
60
91
 
61
92
  #### `getTransactions(document: OFXDocument): OFXTransaction[]`
@@ -229,6 +260,39 @@ console.log("Accounts:", result.accounts);
229
260
  console.log("Balances:", result.balances);
230
261
  ```
231
262
 
263
+ ## Encoding Support
264
+
265
+ The library automatically detects and handles various character encodings commonly used in OFX files, especially from Brazilian banks.
266
+
267
+ ### Supported Charsets
268
+
269
+ | OFX CHARSET Value | Encoding Used |
270
+ | ----------------- | ------------- |
271
+ | `1252`, `WINDOWS-1252`, `CP1252` | windows-1252 |
272
+ | `8859-1`, `ISO-8859-1`, `LATIN1`, `LATIN-1` | iso-8859-1 |
273
+ | `UTF-8`, `UTF8`, `NONE`, (empty) | utf-8 |
274
+
275
+ ### UTF-8 Auto-Detection
276
+
277
+ Some OFX files declare `CHARSET:1252` but are actually encoded as UTF-8. The library automatically detects this and uses UTF-8 when appropriate, ensuring characters like `Transação` are correctly preserved.
278
+
279
+ ### Best Practices
280
+
281
+ For files from Brazilian banks or any file with unknown encoding, use `parseBuffer()` instead of `parse()`:
282
+
283
+ ```typescript
284
+ import { parseBuffer } from "@fot/ofx";
285
+ import { readFileSync } from "node:fs";
286
+
287
+ // Correct: Read as binary, let the library detect encoding
288
+ const buffer = readFileSync("extrato.ofx");
289
+ const result = parseBuffer(new Uint8Array(buffer));
290
+
291
+ // Avoid: Reading as UTF-8 string may corrupt Windows-1252 characters
292
+ // const content = readFileSync("extrato.ofx", "utf-8");
293
+ // const result = parse(content);
294
+ ```
295
+
232
296
  ## Types
233
297
 
234
298
  ### OFXTransaction
@@ -238,7 +302,7 @@ interface OFXTransaction {
238
302
  TRNTYPE: OFXTransactionType;
239
303
  DTPOSTED: OFXDate;
240
304
  TRNAMT: number;
241
- FITID: string;
305
+ FITID?: string; // Optional, auto-generated if missing
242
306
  NAME?: string;
243
307
  MEMO?: string;
244
308
  CHECKNUM?: string;
package/package.json CHANGED
@@ -60,5 +60,5 @@
60
60
  },
61
61
  "type": "module",
62
62
  "types": "./dist/index.d.ts",
63
- "version": "2.0.0"
63
+ "version": "2.1.0"
64
64
  }