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

@konfirm/state-emitter

v1.0.6

Published

Prepare an emitter with a fixed set of states, which can be referenced by their index of string/symbol value

Downloads

2

Readme

StateEmitter

Codacy Badge Build Status

Create an Emitter with a fixed set of states which allows to listen for and emit on either the index and/or string/Symbol value;

Getting Started

Installation

StateEmitter is a scoped package, which means both the installation and require (or import) need the scope along with the package name.

$ npm install @konfirm/state-emitter

Usage

A StateEmitter instance allows for states to be either a string or a Symbol, whereas Symbols have the advantage they cannot be reproduced and are therefor gauranteed unique.

Example with string values

const StateEmitter = require('@konfirm/state-emitter');
const emitter = new StateEmitter(['foo', 'bar', 'baz']);

//  the "states" can now be referenced by both their index or the string value
emitter.on(1, () => console.log('this is bar'));
emitter.on('bar', () => console.log('this is bar too'));

emiter.emit(1);
//  this is bar
//  this is bar too

Example with Symbols

const StateEmitter = require('@konfirm/state-emitter');
const foo = Symbol('foo');
const bar = Symbol('bar');
const baz = Symbol('baz');
const emitter = new StateEmitter([foo, bar, baz]);

//  the "states" can now be referenced by both their index or the Symbol value
emitter.on(1, () => console.log('this is symbol bar'));
emitter.on(bar, () => console.log('this is symbol bar too'));

emiter.emit(1);
//  this is symbol bar
//  this is symbol bar too

API

EventEmitter methods

As StateEmitter extends from the native Node.js EventEmitter, all methods are available, with the exception of:

  • listenerCount(emitter, eventName) is officially deprecated, StateEmitter does not implement this so the eventName argument is not guaranteed to be a normalized state.
  • rawListeners(eventName) is a new addition (Node.js v9.4.0+), StateEmitter doen not (yet) implement this method so the eventName argument is not guaranteed to be a normalized state.

Additional methods

index(string|symbol state)

Obtain the index associated with a string or symbol state

const StateEmitter = require('@konfirm/state-emitter');
const baz = Symbol('baz');
const emitter = new StateEmitter(['foo', 'bar', baz]);

emitter.index('foo');   //  0
emitter.index(baz);     //  2
emitter.index('nope');  //  undefined

state(index)

Obtain the string or symbol state associated with the index.

const StateEmitter = require('@konfirm/state-emitter');
const baz = Symbol('baz');
const emitter = new StateEmitter(['foo', 'bar', baz]);

emitter.state(0);  //  'foo'
emitter.state(2);  //  Symbol('baz')
emitter.state(3);  //  undefined

normalize(state)

Obtain the state from either a provided index or a known value. Throws an Error('Unknown state "<value>"') otherwise.

const StateEmitter = require('@konfirm/state-emitter');
const baz = Symbol('baz');
const emitter = new StateEmitter(['foo', 'bar', baz]);

emitter.state(0);  //  'foo'
emitter.state(2);  //  Symbol('baz')
emitter.state(3);  //  throws Error('Error('Unknown state "3"')

License

MIT License Copyright (c) 2018 Rogier Spieker (Konfirm)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.