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

countjs

v1.2.0

Published

A small utility to count things

Downloads

10

Readme

CountJS

Build Status

Count items and compare them against a reference

Installation

Simply install it via NPM (npm install countjs) and require it in your code:

var Counter = require('countjs')

var c = new Counter()

Usage

countjs is relatively simple -- just instantiate a counter and keep adding stuff to it:

var c = new Counter()

c.add('a')
c.add('a')
c.add('b')

Then you can get how many items are there in the counter:

c.get('a') // 2
c.get('b') // 1
c.get() // {a: 2, b: 1}

You can also specify a custom quantity to add:

c.add('a', {qty: 3}) // 2
c.get('a') // 3

And create a diff between counters:

var c1 = new Counter({a: 1})
var c2 = new Counter({b: 1})

c1.diff(c2) // {a: 1, b: 0}

You can also use "references": if an item has qty X in the reference, then your counter will not be able to add more than X qty of that item:

var ref = {a: 1, b: 2}
var c = new Counter({}, ref)

c.add('a') // true
c.add('a') // false
c.add('b', {qty: 3}) // false
c.add('b', {qty: 2}) // true
c.add('c') // false

c.get() // {a: 1, b: 2}

You can force the counter to accept the new value:

var ref = {a: 1}
var c = new Counter({}, ref)

c.add('a') // true
c.add('a') // false
c.add('a', {qty: 1, force: true}) // true
c.get() // {a: 2}

When using references, you can get a diff between the current counter and the reference. For example:

var ref = {a: 1}
var c = new Counter({}, ref)

c.add('a', {qty: 2, force: true}) // true
c.add('b', {qty: 3, force: true}) // true

c.diff() // {a: {mine: 2, other: 1, diff: 1}, b: {mine: 3, other: 0, diff: 3}}

Last but not least, you can get a full comparison between counters:

var c1 = new Counter({a: 1, b: 1, d: 3})
var c2 = new Counter({b: 2, c: 1, d: 3})

comparison = c1.compare(c2)
// {
//  a: mine 1, other 0
//  b: mine 1, other 2
//  c: mine 0, other 1
//  d: mine 3, other 3
// }

compare is, basically, a #diff() that also includes same values between counters.

Tests

Just run npm test and welcome to Greenland!