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

escape-stack

v0.3.1

Published

A sweet way of handling things that close (or do other things) on escape

Downloads

1,110

Readme

escape-stack

A sweet way of handling things that close (or do other things) on escape

Demo

http://relateiq.github.io/escape-stack

See the Developing section for instructions on how to play with the demo locally

Installation

npm install escape-stack

Use the --save flag to save it to your package.json.

Usage

Escape Stack exports a function that creates the stack. You can add event handler functions to the stack which get called as you pop the stack.

import createEscapeStack from 'escape-stack';

const escapeStack = createEscapeStack();

Adding to the Escape Stack

The add(handler: Function) function pushes an event handler onto the stack.

The following example functions return true so they pop once per each escape keypress.

function closeModal(event) {
  // logic!
  return true;
}

function paintItBlack() {
  // logic!
  return true;
}

function doTheHappyDance() {
  // logic!
  return true;
}

escapeStack.add(closeModal);
escapeStack.add(paintItBlack);
escapeStack.add(doTheHappyDance);

Popping from the Escape Stack

Pressing the escape key pops and calls the most recent event handler that was added to the stack.

In our example, pressing escape once will call doTheHappyDance(). Pressing it again will call paintItBlack(). And pressing it again will call closeModal(). At this point, the stack is empty, and further presses of the escape key are no-ops until more handlers are added to the stack.

Single and Continuous Popping

Internally, pop() checks the return value of the handler functions. It continues to pop the handlers (remove them from the stack and execute them) until true is returned by a handler function or until the stack is empty.

In our example, since we returned true for every handler function we added to the escape stack, pressing the escape key will only remove and call one function at a time every time the escape key is pressed.

If they all returned false, they will all be removed and called the first time the escape key is pressed.

If doTheHappyDance() returned false and paintItBlack() returned true, both will be removed and called when the escape key is pressed, but not closeModal().

Removing a Handler

The return value of the add() function is a function that removes that specific handler from the stack when called.

function closeModal(event) {
  // logic!
  return true;
}

function paintItBlack() {
  // logic!
  return true;
}

function doTheHappyDance() {
  // logic!
  return true;
}

const removeCloseModal = escapeStack.add(closeModal);
const removePaintItBlack = escapeStack.add(paintItBlack);
const removeDoTheHappyDance = escapeStack.add(doTheHappyDance);

// remove paintItBlack() from the stack without executing it
removePaintItBlack();

In this example, pressing escape will call doTheHappyDance(), and pressing it again will call closeModal(), leaving the stack empty.

Developing

This library is written in Typescript. In order to run or modify the source you will first need to install typescript by running:

npm install -g typescript

Then install the depedendencies by running:

npm install
typings install

The source code lives in ./src/. When compiled, the resulting javascript (es5) code will be in ./.src/. Similarly, the demo app that utilizes the library lives in ./app/, and its resulting javascript code will be in ./.app/.

Tests live alongside their respective files being tested, and end in .spec.ts. The tests for Escape Stack are written using tape.

To start a watcher that continues to compile the source and run tests, run:

npm run start

Every time a change is made to the source, this will recompile the code and run tests on it. It will let you know which port the demo app is running on (typically :8080).

Contributing

Have any suggestions? Have an issue? Want to make any improvements? We would love your feedback!

Use the issue tracker to report any issues.

If you would like to contribute code:

  • clone this repo
  • make a new branch
  • make changes
  • ensure all the tests pass, and write new tests as necessary
  • submit a pull request!