1.0.1 • Published 5 years ago
csv-wizard v1.0.1
csv-wizard
Handling CSV on Node.js
Installation
npm install csv-wizard
Methods
.ExtIsCSV(path[, callback])
Check whether The extension of given path is csv
or not.
const csvWizard = require('csv-wizard');
const path = './test/test.csv';
csvWizard.ExtIsCSV(path); // true
csvWizard.ExtIsCSV(path, (error, data) => {
if(error) console.error(error);
else console.log(data) // true
});
Parameter
- (String)
path
: The path to check. - (Function)
callback
: If you want to get result through callback, Use it.
Result
Synchronous
true
or false
.
Asynchronous
error
: It is exist if there is error.- (Boolean)
data
:true
orfalse
.
.ExtIsCSVPromise(path)
Check whether The extension of given path is csv
or not.
const csvWizard = require('csv-wizard');
const path = './test/test.csv';
const file = fs.readFileSync(path, 'utf-8');
csvWizard.ExtIsCSVPromise(file).then((data) => (
console.log(data) // true
)).catch(error) => (
console.error(error)
));
Parameter
- (String)
path
: The path to check.
Result
Promise<Boolean>
.ToArray(path[, callback])
Convert CSV to Array.
const csvWizard = require('csv-wizard');
const path = './test/test.csv';
const file = fs.readFileSync(path, 'utf-8');
csvWizard.ToArray(file) // [['A', 'B', 'C'], [1, 2, 3]...]
csvWizard.ToArray(file, (error, data) => {
if(error) console.error(error);
else console.log(data) // [['A', 'B', 'C'], [1, 2, 3]...]
});
Parameter
- (String)
path
: The path to convert. - (Function)
callback
: If you want to get result through callback, Use it.
Result
Synchronous
Array
.
[
['Cell', 'Cell 2'...more cells]
...more rows
]
Asynchronous
error
: It is exist if there is error.- (Boolean)
data
:Array
.
.ToArrayPromise(path)
Convert CSV to Array.
const csvWizard = require('csv-wizard');
const path = './test/test.csv';
const file = fs.readFileSync(path, 'utf-8');
csvWizard.ToArrayPromise(file).then((data) => (
console.log(data) // [['A', 'B', 'C'], [1, 2, 3]...]
)).catch(error) => (
console.error(error)
));
Parameter
- (String)
path
: The path to convert.
Result
Promise<Array>
.ToObject(path[, callback])
Convert CSV to Array of Object.
const csvWizard = require('csv-wizard');
const path = './test/test.csv';
const file = fs.readFileSync(path, 'utf-8');
csvWizard.ToObject(file) // [['A', 'B', 'C'], [1, 2, 3]...]
csvWizard.ToObject(file, (error, data) => {
if(error) console.error(error);
else console.log(data) // [['A', 'B', 'C'], [1, 2, 3]...]
});
Parameter
- (String)
path
: The path to convert. - (Function)
callback
: If you want to get result through callback, Use it.
Result
Synchronous
Array
.
[
{
A: 1,
B: 2,
C: 3
}
...more rows
]
Asynchronous
error
: It is exist if there is error.- (Boolean)
data
:Array
.
.ToObjectPromise(path)
Convert CSV to Array of Object.
const csvWizard = require('csv-wizard');
const path = './test/test.csv';
const file = fs.readFileSync(path, 'utf-8');
csvWizard.ToObjectPromise(file).then((data) => (
console.log(data) // [{ A: 1, B: 2, C: 3 }...]
)).catch(error) => (
console.error(error)
));
Parameter
- (String)
path
: The path to convert.
Result
Promise<Array>
.ToCSV(path[, callback])
Convert Arrayof Object to CSV.
const csvWizard = require('csv-wizard');
const array = [['A', 'B', 'C'], [1, 2, 3], [4, 5, 6]];
csvWizard.ToCSV(array) // A,B,C 1,2,3 4,5,6
csvWizard.ToCSV(array, (error, data) => {
if(error) console.error(error);
else console.log(data) // A,B,C 1,2,3 4,5,6
});
Parameter
- (String)
path
: The Array to convert. - (Function)
callback
: If you want to get result through callback, Use it.
Result
Synchronous
String(CSV)
.
A,B,C
1,2,3
4,5,6
Asynchronous
error
: It is exist if there is error.- (Boolean)
data
:String(CSV)
.
.ToCSVPromise(path)
Convert Arrayof Object to CSV.
const csvWizard = require('csv-wizard');
const array = [['A', 'B', 'C'], [1, 2, 3], [4, 5, 6]];
csvWizard.ToCSV(array).then((data) => (
console.log(data) // A,B,C 1,2,3 4,5,6
)).catch(error) => (
console.error(error)
));
Parameter
- (String)
Array
: The Array to convert.
Result
Promise<String(CSV)>
🚩 Author
Donghoon Yoo Developer
- Email: yoodonghoon01@gmail.com
- Blog: blog.donghoonyoo.com
- Roles: Development, Writing document.