{"id":20206831,"url":"https://github.com/enquirer/prompt-choices","last_synced_at":"2025-10-26T09:45:43.992Z","repository":{"id":60775126,"uuid":"66798765","full_name":"enquirer/prompt-choices","owner":"enquirer","description":"This repository has been archived, use Enquirer instead.","archived":false,"fork":false,"pushed_at":"2018-05-28T20:28:26.000Z","size":228,"stargazers_count":19,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-10T02:35:16.526Z","etag":null,"topics":["checkbox","choice","choices","enquirer","inquirer","multi-select","prompt","select"],"latest_commit_sha":null,"homepage":"https://github.com/enquirer/enquirer","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enquirer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-29T00:41:15.000Z","updated_at":"2023-10-25T22:45:27.000Z","dependencies_parsed_at":"2022-10-04T16:34:32.137Z","dependency_job_id":null,"html_url":"https://github.com/enquirer/prompt-choices","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Fprompt-choices","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Fprompt-choices/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Fprompt-choices/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enquirer%2Fprompt-choices/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enquirer","download_url":"https://codeload.github.com/enquirer/prompt-choices/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161272,"owners_count":21057555,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["checkbox","choice","choices","enquirer","inquirer","multi-select","prompt","select"],"created_at":"2024-11-14T05:26:16.289Z","updated_at":"2025-10-26T09:45:38.957Z","avatar_url":"https://github.com/enquirer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# prompt-choices [![NPM version](https://img.shields.io/npm/v/prompt-choices.svg?style=flat)](https://www.npmjs.com/package/prompt-choices) [![NPM monthly downloads](https://img.shields.io/npm/dm/prompt-choices.svg?style=flat)](https://npmjs.org/package/prompt-choices) [![NPM total downloads](https://img.shields.io/npm/dt/prompt-choices.svg?style=flat)](https://npmjs.org/package/prompt-choices) [![Linux Build Status](https://img.shields.io/travis/enquirer/prompt-choices.svg?style=flat\u0026label=Travis)](https://travis-ci.org/enquirer/prompt-choices)\n\n\u003e Create an array of multiple choice objects for use in prompts.\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save prompt-choices\n```\n\n## Usage\n\n```js\nvar Choices = require('prompt-choices');\nvar choices = new Choices(['foo', 'bar', 'baz']);\n```\n\n## API\n\n### [Choices](index.js#L21)\n\nCreate a new `Choices` collection.\n\n**Params**\n\n* `choices` **{Array}**: One or more `choice` strings or objects.\n\n**Example**\n\n```js\nconst choices = new Choices(['foo', 'bar', 'baz']);\nconst choices = new Choices([{name: 'foo'}, {name: 'bar'}, {name: 'baz'}]);\n```\n\n### [.render](index.js#L52)\n\nRender choices.\n\n**Params**\n\n* `position` **{Number}**: Cursor position\n* `options` **{Object}**\n* `returns` **{String}**\n\n### [.renderChoice](index.js#L90)\n\nRender a specific choice. This can be overridden in prompts.\n\n**Params**\n\n* `choice` **{Object}**\n* `position` **{Number}**\n* `options` **{Object}**\n* `returns` **{String}**: Returns the line to render.\n\n**Example**\n\n```js\nchoices.render = function(choice, position, options) {\n  // do custom logic\n  return '';\n};\n```\n\n### [.choice](index.js#L105)\n\nCreate a new `Choice` object.\n\n**Params**\n\n* `val` **{String|Object}**\n* `returns` **{Object}**: Returns a choice object.\n\n**Example**\n\n```js\nchoices.choice('blue');\n```\n\n### [.toChoice](index.js#L123)\n\nReturns a normalized `choice` object.\n\n**Params**\n\n* `choice` **{Object|String}**\n* `returns` **{Object}**\n\n**Example**\n\n```js\nchoices.toChoice('foo');\nchoices.toChoice({name: 'foo'});\n```\n\n### [.addChoice](index.js#L143)\n\nAdd a normalized `choice` object to the `choices` array.\n\n**Params**\n\n* `choice` **{string|Object}**: One or more choices to add.\n\n**Example**\n\n```js\nchoices.addChoice(['foo', 'bar', 'baz']);\n```\n\n### [.addChoices](index.js#L173)\n\nAdd an array of normalized `choice` objects to the `choices` array. This method is called in the constructor, but it can also be used to add choices after instantiation.\n\n**Params**\n\n* `choices` **{Array|Object}**: One or more choices to add.\n\n**Example**\n\n```js\nchoices.addChoices(['foo', 'bar', 'baz']);\n```\n\n### [.toGroups](index.js#L206)\n\nCreate choice \"groups\" from the given choices object. ![choice groups](docs/prompt-groups.gif).\n\n**Params**\n\n* `choices` **{Object}**: (required) The value of each object must be an array of choices (strings or objects).\n* `returns` **{Array}**: Returns an array of normalized choice objects.\n\n**Example**\n\n```js\nchoices.toGroups({\n  foo: ['a', 'b', 'c'],\n  bar: ['d', 'e', 'f']\n});\n```\n\n### [.separator](index.js#L282)\n\nCreate a new `Separator` object. See [choices-separator](https://github.com/enquirer/choices-separator) for more details.\n\n**Params**\n\n* `separator` **{String}**: Optionally pass a string to use as the separator.\n* `returns` **{Object}**: Returns a separator object.\n\n**Example**\n\n```js\nchoices.separator();\n```\n\n### [.hasChoice](index.js#L298)\n\nReturns true if a choice exists.\n\n**Params**\n\n* `val` **{Number}**: The index or key of the choice to check for.\n* `returns` **{Boolean}**\n\n**Example**\n\n```js\nchoices.hasChoice(1);\nchoices.hasChoice('foo');\n```\n\n### [.getChoice](index.js#L314)\n\nGet a non-separator choice from the collection.\n\n**Params**\n\n* `idx` **{Number}**: The selected choice index\n* `returns` **{Object|undefined}**: Return the matched choice object or undefined\n\n**Example**\n\n```js\nchoices.getChoice(1);\nchoices.getChoice('foo');\n```\n\n### [.getIndex](index.js#L341)\n\nGet the index of a non-separator choice from the collection.\n\n**Params**\n\n* `key` **{String}**: The key of the choice to get\n* `returns` **{Number}**: Index of the choice or `-1`;\n\n**Example**\n\n```js\nconst choices = new Choices(['foo', 'bar', 'baz']);\nconsole.log(choices.getIndex('foo')); //=\u003e 0\nconsole.log(choices.getIndex('baz')); //=\u003e 2\nconsole.log(choices.getIndex('bar')); //=\u003e 1\nconsole.log(choices.getIndex('qux')); //=\u003e -1\n```\n\n### [.get](index.js#L365)\n\nGet the choice at the specified index.\n\n**Params**\n\n* `key` **{Number|String}**: The name or index of the object to get\n* `returns` **{Object}**: Returns the specified choice\n\n**Example**\n\n```js\nconst choice = choices.get(1);\n//=\u003e {name: 'foo'}\nconst choice = choices.get(1, 'name');\n//=\u003e 'foo'\n```\n\n### [.clear](index.js#L389)\n\nClear all choices from the instance. This is useful when you need to update the indices of choices without re-instantiating.\n\n**Example**\n\n```js\nchoices.clear();\n```\n\n### [.key](index.js#L403)\n\nReturn the `.key` property from the choice at the given index.\n\n**Params**\n\n* `key` **{String}**: Property name to use for plucking objects.\n* `returns` **{Array}**: Plucked objects\n\n### [.check](index.js#L417)\n\nCheck the choice at the given `idx`.\n\n**Params**\n\n* `val` **{Number|Array}**: The key(s) or index(s) of the choice(s) to check.\n\n**Example**\n\n```js\nchoices.check(1);\n```\n\n### [.uncheck](index.js#L442)\n\nDisable the choice at the given `idx`.\n\n**Params**\n\n* `idx` **{Number}**: The index of the choice to enable.\n\n**Example**\n\n```js\nchoices.uncheck(1);\n```\n\n### [.isChecked](index.js#L473)\n\nReturns true if a choice is checked.\n\n**Params**\n\n* `name` **{String|Number}**: Name or index of the choice.\n* `returns` **{Boolean}**\n\n**Example**\n\n```js\nconst choices = new Choices(['foo', 'bar', 'baz']);\nconsole.log(choices.isChecked('foo'));\n//=\u003e false\nchoices.check('foo');\nconsole.log(choices.isChecked('foo'));\n//=\u003e true\n```\n\n### [.toggle](index.js#L501)\n\nToggle the choice at the given `idx`.\n\n**Params**\n\n* `idx` **{Number}**: The index of the choice to toggle.\n\n**Example**\n\n```js\nchoices.toggle(1);\n// radio mode\nchoices.toggle(1, true);\n```\n\n### [.swap](index.js#L609)\n\nSwap two choices in the choices array.\n\n**Params**\n\n* `a` **{String|Number}**\n* `b` **{String|Number}**\n* `returns` **{Object}**: Returns the `Choices` instance\n\n### [.where](index.js#L625)\n\nReturn choice values for choices that return truthy based\non the given `val`.\n\n**Params**\n\n* `val` **{Array|Object|Function|String|RegExp}**\n* `returns` **{Array}**: Matching choices or empty array\n\n### [.isItem](index.js#L673)\n\nReturns true if the given `choice` is a valid choice item, and\nnot a \"group\" or \"radio\" choice.\n\n**Params**\n\n* `key` **{String}**: Property name to use for plucking objects.\n* `returns` **{Array}**: Plucked objects\n\n### [.isValidIndex](index.js#L688)\n\nReturns true if the given `index` is a valid choice index.\n\n**Params**\n\n* `key` **{String}**: Property name to use for plucking objects.\n* `returns` **{Array}**: Plucked objects\n\n### [.pluck](index.js#L699)\n\nPluck an object with the specified key from the choices collection.\n\n**Params**\n\n* `key` **{String}**: Property name to use for plucking objects.\n* `returns` **{Array}**: Plucked objects\n\n### [.default](index.js#L735)\n\nGetter for getting the default choice.\n\n### [.checked](index.js#L762)\n\nGetter for getting the checked choices from the collection.\n\n### [.length](index.js#L804)\n\nGetter for getting the length of the collection.\n\n### [.Separator](index.js#L824)\n\nCreate a new `Separator` object. See [choices-separator](https://github.com/enquirer/choices-separator) for more details.\n\n**Params**\n\n* `separator` **{String}**: Optionally pass a string to use as the separator.\n* `returns` **{Object}**: Returns a separator object.\n\n**Example**\n\n```js\nnew Choices.Separator();\n```\n\n### [.isChoices](index.js#L840)\n\nCreate a new `Separator` object. See [choices-separator](https://github.com/enquirer/choices-separator) for more details.\n\n**Params**\n\n* `choices` **{String}**: The value to test.\n* `returns` **{Boolean}**: Returns true if the given value is an instance of `Choices`.\n\n**Example**\n\n```js\nconst Choices = require('prompt-choices');\nconst choices = new Choices(['foo']);\nconsole.log(Choices.isChoices(choices)); //=\u003e true\nconsole.log(Choices.isChoices({})); //=\u003e false\n```\n\n### [.isChoice](index.js#L859)\n\nCreate a new `Separator` object. See [choices-separator](https://github.com/enquirer/choices-separator) for more details.\n\n**Params**\n\n* `choice` **{String}**: The value to test.\n* `returns` **{Boolean}**: Returns true if the given value is an instance of `Choice`.\n\n**Example**\n\n```js\nconst Choices = require('prompt-choices');\nconst choices = new Choices(['foo']);\nconst foo = choices.getChoice('foo');\nconsole.log(Choices.isChoice(foo)); //=\u003e true\nconsole.log(Choices.isChoice({})); //=\u003e false\n```\n\n## Release history\n\n### v3.0.2\n\n**Added**\n\n* adds array support to `.isChecked`\n\n**Fixed**\n\n* ensures that choice groups are checked/unchecked based on group items\n\n### v3.0.0\n\n**Added**\n\n* adds support for choice \"groups\"! This allows you to define an object of choice arrays, where each key in the object creates a choice group.\n\n### v2.0.0\n\n**Changed**\n\n* renamed `Move` class to `Actions`\n* renamed `choices.move` property to `choices.actions`\n\n**Removed**\n\n* removed `.enable` and `.disable` prototype methods from both `Choice` and `Choices`. These methods were ambiguous as they blurred the distinction between \"enabling\" a choice (meaning that it's \"checked\") versus enabling a property on a choice. If this is confusing, that's why they were removed.\n\n**Added**\n\n* adds `Actions` class (previously named `Move`) for managing actions on choices\n* adds `.addChoice` prototype method, for adding a single choice after instantiation\n* adds `.action` prototype method to `Choices`, which calls a method on the `Actions` class\n* adds `.check` and `.uncheck` prototype methods (previously ambiguously named `.enable` and `.disable`)\n\n## Attribution\n\nSome of the code in this library was initially based on the `Choices` class in Inquirer.\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Related projects\n\nYou might also be interested in these projects:\n\n* [enquirer](https://www.npmjs.com/package/enquirer): Intuitive, plugin-based prompt system for node.js. | [homepage](http://enquirer.io \"Intuitive, plugin-based prompt system for node.js.\")\n* [prompt-base](https://www.npmjs.com/package/prompt-base): Base prompt module used for creating custom prompts. | [homepage](https://github.com/enquirer/prompt-base \"Base prompt module used for creating custom prompts.\")\n* [prompt-checkbox](https://www.npmjs.com/package/prompt-checkbox): Multiple-choice/checkbox prompt. Can be used standalone or with a prompt system like [Enquirer]. | [homepage](https://github.com/enquirer/prompt-checkbox \"Multiple-choice/checkbox prompt. Can be used standalone or with a prompt system like [Enquirer].\")\n* [prompt-question](https://www.npmjs.com/package/prompt-question): Question object, used by Enquirer and prompt plugins. | [homepage](https://github.com/enquirer/prompt-question \"Question object, used by Enquirer and prompt plugins.\")\n* [prompt-radio](https://www.npmjs.com/package/prompt-radio): Radio prompt. Can be used as a standalone prompt, or as a plugin for [Enquirer]. | [homepage](https://github.com/enquirer/prompt-radio \"Radio prompt. Can be used as a standalone prompt, or as a plugin for [Enquirer].\")\n\n### Author\n\n**Jon Schlinkert**\n\n* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)\n* [GitHub Profile](https://github.com/jonschlinkert)\n* [Twitter Profile](https://twitter.com/jonschlinkert)\n\n### License\n\nCopyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 28, 2018._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenquirer%2Fprompt-choices","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenquirer%2Fprompt-choices","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenquirer%2Fprompt-choices/lists"}