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

@tmuniversal/counter

v1.0.5

Published

Counters that are a bit more advanced than a simple variable

Downloads

19

Readme

Counter

Counter creates simple counters that are a bit more than just a variable. These counters can watch for changes and alert you when a target value is reached or passed.

Getting Started

Installation

With npm: npm install --save @tmuniversal/counter

With yarn: yarn add @tmuniversal/counter

Usage

Creating a counter

const Counter = require('@tmuniversal/counter')

const testCounter = new Counter(3) // Start at 3

Working with the counter

const exampleCounter = new Counter(1, {
  target: 4,
  exactMatch: false,
  once: true
})

console.log(exampleCounter.value)
// => 1

// The value can be set directly
exampleCounter.value = 5

// Or be changed using .increment() and .decrement()
exampleCounter.decrement(2)
// => 3

exampleCounter.increment()
// => 4

console.log(exampleCounter.value)
// => 4

A counter will provide a custom toString() method for easy access to the value within template strings:

console.log(`The value of the counter is ${exampleCounter}`)
// The value of the counter is 4

The counter will save it's latest operations, this means you can access the last change (the difference between the current and the previous value):

console.log(exampleCounter.lastChange)
// => 1

exampleCounter.decrement(2)

console.log(exampleCounter.lastChange)
// => -2

The last five values of the counter are saved in an array, ordered from first to last. The current value is saved as the last element of the array.

console.log(exampleCounter.last5)
// => [ 5, 3, 4, 2 ]

exampleCounter.increment()

console.log(exampleCounter.last5)
// => [ 5, 3, 4, 2, 3 ]

exampleCounter.increment()

console.log(exampleCounter.last5)
// => [ 3, 4, 2, 3, 4 ]

Events

A counter emits two kinds of events: "change", when the value of the counter changes; "target" when the counter reaches (or passes) the target option.

exampleCounter.on('target', value => {
  console.log(`reached ${value}`)
})

exampleCounter.on('change', (newValue, oldValue) => {
  console.log(`${oldValue} => ${newValue}`)
})

Note: Passing the target means that the current value is equal or beyond the target value, as seen from the starting value. This means the target event can count backwards.

Credits

This project is heavily inspired by the existing but very old package counter.

License

Please refer to the LICENSE file.