npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

easier-fs

v1.0.5

Published

FileSystem but much easier =). This is a wrapper of default node file system.

Downloads

3

Readme

easier-fs

A library that wraps the default node.js file system, making file operations easier.

Advantages

Some advantages of using easier-fs over the native fs module include:

  • Easier file manipulation: easier-fs provides methods for reading and writing to files as text or JSON, as well as for appending data to a file or merging data with an existing JSON file. These methods abstract away much of the complexity of working with the fs module and make it easier to perform common file manipulation tasks.

  • Improved error handling: easier-fs provides more informative error messages than the fs module in cases where a file does not exist or cannot be accessed. This can make it easier to identify and fix issues when working with files.

  • Consistent API: easier-fs provides a consistent API for working with different file types, making it easier to switch between file types without having to learn new method names or syntax.

  • Customizability: easier-fs allows users to customize the library's behavior through the config parameter in the constructor. This can be used to add support for additional file types or to override the default behavior of certain methods. (WILL BE ADDED LATER)

Usage

To use easier-fs, import the library and create a new instance of the easier-fs class, passing in the name of the file you want to work with and any additional configuration options.

import efs from 'easier-fs';

const fileSystem = new efs({ name: 'example.txt' });

You can then use the provided methods to read from or write to the file. For example, to read the contents of a file as text:

const fileContents = fileSystem.read();

Or to write some data:

fileSystem.write({ data: "Hello World" });

For a full list of available methods and their usage, see the documentation below.

Documentation

Constructor

new efs({ name, mode='default', fileType, args=[], config })

Creates a new instance of easier-fs.

Parameters
  • name (string): The name of the file to work with.
  • mode (string, optional): The mode in which to open the file. Possible values are 'default', 'read', and 'write'. Default is 'default'.
  • fileType (string, optional): The type of the file. Possible values are 'txt' and 'json'. Default is 'txt'.
  • args (array, optional): An array of arguments to pass to the chosen file action.
  • config (object, optional): An object containing custom configuration options for the library.
Returns

A new instance of easier-fs.

write

write({ name, data = '', fileType=this.fileConfig.type, mode ,args}={})

Writes data to the file.

Parameters
  • name (string): You can change file that you are working on (only for a single time)
  • data (string or object): The data to write to the file.
  • fileType (string, optional): The file type to use for writing. If not provided, the fileType property of the fileConfig object or from the filename ("name.txt") itself will be used.
  • mode (string, optional): The action to perform when writing to the file. Possible values are 'default' and 'merge' (for JSON files only) and 'default' and 'append' (for txt files only) . Default is 'default'.
  • args (array, optional): An array of arguments to pass to the chosen file action (for example works only with append to choose way).
Returns

The current instance of easier-fs.

read

read({ name, fileType=this.fileConfig.type }={})

read data from the file.

Parameters
  • name (string): You can change file that you are working on (only for a single time)
  • data (string or object): The data to write to the file.
  • fileType (string, optional): The file type to use for writing. If not provided, the fileType property of the fileConfig object or from the filename ("name.txt") itself will be used.
Returns

The current instance of easier-fs.

Examples

Here are some examples of how to use these functions with the ES6 import syntax

basic usage:

import efs from 'easier-fs'
// Read a txt file
const txtFile = new efs({ name: 'my-file.txt'})
.read(); // Reads the contents of 'my-file.txt' as a string

// Read a json file
const jsonFile = new efs({ name: 'my-file.json'})
.read(); // Reads the contents of 'my-file.json' as a JSON object

// Write to a txt file
const txtFile = new efs({ name: 'my-file.txt'})
.write({ data: 'Hello, World!' }); // Overwrites 'my-file.txt' with the string 'Hello, World!'

// Append data to a txt file
const txtFile = new efs({ name: 'my-file.txt'})
.write({ mode: 'append', data: 'Hello, World!' }); // Appends the string 'Hello, World!' to the end of 'my-file.txt'

// Write to a json file
const jsonFile = new efs({ name: 'my-file.json'})
.write({ data: { message: 'Hello, World!' } }); // Overwrites 'my-file.json' with the JSON object { message: 'Hello, World!' }

// Merge data with an existing json file
const jsonFile = new efs({ name: 'my-file.json'})
.write({ mode: 'merge', data: { message: 'Hello, World!' } }); // Merges the existing data in 'my-file.json' with the JSON object { message: 'Hello, World!' }

Advanced usage:

Filename without filetype

if you pass filename , that does not have a filetype , you can specify it as the second argument:

const txtFile = new efs({ name: 'my-file', fileType: 'txt' })

Chaining

You can make use a chain when you use write or other methods that does not return value (example: you cannot use it with read, because it returns file data):

const txtFile = new efs({ name: 'my-file.txt' })
.write({data:"Test 1"})
.write({mode: 'append', data:"Test 2"})

mode

you can specify mode in the beginning, and it will be applied to all next actions:

const txtFile = new efs({ name: 'my-file.txt',mode: 'append' })
.write({data:"Test 1"})
.write({data:"Test 2"})
.write({data:"Test 3"})
.read(); //will return "Test 1Test 2Test 3"

or for a single time

const txtFile = new efs({ name: 'my-file.txt'})
.write({data:"Test 1"})
.write({data:"Test 2"})
.write({data:"Test 3",mode: 'append'})
.read();//will return Test 2Test 3

filename

You can change the file you are working on at any function, but for a single time

const txtFile = new efs({ name: 'file1.txt',mode: 'append' })
.write({data:"Test 1"})//saves to file1.txt
.write({name:'file2.txt',data:"Test 2"})//saves to file2.txt
.read();//will return Test 1 from file1.txt
const txtFile = new efs({ name: 'file1.txt',mode: 'append' })
.write({data:"Test 1"})//saves to file1.txt
.write({name:'file2.txt',data:"Test 2"})//saves to file2.txt
.read({name:'file2.txt'});//will return Test 2 from file2.txt

additional

you can provide additional arguments for append mode:

const txtFile = new efs({ name: 'file1.txt',mode: 'append',args=[false] })
.write({data:"Test 1"})

true - data will append to the end of the file false - data will append to the beginning of the file, before file data