csvparser/readme.md

258 lines
7.2 KiB
Markdown

```markdown
# @fin.cx/csvparser
a csvparser for fin2021
## Install
To install the `@fin.cx/csvparser` module, you can use npm. Simply run the following command:
```bash
npm install @fin.cx/csvparser
```
## Usage
The `@fin.cx/csvparser` module provides tools to parse CSV files from various financial institutions such as Commerzbank, Fidor Bank, PayPal, and Spendesk. This helps in extracting and managing financial transaction data efficiently.
Here's a guide to showcase various functionalities along with comprehensive TypeScript code examples.
### Getting Started
First, make sure to import the necessary components from the `@fin.cx/csvparser` module.
```typescript
import { CsvSpendesk, CsvPayPal, CsvFidor, CsvCommerzbank } from '@fin.cx/csvparser';
```
### Spendesk CSV Parsing
#### Parsing Spendesk CSV from File
The `CsvSpendesk` class provides methods to parse Spendesk CSV files.
```typescript
import { CsvSpendesk } from '@fin.cx/csvparser';
async function parseSpendeskFile(filePath: string) {
try {
const spendeskData = await CsvSpendesk.fromFile(filePath);
const transactions = await spendeskData.getTransactions();
console.log(transactions);
} catch (error) {
console.error('Error parsing Spendesk file:', error);
}
}
// Usage example
parseSpendeskFile('./path/to/spendesk.csv');
```
#### Parsing Spendesk CSV from Directory
You might have multiple Spendesk CSV files in a directory. The `fromDir` method helps in parsing all of them.
```typescript
import { CsvSpendesk } from '@fin.cx/csvparser';
async function parseSpendeskDirectory(dirPath: string) {
try {
const spendeskData = await CsvSpendesk.fromDir(dirPath);
const transactions = await spendeskData.getTransactions();
console.log(transactions);
} catch (error) {
console.error('Error parsing Spendesk directory:', error);
}
}
// Usage example
parseSpendeskDirectory('./path/to/spendesk_dir');
```
### PayPal CSV Parsing
The `CsvPayPal` class is used to parse PayPal CSV files.
```typescript
import { CsvPayPal } from '@fin.cx/csvparser';
async function parsePayPalFile(filePath: string) {
try {
const payPalData = new CsvPayPal();
const csvDescriptor = {
filename: 'paypal.csv',
contentString: await fs.promises.readFile(filePath, 'utf8')
}
payPalData.addCsvDecriptor(csvDescriptor);
const transactions = await payPalData.getTransactions();
console.log(transactions);
} catch (error) {
console.error('Error parsing PayPal file:', error);
}
}
// Usage example
parsePayPalFile('./path/to/paypal.csv');
```
### Fidor Bank CSV Parsing
The `CsvFidor` class is used to parse Fidor Bank CSV files.
```typescript
import { CsvFidor } from '@fin.cx/csvparser';
async function parseFidorFile(filePath: string) {
try {
const fidorData = new CsvFidor();
const csvDescriptor = {
filename: 'fidor.csv',
contentString: await fs.promises.readFile(filePath, 'utf8')
}
fidorData.addCsvDecriptor(csvDescriptor);
const transactions = await fidorData.getTransactions();
console.log(transactions);
} catch (error) {
console.error('Error parsing Fidor file:', error);
}
}
// Usage example
parseFidorFile('./path/to/fidor.csv');
```
### Commerzbank CSV Parsing
The `CsvCommerzbank` class is used to parse Commerzbank CSV files.
```typescript
import { CsvCommerzbank } from '@fin.cx/csvparser';
async function parseCommerzbankFile(filePath: string) {
try {
const commerzbankData = new CsvCommerzbank();
const csvDescriptor = {
filename: 'commerzbank.csv',
contentString: await fs.promises.readFile(filePath, 'utf8')
}
commerzbankData.addCsvDecriptor(csvDescriptor);
const transactions = await commerzbankData.getTransactions();
console.log(transactions);
} catch (error) {
console.error('Error parsing Commerzbank file:', error);
}
}
// Usage example
parseCommerzbankFile('./path/to/commerzbank.csv');
```
### Advanced Usage
#### Merging Spendesk Data
You may need to merge multiple Spendesk CSV data instances.
```typescript
import { CsvSpendesk } from '@fin.cx/csvparser';
async function mergeSpendeskData(filePaths: string[]) {
try {
const spendeskInstances = await Promise.all(filePaths.map(CsvSpendesk.fromFile));
const mergedInstance = spendeskInstances.reduce(
(merged, currentInstance) => merged.concat(currentInstance),
spendeskInstances[0]
);
console.log(await mergedInstance.getTransactions());
} catch (error) {
console.error('Error merging Spendesk data:', error);
}
}
// Usage example
mergeSpendeskData([
'./path/to/spendesk1.csv',
'./path/to/spendesk2.csv'
]);
```
#### Working with PayPal Linked Transactions
PayPal CSV data contains linked transactions, which you may need to handle specifically.
```typescript
import { CsvPayPal } from '@fin.cx/csvparser';
async function handlePayPalLinkedTransactions(filePath: string) {
try {
const payPalData = new CsvPayPal();
const csvDescriptor = {
filename: 'paypal.csv',
contentString: await fs.promises.readFile(filePath, 'utf8')
}
payPalData.addCsvDecriptor(csvDescriptor);
const transactions = await payPalData.getTransactions();
const linkedTransactions = transactions.filter(tx => tx.linkedTransactionCode);
console.log(linkedTransactions);
} catch (error) {
console.error('Error handling linked transactions:', error);
}
}
// Usage example
handlePayPalLinkedTransactions('./path/to/paypal.csv');
```
#### Parsing Multiple File Types
You may need to parse CSV files from different financial service providers.
```typescript
import { CsvSpendesk, CsvPayPal, CsvFidor, CsvCommerzbank } from '@fin.cx/csvparser';
async function parseMultipleFiles(filePaths: {spendesk: string, paypal: string, fidor: string, commerzbank: string}) {
try {
const spendeskData = await CsvSpendesk.fromFile(filePaths.spendesk);
const payPalData = new CsvPayPal();
const csvDescriptorPayPal = {
filename: 'paypal.csv',
contentString: await fs.promises.readFile(filePaths.paypal, 'utf8')
}
payPalData.addCsvDecriptor(csvDescriptorPayPal);
const fidorData = new CsvFidor();
const csvDescriptorFidor = {
filename: 'fidor.csv',
contentString: await fs.promises.readFile(filePaths.fidor, 'utf8')
}
fidorData.addCsvDecriptor(csvDescriptorFidor);
const commerzbankData = new CsvCommerzbank();
const csvDescriptorCommerzbank = {
filename: 'commerzbank.csv',
contentString: await fs.promises.readFile(filePaths.commerzbank, 'utf8')
}
commerzbankData.addCsvDecriptor(csvDescriptorCommerzbank);
console.log(await spendeskData.getTransactions());
console.log(await payPalData.getTransactions());
console.log(await fidorData.getTransactions());
console.log(await commerzbankData.getTransactions());
} catch (error) {
console.error('Error parsing multiple files:', error);
}
}
// Usage example
parseMultipleFiles({
spendesk: './path/to/spendesk.csv',
paypal: './path/to/paypal.csv',
fidor: './path/to/fidor.csv',
commerzbank: './path/to/commerzbank.csv'
});
```
By covering multiple use cases and providing comprehensive documentation and code samples, this readme should help developers integrate and utilize the `@fin.cx/csvparser` module effectively.
```
undefined