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

inquirer-maxlength-input-prompt

v1.0.2

Published

Input prompt with max length for inquirer

Downloads

161,530

Readme

inquirer-maxlength-input-prompt

Input prompt with max length for inquirer

tests test coverage npm published version require node version license

Extends the built-in input prompt type to allow a max input length to be specified, and shows a character counter which updates as the user types.

inquirer-maxlength-input-prompt

Usage

Install using npm or yarn:

yarn add inquirer-maxlength-input-prompt

Register the prompt with inquirer:

const inquirer = require('inquirer')
const MaxLengthInputPrompt = require('inquirer-maxlength-input-prompt')

// The name doesn't have to `maxlength-input` - it can be anything you like
inquirer.registerPrompt('maxlength-input', MaxLengthInputPrompt)

When you use the prompt, you must provide a maxLength option:

inquirer.prompt([
  {
    type: 'maxlength-input',
    name: 'title',
    message: 'Enter a title',
    maxLength: 20
  }
]).then(answers => {
  /* ...snip... */
})

Options

In addition to the options that the built-in input type prompt takes, this plugin also takes:

  • maxLength (Number [required]): the maximum number of characters the user can enter

The following options behave slightly differently, and are demonstrated in:

  • transformer (Function): you get the current answers hash as an extra (second) argument.
  • validate (Function): for convenience, you get the raw user input as an extra (third) argument. This makes it easy to do checks against raw user input when you have a filter parameter that adds prefixes/suffixes/otherwise modifies the input.
    • the validation against maxLength is performed before your own custom validate function is invoked

These additions are demonstrated in the example "Adding a prefix" below.

Examples

Basic usage

const inquirer = require('inquirer')
const MaxLengthInputPrompt = require('inquirer-maxlength-input-prompt')

inquirer.registerPrompt('maxlength-input', MaxLengthInputPrompt)

inquirer.prompt([
  {
    type: 'maxlength-input',
    name: 'title',
    message: 'Enter a title',
    maxLength: 15
  }
]).then(console.log)

Adding a prefix

This example shows one way to prefix the user's input with a value and pretty print it during prompting, using the filter and transformer options respectively. In addition, this example also shows how the raw parameter that is passed to the validate function can be used - in this case, it is used to ensure the user still enters data despite the presence of the prefix.

Advanced example

const chalk = require('chalk')
const inquirer = require('inquirer')
const MaxLengthInputPrompt = require('inquirer-maxlength-input-prompt')

inquirer.registerPrompt('maxlength-input', MaxLengthInputPrompt)

inquirer.prompt([
  {
    type: 'list',
    name: 'commitType',
    message: 'What type of change are you committing?',
    choices: ['feature', 'fix', 'docs', 'other']
  },
  {
    type: 'maxlength-input',
    name: 'commitHeader',
    maxLength: 50,
    message: 'Enter the commit subject',
    validate(input, answers, raw) {
      if (!raw) {
        return 'Please enter a subject for your commit'
      }

      return true
    },
    filter(input, answers) {

      // Return prefix + input
      return `${answers.commitType}: ${input}`
    },
    transformer(input, answers) {

      // Pretty print prefix before input to assist user
      return chalk.blue(`${answers.commitType}: `) + input
    }
  }
]).then(console.log)