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

type-switch

v1.0.0

Published

handles/tracks keypress events

Downloads

4

Readme

type-switch

A portable signal engine used to power games and interactive experiences with keypress events.

Installation

npm install --save 'type-switch'

Usage

var TypeSwitch = require('type-switch');

var myTypeSwitch = new TypeSwitch();

API

TypeSwitch({options})

options

Type: object

Options set here will override the defaults in the constructor.

stubbornMode

One of the larger distinctions among typing games is whether or not an incorrect input will advance the user's position in the prompt. Setting stubbornMode to true will force the user to type the correct character before advancing further.

Type: boolean Default: false

typeSwitch.getGameStats()

returns an object containing your instance's current game stats

example
myTypeSwitch.getGameStats();

/*{
  prompt: 'Hey Mr. User, type this prompt!',
  time: 23,
  paused: false,
  currentIndex: 5,
  lastExpectedInput: 'M',
  lastUserInput: 'M',
  result: 'correct',
  incorrectTotal: 2
  }*/

This should all be pretty self-explanatory, however it's important to note that 'currentIndex' is not the index when the key was pressed, but rather the index of the prompt resulting from the last keystroke. So in the case above, the user was at prompt[4] when they typed the correct answer leading to a currentIndex of 5.

typeSwitch.start(prompt)

begins game clock and accepts a prompt for the user to type

prompt

Type: string

typeSwitch.changeCurrentIndex(newIndex)

changes the users position in the game prompt

newIndex

Required

Type: number

typeSwitch.changeTime(newTime)

changes the game time to provided value

newTime

Required

Type: number

typeSwitch.changePrompt(newPrompt)

used to change the user's prompt mid-game

newPrompt

Required

Type: string

typeSwitch.pauseGame()

pauses game clock and causes type-switch to refrain from listening to keystrokes

typeSwitch.resumeGame()

resumes game clock and informs type-switch to continue listening for keystrokes

typeSwitch.pauseGameClock()

pauses game clock, but type-switch continues to listen for keystrokes

typeSwitch.resumeGameClock()

resumes game clock, but does not reinstitute type-switch's keypress event listener

typeSwitch.restartGame()

This method resets the user's current index, game clock, and incorrect total, then begins the game anew with the most recently provided prompt.

typeSwitch.resetGame()

This method is useful if you want to restart the game, but have some other code you want to run beforehand. All game stats will be reset except the most recent prompt and the game will not immediately begin again.

typeSwitch.broadcast(event)

type-switch is an EventEmitter. Use this method to create your own custom hooks.

event

Required

Type: string

type-switch has 3 hooks by default: 'correct', 'incorrect', and 'complete'. Calling typeSwitch.getGameStats() within these hooks is a powerful way of monitoring the user's progress and running your own code in synchrony with each keypress.

example
var myTypeSwitch = new TypeSwitch();
var points = 0;
var failureMessage = 'Oh man! You stink! You should have typed ';
var successMessage = 'Great job, good sir! *tips hat*';
function partyTime() {...};

myTypeSwitch.on('incorrect', function() {
  var stats = myTypeSwitch.getGameStats();
  if (stats.incorrectTotal > 4) {
    alert(failureMessage + stats.lastExpectedInput + ', but you typed ' + stats.lastUserInput + ', time to start over!');
    points = 0;
    myTypeSwitch.restartGame();
  }
});

myTypeSwitch.on('correct', function() {
  points++;
});

myTypeSwitch.on('complete', function() {
  alert(successMessage);
  partyTime();
});

License

MIT © [Christopher Howard]