CSV parser. Supports all nodeJS versions.
npm install csv-for-youbash
For use in your project
npm install csv-for-you
For global CLI access
npm install -g csv-for-you
`
---
๐ Quick Start
$3
`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 }]
`
$3
`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
`
$3
`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
$3
- CSV Operations
- Parsing
- Adding Rows
- Editing Rows
- Deleting Rows
- Validation
- File Operations
- Advanced Features
- CLI Commands
- Configuration
---
CSV Operations
$3
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()
});
`
---
$3
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
);
`
---
$3
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'
}
});
`
---
$3
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
});
`
---
$3
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.
$3
`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');
`
$3
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
$3
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);
`
$3
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();
`
$3
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!
$3
`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"
`
$3
`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
`
$3
`bash
Batch operations
csvfy batch ./batch-config.json
Watch for changes
csvfy watch ./data.csv
csvfy watch ./src --command "npm test"
`
$3
`bash
General help
csvfy --help
Command-specific help
csvfy parse --help
csvfy validate --help
`
---
๐ก Examples
$3
`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()
});
}
}
`
$3
`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});
}
}
});
`
$3
`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
$3
See all features in action:
`bash
node demo.js
`
This interactive demo showcases all 21 functions with real examples!
$3
`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');
``