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 🙏

© 2025 – Pkg Stats / Ryan Hefner

jc-action-logger

v1.0.1

Published

[![npm version](https://img.shields.io/npm/v/jc-action-logger.svg)](https://www.npmjs.com/package/jc-action-logger)

Readme

Action Logger

npm version

Contents

  1. Getting Started
  2. Installing
  3. Using the Library
  4. Intended Use
  5. Developing Locally
  6. Running Tests
  7. Assumptions
  8. Future Considerations

Getting Started

In order to use this library, you will need both NodeJS and npm installed on your machine. For NodeJS, please install at least version 10 (current active LTS version) or later. You can find a link to download NodeJS here for whatever system you are running. Downloading NodeJS from this source, will also install the latest npm version, so you should not have to install npm separately. If for whatever reason, you would like to install npm separately, you can find instructions to do so here.

Installing

Once you have NodeJS and npm installed, you can then install this library. This library uses npm as its distribution mechanism, so in order to install the library, you can use the following command:

npm install --save jc-action-logger

This needs to be run in the project directory for which you are installing this library. The project should already be setup as an npm enabled project. If it is not you can look here at how to initialize a project with npm.

Using the Library

This library allows you to log specific actions that are associated with a given time. The library will then track the average time for each action logged. The Library then lets you retrieve the average time associated with all of the actions logged in the system.

The library exposes a Logger class, that can be instantiated in the following way:

const Logger = require('jc-action-logger');

const logger = new Logger();

The Logger class has the following API:

addAction

logger.addAction(actionString);

addAction is an asynchronous function that takes a JSON serialized string as its only argument. It returns a Promise that does not return any data when resolved. It will asynchronously log the action so that the time value can then be calculated as part of the average time for that specific action.

This must be valid JSON and must be structured in the following format:

{"action":"jump","time":100}

The value for the action property is a string that represents the name of the action being logged. The value for the time property is a number that represents the time associated with that action.

If there are any errors that occur within addAction it will throw an exception that can be caught in a catch block, which would look like this:

logger.addAction(actionString)
  .catch(error => {});

getStats

logger.getStats()
  .then(actionData => {})
  .catch(error => {});

getStats is used to asynchronously retrieve the average time for each action that has been logged. It takes no arguments and returns a Promise that resolves with the the data of all of the actions and their average. In the case above, actionData is a JSON serialized string, which takes the following form:

[
  {"action":"jump","avg":20},
  {"action":"run","avg":50}
]

If there are any errors that occur within getStats it will throw an exception that can be caught in the catch block.

Using async/await

You can utilize this library using the Promise API, like mentioned above, with then and catch. However, if you want, you can also utilize the library with async/await, which would look like this:

async function addAction() {
  try {
    await logger.addAction('{"action":"jump","time":100}');
  } catch (error) {
    //handle error
  }
}
async function getStats() {
  try {
    const stats = await logger.getStats();
    const deserializedStats = JSON.parse(stats);
  } catch (error) {
    //handle error
  }
}

Intended Use

This library could be used for many purposes. It would be helpful for any sort of system where you have something happening, an event or action, and would like to track some sort of time metric associated with that event. One example would be performance tracking. Say you had a couple of events within a system and you wanted to track how long on average each of these events took, then this would be a good use case for this library.

Developing locally

When developing locally, you can pull down this github repository.

You will need to have git installed on your machine along with the above mentioned dependencies of NodeJS and npm.

Running Tests

In order to run the tests for the library, you need to do the following:

npm install
npm run test

This library uses Jest as the testing framework.

Assumptions

In regards to the environment that this code will be run in, the only assumption that I am making is that it will be on a machine that can run the above specified dependencies of NodeJS and npm.

I am assuming that a library of this sort would be consumed by some sort of package manager, which in this case, I chose to use npm.

When the assignment says that a user will be making concurrent calls into this library, I am assuming that this means that even though the nature of the library functionality is not inherently asynchronous, it should be asynchronous in order to allow for concurrent calls. Therefore, I utilized setTimeout to turn the synchronous nature of adding and retrieving the data into asynchronous code.

The assignment does not say anything about rounding the averages, but I decided to round the averages that are sent back from getStats to two decimal places. This was primarily just to make the response cleaner and could be easily changed as I am not rounding the average that is stored, but only the average that is returned.

Future Considerations

I think in terms of future considerations, this sort of library would ideally be connected to some sort of persistent data layer, which would store the data around the actions. This would also make the concurrency and asynchronous aspect of the library make more sense, since the library would be making calls to a data store.