file-handling-toolkit
v1.5.0
Published
A toolkit for simplifying common file handling tasks in Node.js applications.
Downloads
11
Readme
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);
});
Append to a File
const { writeFile } = require('file-handling-toolkit');
const data = 'Appending this text.';
writeFile('output.txt', data, true)
.then(() => {
console.log('Data appended to file successfully!');
})
.catch(error => {
console.error('Error appending 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, append?: boolean): 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.append
(optional): Iftrue
, appends data to the file instead of overwriting it. Defaults tofalse
.
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:
- Append to a File Feature: Added an example usage for appending data to an existing file.
- API Reference Update: Updated the
writeFile
function in the API reference to include the new optionalappend
parameter.
This updated README.md
provides clear instructions and examples for users on how to use the new appending feature along with the existing file read, write, and CSV read features.