csv-for-you
v1.0.7
Published
CSV parser. Supports all nodeJS versions.
Downloads
16
Maintainers
Readme
csv-for-you
This npm package is used by nodeJS developers for parsing CSV files into JSON objects, arrays numbers or strings. If you liked the package please star and follow me on GitHub
Installation
- Run
npm install csv-for-you
. - Fork the git repository
https://github.com/Minka1902/csv-for-you.git
, and place it next to your project.
Usage
- In your entry point, import parseCsv from the package:
const parseCsv = require('csv-for-you');
- Define your function as follows:
const csv = require('csv-for-you');
const defaultOptions = {
arraySeparator: ';',
objectSeparator: ';',
lineAsArray: true,
fileAsArray: true,
returnAsString: []
};
const options2 = {
arraySeparator: '|',
objectSeparator: '^',
lineAsArray: false,
fileAsArray: true,
returnAsString: ['name', 'ID']
};
async function myFunction() {
const myCsvFileData = await csv.parse('C:\\path\\to\\my\\file.csv', defaultOptions );
const otherCsvFileData = await csv.parse('C:\\path\\to\\other\\file.csv', options2 );
// Use the data however you'd like
};
const myOtherFunction = async () => {
const myCsvFileData = await csv.parse('C:\\path\\to\\my\\file.csv', defaultOptions );
const otherCsvFileData = await csv.parse('C:\\path\\to\\other\\file.csv', options2 );
// Use the data however you'd like
};
csv.addRow('C:\\path\\to\\my\\file.csv', { name: "john smith" } );
csv.addRow('C:\\path\\to\\other\\file.csv', { name: "john smith" }, { lineNumber: 777 } );
Options
This object contains the options for the CSV parser:
- arraySeparator - the Char that represents the separator between Array elements (
;
by default) - objectSeparator - the Char that represents the separator between Object elements (
;
by default) - lineAsArray - Boolean that represents rather a line should be represented as an Array or Object (
true
by default) - fileAsArray - Boolean that represents rather the file should be represented as an Array or Object (
true
by default) - returnAsString - Array of property names that should be returned as a string (empty by default)
Features
- Parses strings in CSV
- Parses numbers in CSV
- Parses arrays - numbers, strings, arrays and objects
- Parses objects - numbers, strings, arrays and objects
- Add data with the
addRow
function
CSV file format
- Properties - The first line of the file must be the properties of the objects
- Numbers - Any integer or float number
- Strings - Strings of any length
- Arrays - Must start with
[
and end with]
while the separator is not,
(arraySeparator in the options object to change) - Objects - Must start with
{
and end with}
while the separator is not,
(objectSeparator in the options object to change) - Values are separated by
,
and nothing else! - No need for whitespace after a coma - it might create problems
Future features
- Usage of callbacks for file/line/type of value
- Parsing text to JSON
- Fetching data from servers using URL
- Reading file structure starting from a folder
- Creating a CSV file from JSON object
- Error notifier - Lets you know what is the problem
- Generating numeric data to CSV or JSON
- Generating lingual data to CSV or JSON
Issues and Requests
For issues or feature requests go to https://github.com/Minka1902/csv-for-you/issues and add a new one. In the title write Request/Issue and elaborate in the description. I promise that i'll try my best to do everything.