1.0.5 • Published 3 years ago

@tmuniversal/counter v1.0.5

Weekly downloads
26
License
MIT
Repository
github
Last release
3 years ago

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.

1.0.5

3 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago