feat: adding comments to functions
This commit is contained in:
parent
e24cb16e8e
commit
001aa6ced5
11
index.js
11
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')
|
const { readFile } = require('./utils/converter')
|
||||||
|
|
||||||
|
// Defining an asynchronous function named add which takes a path parameter
|
||||||
const add = async (path) => {
|
const add = async (path) => {
|
||||||
try {
|
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
|
||||||
return await readFile(`${path}`)
|
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) {
|
} 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)
|
throw new Error(e.message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exporting the add function to make it available for use in other modules
|
||||||
module.exports = add
|
module.exports = add
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
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 csvToJson = (csv) => {
|
||||||
const lines = csv.split(/\r\n|\n/)
|
const lines = csv.split(/\r\n|\n/)
|
||||||
const result = [];
|
const result = [];
|
||||||
@ -17,6 +22,13 @@ const csvToJson = (csv) => {
|
|||||||
return JSON.parse(JSON.stringify(result)); //JSON
|
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) => {
|
exports.readFile = async (pathFile) => {
|
||||||
const checkExt = path.extname(pathFile)
|
const checkExt = path.extname(pathFile)
|
||||||
if (checkExt !== '.csv') throw new Error('Wrong extension file')
|
if (checkExt !== '.csv') throw new Error('Wrong extension file')
|
||||||
@ -33,4 +45,3 @@ exports.readFile = async (pathFile) => {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user