# inquirer-maxlength-input-prompt

> Input prompt with max length for inquirer

Latest version **1.0.2** (published 2020-11-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install inquirer-maxlength-input-prompt
pnpm add inquirer-maxlength-input-prompt
yarn add inquirer-maxlength-input-prompt
bun add inquirer-maxlength-input-prompt
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2020-11-10 |
| First published | 2018-02-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 5 |
| Unpacked size | 229.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | James Warwood |
| Maintainers | jwarby |
| Keywords | inquirer, prompt, maxlength, length, character, counter |

## Links

- npm: https://www.npmjs.com/package/inquirer-maxlength-input-prompt
- Repository: https://github.com/jwarby/inquirer-maxlength-input-prompt
- Homepage: https://github.com/jwarby/inquirer-maxlength-input-prompt#readme
- Issues: https://github.com/jwarby/inquirer-maxlength-input-prompt/issues
- npm.io page: https://npm.io/package/inquirer-maxlength-input-prompt

## Dependencies (5)

- [chalk](https://npm.io/package/chalk.md) ^2.3.1
- [inquirer](https://npm.io/package/inquirer.md) ^5.1.0
- [lodash.invoke](https://npm.io/package/lodash.invoke.md) ^4.5.2
- [lodash.isfunction](https://npm.io/package/lodash.isfunction.md) ^3.0.9
- [lodash.partialright](https://npm.io/package/lodash.partialright.md) ^4.2.1

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2020-11-10
- 1.0.1 — 2020-07-22
- 1.0.0 — 2018-02-28

## README

# inquirer-maxlength-input-prompt
> Input prompt with max length for [inquirer](https://www.npmjs.com/package/inquirer)

> [![tests](https://img.shields.io/travis/jwarby/inquirer-maxlength-input-prompt.svg?label=tests&style=flat)](https://travis-ci.org/jwarby/inquirer-maxlength-input-prompt)
> ![test coverage](https://img.shields.io/coveralls/jwarby/inquirer-maxlength-input-prompt.svg?label=test%20coverage&style=flat)
> [![npm published version](https://img.shields.io/npm/v/inquirer-maxlength-input-prompt.svg?style=flat)](https://www.npmjs.com/package/inquirer-maxlength-input-prompt)
> [![require node version](https://img.shields.io/node/v/inquirer-maxlength-input-prompt.svg?style=flat)](https://nodejs.org/en/download/releases/)
> ![license](https://img.shields.io/github/license/jwarby/inquirer-maxlength-input-prompt.svg?style=flat)

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](screencast/basic_example.gif)

## Usage

Install using `npm` or `yarn`:

```bash
yarn add inquirer-maxlength-input-prompt
```

Register the prompt with `inquirer`:

```javascript
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:

```javascript
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](https://www.npmjs.com/package/inquirer#input---type-input)
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](#adding-a-prefix)"
below.

## Examples

### Basic usage

```javascript
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](screencast/advanced_example.gif)

```javascript
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)
```

---
_Source: https://npm.io/package/inquirer-maxlength-input-prompt · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
