feat: adding comments to functions

This commit is contained in:
Carlos 2024-03-24 21:04:27 -04:00
parent e24cb16e8e
commit 001aa6ced5
2 changed files with 21 additions and 3 deletions

View File

@ -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')
// 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

View File

@ -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<Object>} 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<Array<Object>>} 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
}