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 🙏

© 2026 – Pkg Stats / Ryan Hefner

read-and-write

v1.1.7

Published

Start by importing the constructor function

Downloads

15

Readme

Getting Started

Start by importing the constructor function

const ReadAndWrite = require("read-and-write").ReadAndWrite;

Next create a new instance of the constructor function with the path to the file that you want to read from and write to

const fileReader = new ReadAndWrite("./users.txt");

Reading From A File

Synchronously

The following line reads all records from the file

let users = fileReader.readAllRecordsSync();

It should return an array of objects, each object representing one record

Example:

[
  {
    username: 'berrybloxinator',
    firstName: 'Brady',
    lastName: 'Liechty',
    email: '[email protected]',
    age: '20',
    userId: '03a13ff9-9bfe-483f-8cc5-fa16caf1898b',
    timeCreated: '1/28/2020, 2:33:33 PM'
  },
  {
    username: 'skateboardnerd',
    firstName: 'John',
    lastName: 'Hill',
    email: '[email protected]',
    age: '25',
    userId: '3ef6972e-6c71-4fd2-82df-da4c2dc9baf5',
    timeCreated: '1/28/2020, 2:34:18 PM'
  }
]

Asynchronously

To use this function asynchronously you have the option pass a callback that will be called when all the files contents have been read

The readAllRecords function will call your callback with the file contents formatted as an array of objects just like with the readAllRecordsSync function

let users = [];
fileReader.readAllRecords(fileContents => {
    users = fileContents;
});

Appending Records

Synchronously

To append records pass in an array of objects

let users = [];
const d = new Date();
const tempUsers = [
    {
        username: "Destiny",
        firstName: "Steven",
        lastName: "Bonnell",
        email: "[email protected]",
        age: 32,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    },
    {
        username: "xXhardcore_gamerXx",
        firstName: "Doug",
        lastName: "Smith",
        email: "[email protected]",
        age: 29,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    }
];
fileReader.appendRecordsSync(tempUsers);

Append records does not return anything so in this case I have to manually add the appended users to my array

users.push(...tempUsers);

Appending records will not overwrite the file, only add to the end of it.

Asynchronously

If you do this function asynchronously you have the option of passing in a callback that will do whatever you want it to whenever the function has appended the user(s)

This callback is not called with any parameters

let users = [];
const d = new Date();
const tempUsers = [
    {
        username: "Destiny",
        firstName: "Steven",
        lastName: "Bonnell",
        email: "[email protected]",
        age: 32,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    },
    {
        username: "xXhardcore_gamerXx",
        firstName: "Doug",
        lastName: "Smith",
        email: "[email protected]",
        age: 29,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    }
];
fileReader.appendRecords(tempUsers, () => {
    console.log("user successfully appended to file");
});
users.push(...tempUsers);

Writing Records

Synchronously

Writing records is done almost the exact same way except for the fact that the file is overwritten

let users = [];
const d = new Date();
const tempUsers = [
    {
        username: "Destiny",
        firstName: "Steven",
        lastName: "Bonnell",
        email: "[email protected]",
        age: 32,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    },
    {
        username: "xXhardcore_gamerXx",
        firstName: "Doug",
        lastName: "Smith",
        email: "[email protected]",
        age: 29,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    }
];
fileReader.writeRecordsSync(tempUsers);
users = tempUsers;

Asynchronously

Just like the asynchronous version of appendUsers you can pass in a callback that will do whatever you want when the users are written to the file

This callback is not called with any parameters

let users = [];
const d = new Date();
const tempUsers = [
    {
        username: "Destiny",
        firstName: "Steven",
        lastName: "Bonnell",
        email: "[email protected]",
        age: 32,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    },
    {
        username: "xXhardcore_gamerXx",
        firstName: "Doug",
        lastName: "Smith",
        email: "[email protected]",
        age: 29,
        userId: uuid.v4(),
        timeCreated: d.toLocaleString()
    }
];
fileReader.writeRecords(tempUsers, () => {
    console.log("Users successfully written to file");
});
users = tempUsers;

Deleting Records

Synchronously

To delete a record you have to pass in an id

An id is an object that is used to identify the record that you want to be deleted

let users = [];
users = fileReader.deleteRecordSync({
    key: "name",
    value: "Brady"
});

Pretty self explanatory

This will find the record with the key and value that you pass in

It will remove it from the file and return an array with all of of the records except the one that was deleted

Asynchronously

Keep in mind that for deleteRecord your callback will be called with an updated array of your records

let users = [];
fileReader.deleteRecord({
    key: "name",
    value: "Brady"
}, refactoredUsers => {
    users = refactoredUsers;
});

Editing Records

Synchronously

This is the most complicated function in this package

Just like the delete records function, the first parameter is an id to determine which record to edit

The second parameter is an array of id's to tell the function which parts of the record you want to change

The second parameter is where you specify the key and then specify what value you want the keys value to change to

let users = [];
users = fileReader.editRecordSync({
    key: "userId",
    value: 3ef6972e-6c71-4fd2-82df-da4c2dc9baf5
}, [
    {
        key: "username",
        value: "AnthonyPadilla"
    }, {
        key: "email",
        value: "[email protected]"
    }
]);

After running this the record with the userId of 3ef6972e-6c71-4fd2-82df-da4c2dc9baf5 would change from

{
    username: "A_Padilla",
    firstName: "Anthony",
    lastName: "Padilla",
    email: "[email protected]",
    age: 29,
    userId: 3ef6972e-6c71-4fd2-82df-da4c2dc9baf5,
    timeCreated: 1/28/2020, 2:34:18 PM
}

to

{
    username: "AnthonyPadilla",
    firstName: "Anthony",
    lastName: "Padilla",
    email: "[email protected]",
    age: 29,
    userId: 3ef6972e-6c71-4fd2-82df-da4c2dc9baf5,
    timeCreated: 1/28/2020, 2:34:18 PM
}

The editRecordSync function will return an array with all of the records including the edited record

Asynchronously

Your callback will again be called with an updated array of your records

let users = [];
fileReader.editRecord({
    key: "userId",
    value: 3ef6972e-6c71-4fd2-82df-da4c2dc9baf5
}, [
    {
        key: "username",
        value: "AnthonyPadilla"
    }, {
        key: "email",
        value: "[email protected]"
    }
], refactoredUsers => {
    users = refactoredUsers;
});

Notes

The records that you write to a file can have any amount of key value pairs and you can have whatever keys and values you wish

The callback is optional for every asynchronous version of all functions

Synchronous functions will always end with Sync and asynchronous functions will not

readAllRecordsSync (synchronous)

readAllRecords (asynchronous)