file-handling-toolkit

1.5.0 • Public • Published

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): If true, appends data to the file instead of overwriting it. Defaults to false.

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:

  1. Append to a File Feature: Added an example usage for appending data to an existing file.
  2. API Reference Update: Updated the writeFile function in the API reference to include the new optional append 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.

Package Sidebar

Install

npm i file-handling-toolkit

Weekly Downloads

123

Version

1.5.0

License

MIT

Unpacked Size

7.24 kB

Total Files

10

Last publish

Collaborators

  • iamvirul