@trinkets/counter

0.0.2 • Public • Published

@trinkets/counter

Tally up sequences of things, in the spirit of Python's Counter.

Installation

npm install @trinkets/counter

Usage

import * as counter from '@trinkets/counter'
// or for just the factory method
// import {create} from '@trinkets/counter'

// Generate a counter
const c = counter.create()

// Add one item...
c('cat')
// ...which is an alias for the more specific...
c.add('cat')
// ...or even more explicit.
c.add('cat', 1)

// Get the value of a particular key
c.get('cat') // === 1, if only one of the calls was made.
c.get('dog') // === 0 by default, not undefined.

// Check if a key has ever been attempted to be tallied
c.exists('dog') // === false

// Tally an array of elements.
c(['pig', 'sheep', 'goat', 'goat'])

// Decrement a particular count.
c('goat', -1)

// Return an array of array counts in descending order, where each element
// is the form of [label: string, count: number].
const counts = c.sorted()
counts.forEach(([label, count]) => {
    console.log(`${label}: ${count}`)
})

Readme

Keywords

Package Sidebar

Install

npm i @trinkets/counter

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

68.8 kB

Total Files

17

Last publish

Collaborators

  • jeremyosborne