diff --git a/index.js b/index.js index 1344f31..5e6280d 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,12 @@ const { readFile } = require('./utils/converter') const add = async (path) => { - return await readFile(`${path}`) + try { + if (typeof path !== 'string') throw new Error('path have to be an string') + return await readFile(`${path}`) + } catch (e) { + throw new Error(e.message) + } } module.exports = add diff --git a/package.json b/package.json index 34409f7..d37f464 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "async-convert-csv-to-json", - "version": "1.0.1", + "version": "1.1.1", "description": "convert csv file to json", "main": "index.js", "scripts": { diff --git a/utils/MOCK_DATA.csv b/utils/MOCK_DATA.csv new file mode 100644 index 0000000..e89b9f6 --- /dev/null +++ b/utils/MOCK_DATA.csv @@ -0,0 +1,11 @@ +id,first_name,last_name,email,gender,ip_address +1,Rowney,Vanlint,rvanlint0@archive.org,Male,166.243.73.22 +2,Granny,Zecchetti,gzecchetti1@toplist.cz,Bigender,76.2.20.212 +3,Elena,Quilkin,equilkin2@artisteer.com,Female,206.153.61.10 +4,Rob,Kilkenny,rkilkenny3@spiegel.de,Genderqueer,56.156.35.222 +5,Alayne,Sleit,asleit4@si.edu,Female,105.12.142.0 +6,Arleta,Radsdale,aradsdale5@psu.edu,Female,20.128.1.2 +7,Maximilian,Oswick,moswick6@hc360.com,Male,241.240.199.79 +8,Gil,Chinnick,gchinnick7@slashdot.org,Male,2.145.45.118 +9,Galen,Goulter,ggoulter8@sakura.ne.jp,Male,37.93.133.252 +10,Samara,Benka,sbenka9@nydailynews.com,Female,225.2.115.39 diff --git a/utils/converter.js b/utils/converter.js index c291270..2a00f2c 100644 --- a/utils/converter.js +++ b/utils/converter.js @@ -1,4 +1,5 @@ const fs = require('fs') +const path = require('path') const csvToJson = (csv) => { const lines = csv.split(/\r\n|\n/) @@ -17,6 +18,8 @@ const csvToJson = (csv) => { } exports.readFile = async (pathFile) => { + const checkExt = path.extname(pathFile) + if (checkExt !== '.csv') throw new Error('Wrong extension file') const result = new Promise((resolve, reject) => { fs.readFile(`${pathFile}`, 'utf8', (err, data) => { if (err) {