diff --git a/index.js b/index.js index 5e6280d..13433dd 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,19 @@ +// Importing the readFile function from a local file named converter within a utils directory const { readFile } = require('./utils/converter') +// Defining an asynchronous function named add which takes a path parameter const add = async (path) => { try { - if (typeof path !== 'string') throw new Error('path have to be an string') - return await readFile(`${path}`) + // Checking if the path parameter is a string, if not, throwing an error + if (typeof path !== 'string') throw new Error('path has to be a string') + + // Using the imported readFile function to asynchronously read the contents of the file at the specified path + return await readFile(`${path}`) } catch (e) { + // Catching any errors that occur during the execution of the function and rethrowing them with a descriptive message throw new Error(e.message) } } +// Exporting the add function to make it available for use in other modules module.exports = add diff --git a/utils/converter.js b/utils/converter.js index 564ae66..8c00d8f 100644 --- a/utils/converter.js +++ b/utils/converter.js @@ -1,6 +1,11 @@ const fs = require('fs') const path = require('path') +/** + * Converts CSV data to JSON format. + * @param {string} csv - The CSV data to be converted. + * @returns {Array} An array of objects representing the converted JSON data. + */ const csvToJson = (csv) => { const lines = csv.split(/\r\n|\n/) const result = []; @@ -17,6 +22,13 @@ const csvToJson = (csv) => { return JSON.parse(JSON.stringify(result)); //JSON } +/** + * Reads a CSV file and converts it to JSON. + * @async + * @param {string} pathFile - The path to the CSV file. + * @returns {Promise>} A promise that resolves to an array of objects representing the CSV data in JSON format. + * @throws {Error} Throws an error if the file extension is not '.csv'. + */ exports.readFile = async (pathFile) => { const checkExt = path.extname(pathFile) if (checkExt !== '.csv') throw new Error('Wrong extension file') @@ -33,4 +45,3 @@ exports.readFile = async (pathFile) => { return result } -