File Handling Toolkit
Thursday, May 9, 2024
File Handling Toolkit
File Handling Toolkit is a Node.js package that provides utility functions for handling files, including reading and writing text files, as well as reading CSV files and converting them to arrays of objects.
Installation
You can install the File Handling Toolkit package via npm:
npm install file-handling-toolkit
Usage
Read a File
const { readFile } = require('file-handling-toolkit'); readFile('example.txt') .then(data => { console.log('File content:', data); }) .catch(error => { console.error('Error reading file:', error); });
Write to a File
const { writeFile } = require('file-handling-toolkit'); const data = 'Hello, world!'; writeFile('output.txt', data) .then(() => { console.log('Data written to file successfully!'); }) .catch(error => { console.error('Error writing to file:', error); });
Read a CSV File
const { readCsvToArray } = require('file-handling-toolkit'); readCsvToArray('example.csv') .then(data => { console.log('CSV Data:', data); }) .catch(error => { console.error('Error reading CSV file:', error); });
API Reference
readFile(filePath: string): Promise<string>
Reads the contents of a file and returns a Promise that resolves with the file content as a string.
- filePath: Path to the file to be read.
writeFile(filePath: string, data: string): Promise<void>
Writes data to a file and returns a Promise that resolves when the operation completes successfully.
- filePath: Path to the file to write.
- data: Data to be written to the file.
readCsvToArray(filePath: string): Promise<object[]>
Reads a CSV file and returns a Promise that resolves with the content of the CSV file as an array of objects.
- filePath: Path to the CSV file to be read.
License
This project is licensed under the MIT License; see the LICENSE file for details.
Explanation:
- Added CSV Reading Feature: Included is an example usage for reading a CSV file and converting it to an array of objects.
- API Reference: Added a new section for the readCsvToArray function in the API reference, detailing its usage and parameters.
This updated README.md provides clear instructions and examples for users on how to use the new CSV reading feature along with the existing file read and write features.