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

single-prompt

v0.2.3

Published

prompt for a single character or digit

Downloads

12

Readme

single-prompt Build Status

NPM

Renamed to keypress-prompt

single-prompt is a simple prompter to get a single character or digit on the console:

  • returns a promise
  • automatically lowercases the answer to avoid any issue with case
  • trigged on a single keypress (no need to press enter)
  • limited to single character choices
  • does work with numeric choices and attempts to coerce input to choice type
  • rejects promise if ctrl-c is pressed

Example of prompting for a character

$ cat character.js
var prompter = require('single-prompt');

prompter
  .prompt('Are you crazy', ['y', 'n'])
  .then(function(choice) {
      console.log('choice', choice);
  });

$ node character.js
Are you crazy [y, n]: n
choice n

Example of prompting for a number

$ cat number.js
var prompter = require('./src/single-prompt');

prompter
  .prompt('Number of diners', [1, 2, 3, 4, 5])
  .then(function(choice) {
      console.log('choice', choice);
  });

$ node number.js
Number of diners [1, 2, 3, 4, 5]: 2
choice 2

Note that internally single-prompt attempts to coerce the input to the type of the provided choices. If the match is an integer, it will return an integer so in this example, 2 is of type 'number'. It is assumed you won't do something silly like prompt with options like [1, '1']. It will work just maybe not quite how you want it to.

Bailing out

If Ctrl-C is press at the prompt, the promise will be rejected.

Testing

fakeKeypress is provided for use in tests. For example:

it('lowercases the input to avoid issues with case', function(done) {
    var promise = prompter.prompt('Yes or no', ['y', 'n']);

    prompter.fakeKeypress('N');

    promise.then(
        function(key) {
            expect(key).toBe('n');
            done();
        },
        function() {
            expect('promise').toBe('not rejected');
            done();
        }
    );
});

To send Ctrl-C:

fakeKeypress('c', {
    name: 'c',
    ctrl: true
});