Socket
Socket
Sign inDemoInstall

prompt-choices

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prompt-choices - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

181

index.js
'use strict';
var toggleArray = require('toggle-array');
var Separator = require('choices-separator');

@@ -8,11 +9,33 @@ var Choice = require('./lib/choice');

/**
* Choices collection
* Collection of multiple `choice` objects
* @param {Array} choices All `choice` to keep in the collection
* Create a new `Choices` collection.
*
* ```js
* var choices = new Choices(['foo', 'bar', 'baz']);
* var choices = new Choices([{name: 'foo'}, {name: 'bar'}, {name: 'baz'}]);
* ```
* @param {Array} `choices` One or more `choice` strings or objects.
* @api public
*/
function Choices(choices, answers) {
this.answers = answers;
this.choices = [];
this.checked = {};
this.keymap = {};
this.addChoices(choices);
}
/**
* Add 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.
*
* ```js
* choices.addChoices(['a', 'b', 'c']);
* ```
* @param {Array|Object} `choices` One or more choices to add.
* @api public
*/
Choices.prototype.addChoices = function(choices) {
choices = utils.arrayify(choices);

@@ -29,5 +52,6 @@ var len = choices.length;

} else {
choice = new Choice(choice, answers);
choice = new Choice(choice, this.answers);
this.keymap[choice.key] = choice;
}
// push normalized "choice" object onto array
this.choices.push(choice);

@@ -38,5 +62,10 @@ }

/**
* Get a valid choice from the collection
* @param {Number} `idx` The selected choice index
* Get a non-separator choice from the collection.
*
* ```js
* choices.getChoice(1);
* ```
* @param {Number} `idx` The selected choice index
* @return {Object|undefined} Return the matched choice object or undefined
* @api public
*/

@@ -54,5 +83,10 @@

/**
* Get a valid choice from the collection
* @param {Number} idx The selected choice index
* @return {Choice|Undefined} Return the matched choice or undefined
* Get the index of a non-separator choice from the collection.
*
* ```js
* choices.getChoice('foo');
* ```
* @param {String} `key` The key of the choice to get
* @return {Number} Index of the choice or `-1`;
* @api public
*/

@@ -67,9 +101,14 @@

}
return 0;
return -1;
};
/**
* Get a raw element from the collection
* @param {Number} idx The selected index value
* @return {Choice|Undefined} Return the matched choice or undefined
* Get the choice or separator object at the specified index.
*
* ```js
* choices.getChoice(1);
* ```
* @param {Number} `idx` The index of the object to get
* @return {Object} Returns the specified choice
* @api public
*/

@@ -85,15 +124,68 @@

/**
* Enable the choice at the given `idx`.
*
* ```js
* choices.enable(1);
* ```
* @param {Number} `idx` The index of the choice to enable.
* @api public
*/
Choices.prototype.enable = function(idx) {
this.getChoice(idx).checked = true;
return this;
};
/**
* Disable the choice at the given `idx`.
*
* ```js
* choices.disable(1);
* ```
* @param {Number} `idx` The index of the choice to enable.
* @api public
*/
Choices.prototype.disable = function(idx) {
this.getChoice(idx).checked = false;
return this;
};
/**
* Enable the choice at the given `index`, and disable all other choices.
*
* ```js
* choices.toggleChoices(1);
* ```
* @param {Number} `idx` The index of the choice to toggle.
* @api public
*/
Choices.prototype.toggleChoices = function(idx) {
toggleArray(this.choices, 'checked', idx);
return this;
};
/**
* Toggle the choice at the given `idx`.
* @param {Number} `idx` The index of the choice to toggle.
*
* ```js
* choices.toggleChoice(1);
* ```
* @param {Number} `idx` The index of the choice to toggle.
* @api public
*/
Choices.prototype.toggle = function(idx) {
var checked = this.getChoice(idx).checked;
this.getChoice(idx).checked = !checked;
Choices.prototype.toggleChoice = function(idx) {
var enabled = this.getChoice(idx).checked;
this.getChoice(idx).checked = !enabled;
return this;
};
/**
* Match the valid choices against a where clause
* @param {Object} whereClause Lodash `where` clause
* @return {Array} Matching choices or empty array
* Return choices that return truthy based on the given `val`.
*
* @param {Object|Function|String} `val`
* @return {Array} Matching choices or empty array
* @api public
*/

@@ -125,5 +217,6 @@

/**
* Pluck a particular key from the choices
* @param {String} propertyName Property name to select
* @return {Array} Selected properties
* Pluck an object with the specified key from the choices collection.
* @param {String} `key` Property name to use for plucking objects.
* @return {Array} Plucked objects
* @api public
*/

@@ -137,3 +230,2 @@

// Expose usual Array methods
Choices.prototype.indexOf = function() {

@@ -166,2 +258,8 @@ return this.choices.indexOf.apply(this.choices, arguments);

/**
* Getter for getting all non-separator choices from the collection.
* @name .realChoices
* @api public
*/
Object.defineProperty(Choices.prototype, 'realChoices', {

@@ -184,21 +282,44 @@ set: function() {

Object.defineProperty(Choices.prototype, 'length', {
/**
* Getter for getting the length of the collection excluding non-separator choices.
* @name .realLength
* @api public
*/
Object.defineProperty(Choices.prototype, 'realLength', {
set: function() {
throw new Error('.length is a getter and cannot be defined');
throw new Error('.realLength is a getter and cannot be defined');
},
get: function() {
return this.choices.length;
return this.realChoices.length;
}
});
Object.defineProperty(Choices.prototype, 'realLength', {
/**
* Getter for getting the length of the collection.
* @name .length
* @api public
*/
Object.defineProperty(Choices.prototype, 'length', {
set: function() {
throw new Error('.realLength is a getter and cannot be defined');
throw new Error('.length is a getter and cannot be defined');
},
get: function() {
return this.realChoices.length;
return this.choices.length;
}
});
/**
* Getter for getting all choices from the collection. Alias to allow using
* `.choices.all` instead of `.choices.choices`.
*
* @name .all
* @api public
*/
Object.defineProperty(Choices.prototype, 'all', {
set: function() {
throw new Error('.all is a getter and cannot be defined');
},
get: function() {

@@ -205,0 +326,0 @@ return this.choices;

7

lib/choice.js

@@ -12,2 +12,4 @@ 'use strict';

function Choice(choice, answers) {
this.enabled = false;
if (typeof choice === 'string') {

@@ -22,2 +24,3 @@ choice = { name: choice };

if (choice.type === 'separator' || choice.isSeparator) {
choice.isSeparator = true;
return choice;

@@ -32,5 +35,5 @@ }

if (typeof choice.disabled === 'function') {
this.disabled = choice.disabled.call(this, answers);
this.disabled = !!choice.disabled.call(this, answers);
} else {
this.disabled = choice.disabled;
this.disabled = !!choice.disabled;
}

@@ -37,0 +40,0 @@ };

{
"name": "prompt-choices",
"description": "Create an array of multiple choice objects for use in prompts.",
"version": "0.1.1",
"version": "0.2.0",
"homepage": "https://github.com/enquirer/prompt-choices",

@@ -33,3 +33,4 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"lazy-cache": "^2.0.1",
"log-utils": "^0.2.1"
"log-utils": "^0.2.1",
"toggle-array": "^0.1.0"
},

@@ -36,0 +37,0 @@ "devDependencies": {

@@ -20,2 +20,169 @@ # prompt-choices [![NPM version](https://img.shields.io/npm/v/prompt-choices.svg?style=flat)](https://www.npmjs.com/package/prompt-choices) [![NPM downloads](https://img.shields.io/npm/dm/prompt-choices.svg?style=flat)](https://npmjs.org/package/prompt-choices)

## API
### [Choices](index.js#L19)
Create a new `Choices` collection.
**Params**
* `choices` **{Array}**: One or more `choice` strings or objects.
**Example**
```js
var choices = new Choices(['foo', 'bar', 'baz']);
var choices = new Choices([{name: 'foo'}, {name: 'bar'}, {name: 'baz'}]);
```
### [.addChoices](index.js#L39)
Add 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.
**Params**
* `choices` **{Array|Object}**: One or more choices to add.
**Example**
```js
choices.addChoices(['a', 'b', 'c']);
```
### [.getChoice](index.js#L70)
Get a non-separator choice from the collection.
**Params**
* `idx` **{Number}**: The selected choice index
* `returns` **{Object|undefined}**: Return the matched choice object or undefined
**Example**
```js
choices.getChoice(1);
```
### [.getIndex](index.js#L90)
Get the index of a non-separator choice from the collection.
**Params**
* `key` **{String}**: The key of the choice to get
* `returns` **{Number}**: Index of the choice or `-1`;
**Example**
```js
choices.getChoice('foo');
```
### [.get](index.js#L111)
Get the choice or separator object at the specified index.
**Params**
* `idx` **{Number}**: The index of the object to get
* `returns` **{Object}**: Returns the specified choice
**Example**
```js
choices.getChoice(1);
```
### [.enable](index.js#L128)
Enable the choice at the given `idx`.
**Params**
* `idx` **{Number}**: The index of the choice to enable.
**Example**
```js
choices.enable(1);
```
### [.disable](index.js#L143)
Disable the choice at the given `idx`.
**Params**
* `idx` **{Number}**: The index of the choice to enable.
**Example**
```js
choices.disable(1);
```
### [.toggleChoices](index.js#L158)
Enable the choice at the given `index`, and disable all other choices.
**Params**
* `idx` **{Number}**: The index of the choice to toggle.
**Example**
```js
choices.toggleChoices(1);
```
### [.toggleChoice](index.js#L173)
Toggle the choice at the given `idx`.
**Params**
* `idx` **{Number}**: The index of the choice to toggle.
**Example**
```js
choices.toggleChoice(1);
```
### [.where](index.js#L187)
Return choices that return truthy based on the given `val`.
**Params**
* `val` **{Object|Function|String}**
* `returns` **{Array}**: Matching choices or empty array
### [.pluck](index.js#L217)
Pluck an object with the specified key from the choices collection.
**Params**
* `key` **{String}**: Property name to use for plucking objects.
* `returns` **{Array}**: Plucked objects
### [.realChoices](index.js#L256)
Getter for getting all non-separator choices from the collection.
### [.realLength](index.js#L279)
Getter for getting the length of the collection excluding non-separator choices.
### [.length](index.js#L294)
Getter for getting the length of the collection.
### [.all](index.js#L311)
Getter for getting all choices from the collection. Alias to allow using
`.choices.all` instead of `.choices.choices`.
## Attribution

@@ -22,0 +189,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc