csv-for-you
Version:
CSV parser. Supports all nodeJS versions.
598 lines (436 loc) โข 12.6 kB
Markdown
<div align="center">
# ๐ csv-for-you
### A powerful, developer-friendly CSV toolkit for Node.js
[](https://www.npmjs.com/package/csv-for-you)
[](https://opensource.org/licenses/ISC)
**Parse โข Validate โข Transform โข Automate**
[Quick Start](#quick-start) โข [Documentation](#documentation) โข [CLI Guide](#๏ธcli-commands) โข [Examples](#examples)
</div>
## ๐ฏ What is csv-for-you?
A complete CSV solution that goes beyond parsing. Whether you need to read, validate, transform, or automate CSV workflows, csv-for-you has you covered with both a powerful JavaScript API and an intuitive CLI.
### Why csv-for-you?
- ๐ **Simple API** - Get started in seconds
- ๐ช **TypeScript** - Full type definitions included
- ๐จ **Flexible** - Parse nested arrays & objects
- โ
**Validation** - Built-in CSV structure checking
- ๐ **Automation** - Batch operations & file watching
- ๐ฅ๏ธ **CLI** - All features from command line
- ๐ฆ **Zero Config** - Works out of the box
## ๐ฆ Installation
```bash
# For use in your project
npm install csv-for-you
# For global CLI access
npm install -g csv-for-you
```
## ๐ Quick Start
### Your First Parse
```javascript
const csv = require('csv-for-you');
// Parse CSV to JSON
const data = await csv.parse('./employees.csv');
console.log(data);
// [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]
```
### Common Tasks
```javascript
// โ๏ธ Add a row
await csv.addRow('./data.csv', { name: 'Charlie', age: 35 });
// ๐ Validate structure
const result = await csv.validateCSV('./data.csv');
if (result.valid) console.log('โ CSV is valid!');
// ๐ Edit existing data
csv.editRow('./data.csv', {
lineNumber: 2,
data: { name: 'Bob', age: 26 }
});
// ๐๏ธ Delete rows
csv.deleteRows('./data.csv', { rowNumber: -1 }); // Delete last row
```
### CLI Quick Start
```bash
# Parse and display
csvfy parse ./data.csv
# Add a row
csvfy add ./data.csv --data '{"name":"Diana","age":28}'
# Validate
csvfy validate ./data.csv --check-types
# Show help
csvfy --help
```
## ๐ Documentation
### Table of Contents
- [CSV Operations](#csv-operations)
- [Parsing](#parsing)
- [Adding Rows](#adding-rows)
- [Editing Rows](#๏ธediting-rows)
- [Deleting Rows](#๏ธdeleting-rows)
- [Validation](#validation)
- [File Operations](#file-operations)
- [Advanced Features](#advanced-features)
- [CLI Commands](#๏ธcli-commands)
- [Configuration](#๏ธconfiguration)
## CSV Operations
### ๐ Parsing
Convert CSV files to JSON with powerful customization options.
#### Basic Parsing
```javascript
const data = await csv.parse('./data.csv');
```
#### Advanced Parsing
```javascript
const data = await csv.parse('./data.csv', {
arraySeparator: ';', // How array items are separated
objectSeparator: ';', // How object properties are separated
lineAsArray: false, // Return objects instead of arrays
fileAsArray: true, // Return array of items
returnAsString: ['id'] // Keep these fields as strings
});
```
#### Parsing Nested Data
CSV with nested arrays and objects:
```csv
name,scores,metadata
Alice,[85;90;95],{project:A;grade:excellent}
Bob,[75;80;88],{project:B;grade:good}
```
Parse it:
```javascript
const data = await csv.parse('./scores.csv', {
arraySeparator: ';',
objectSeparator: ';'
});
console.log(data);
// [
// {
// name: 'Alice',
// scores: [85, 90, 95],
// metadata: { project: 'A', grade: 'excellent' }
// },
// {
// name: 'Bob',
// scores: [75, 80, 88],
// metadata: { project: 'B', grade: 'good' }
// }
// ]
```
#### Using Callbacks
Transform data during parsing:
```javascript
const data = await csv.parse('./data.csv', {}, {
lineCallback: (line) => {
console.log('Processing line:', line);
return line;
},
numberCallback: (num) => Math.round(num),
stringCallback: (str) => str.trim().toUpperCase()
});
```
### โ Adding Rows
Add new data to your CSV files.
#### Append to End
```javascript
await csv.addRow('./data.csv', {
name: 'Eve',
age: 32,
department: 'Sales'
});
```
#### Insert at Specific Line
```javascript
await csv.addRow('./data.csv',
{ name: 'Frank', age: 29 },
{ lineNumber: 2 } // Insert at line 2
);
```
### โ๏ธ Editing Rows
Modify existing rows in place.
```javascript
csv.editRow('./data.csv', {
lineNumber: 3, // Which row to edit (1-based)
data: {
name: 'Alice Smith',
age: 31,
department: 'Engineering'
}
});
```
### ๐๏ธ Deleting Rows
Remove rows from your CSV.
```javascript
// Delete last row
csv.deleteRows('./data.csv', { rowNumber: -1 });
// Delete specific row
csv.deleteRows('./data.csv', { rowNumber: 5 });
// Delete multiple rows
csv.deleteRows('./data.csv', {
rowNumber: 3,
rowsToDelete: 2 // Delete 2 rows starting at row 3
});
```
### โ
Validation
Ensure your CSV files are properly formatted.
#### Basic Validation
```javascript
const result = await csv.validateCSV('./data.csv');
if (result.valid) {
console.log('โ CSV is valid!');
} else {
console.log('โ Errors:', result.errors);
}
console.log('Stats:', result.stats);
// { totalRows: 10, totalColumns: 4, headers: [...] }
```
#### Advanced Validation
```javascript
const result = await csv.validateCSV('./data.csv', {
checkHeaders: true, // Verify headers exist
checkTypes: true, // Check type consistency
checkColumns: true, // Validate column counts
expectedHeaders: ['name', 'age'] // Required headers
});
// Check results
console.log('Valid:', result.valid);
console.log('Errors:', result.errors);
console.log('Warnings:', result.warnings);
```
## File Operations
Beyond CSV - powerful file management tools.
### ๐ File Management
```javascript
// Create a file
await csv.createFile('./new.csv', {
content: 'name,age\nAlice,30',
overwrite: false
});
// Read file contents
const content = await csv.readFile('./data.csv');
// Append data
await csv.appendToFile('./data.csv', '\nBob,25');
// Copy file
await csv.copyFile('./data.csv', './backup.csv');
// Move file
await csv.moveFile('./old.csv', './new.csv');
// Rename file
await csv.renameFile('./old-name.csv', './new-name.csv');
// Delete file
await csv.deleteFile('./temp.csv');
```
### ๐ณ Directory Tree
Visualize your directory structure:
```javascript
const tree = await csv.getFileTree('.', {
depth: 3,
showHidden: false
});
console.log(tree);
// โโโ ๐ src
// โ โโโ ๐ index.js
// โ โโโ ๐ utils.js
// โโโ ๐ package.json
```
## Advanced Features
### ๐ Batch Operations
Execute multiple operations from a single configuration.
**Create a config file** (`batch-config.json`):
```json
{
"operations": [
{
"type": "createFile",
"file": "./output.csv",
"options": { "content": "name,age\n", "overwrite": true }
},
{
"type": "addRow",
"file": "./output.csv",
"data": { "name": "Alice", "age": 30 }
},
{
"type": "validate",
"file": "./output.csv"
}
],
"stopOnError": true
}
```
**Run it**:
```javascript
const result = await csv.batchOperations('./batch-config.json');
console.log(`โ ${result.success.length} operations succeeded`);
console.log(`โ ${result.failed.length} operations failed`);
```
### ๐ File Watching
Automatically react to file changes.
```javascript
const watcher = csv.watchFile('./data.csv', {
onChange: (filePath, eventType) => {
console.log(`File changed: ${filePath}`);
// Re-parse or process the file
},
debounce: 100
});
// Stop watching when done
watcher.stop();
```
### โ๏ธ Configuration Files
Create a `.csvfyrc.json` in your project:
```json
{
"arraySeparator": "|",
"objectSeparator": "^",
"depth": 5,
"lineAsArray": false
}
```
Load and use it:
```javascript
const config = csv.loadConfig();
const merged = csv.mergeConfig(config, { depth: 10 });
```
## ๐ฅ๏ธ CLI Commands
All features available from the command line!
### CSV Commands
```bash
# Parse CSV to JSON
csvfy parse ./data.csv
csvfy parse ./data.csv --compact
# Add a row
csvfy add ./data.csv --data '{"name":"Alice","age":30}'
# Edit a row
csvfy edit ./data.csv --line 2 --data '{"name":"Bob","age":26}'
# Delete rows
csvfy delete ./data.csv --row -1
csvfy delete ./data.csv --row 3 --count 2
# Validate
csvfy validate ./data.csv
csvfy validate ./data.csv --check-types --expected "name,age,email"
```
### File Commands
```bash
# Create file
csvfy create ./new.csv --content "name,age"
# Read file
csvfy read ./data.csv
# Append
csvfy append ./data.csv --content "\nCharlie,35"
# Copy, move, rename
csvfy copy ./data.csv ./backup.csv
csvfy move ./old.csv ./new.csv
csvfy rename ./old.csv ./new.csv
# Delete
csvfy remove ./temp.csv
# Directory tree
csvfy tree .
csvfy tree ./src --depth 2 --show-hidden
```
### Advanced Commands
```bash
# Batch operations
csvfy batch ./batch-config.json
# Watch for changes
csvfy watch ./data.csv
csvfy watch ./src --command "npm test"
```
### Getting Help
```bash
# General help
csvfy --help
# Command-specific help
csvfy parse --help
csvfy validate --help
```
## ๐ก Examples
### Example 1: Data Processing Pipeline
```javascript
// Read โ Validate โ Transform โ Save
const data = await csv.parse('./input.csv');
const validation = await csv.validateCSV('./input.csv', {
checkTypes: true,
expectedHeaders: ['name', 'email', 'age']
});
if (validation.valid) {
// Add processed timestamp
for (const row of data) {
await csv.addRow('./output.csv', {
...row,
processedAt: new Date().toISOString()
});
}
}
```
### Example 2: Automated Backup System
```javascript
const watcher = csv.watchDirectory('./data', {
onChange: async (filePath) => {
if (filePath.endsWith('.csv')) {
const backupPath = filePath.replace('.csv', `-backup-${Date.now()}.csv`);
await csv.copyFile(filePath, backupPath);
console.log(`โ Backed up: ${filePath}`);
}
}
});
```
### Example 3: CSV Report Generator
```javascript
// Generate monthly report
await csv.createFile('./report.csv', {
content: 'month,sales,revenue\n',
overwrite: true
});
const months = ['Jan', 'Feb', 'Mar'];
for (const month of months) {
await csv.addRow('./report.csv', {
month,
sales: Math.floor(Math.random() * 1000),
revenue: Math.floor(Math.random() * 50000)
});
}
// Validate the report
const result = await csv.validateCSV('./report.csv');
console.log(`Report generated: ${result.stats.totalRows} rows`);
```
## ๐ Learn More
### Run the Demo
See all features in action:
```bash
node demo.js
```
This interactive demo showcases all 21 functions with real examples!
### TypeScript Support
```typescript
import { parse, validateCSV, ParseOptions, ValidationResult } from 'csv-for-you';
const options: ParseOptions = {
lineAsArray: false,
fileAsArray: true
};
const data = await parse('./data.csv', options);
const validation: ValidationResult = await validateCSV('./data.csv');
```
## ๐ License
ISC License - see LICENSE file for details
## ๐ค Contributing
Contributions are welcome! Feel free to open issues or submit pull requests.
## ๐ฎ Support
- **Issues**: [GitHub Issues](https://github.com/Minka1902/csv-for-you/issues)
- **Email**: minka.scharff@gmail.com
<div align="center">
**Made with โค๏ธ by Michael Scharff**
[โญ Star on GitHub](https://github.com/Minka1902/csv-for-you) โข [๐ฆ View on NPM](https://www.npmjs.com/package/csv-for-you)
</div>