prompt-choices
Advanced tools
Comparing version 0.2.1 to 0.3.0
127
index.js
@@ -19,6 +19,10 @@ 'use strict'; | ||
function Choices(choices) { | ||
this.choices = []; | ||
this.checked = {}; | ||
function Choices(choices, answers) { | ||
choices = choices || []; | ||
utils.define(this, 'isChoices', true); | ||
utils.define(this, 'answers', answers || {}); | ||
this.original = choices.slice(); | ||
this.keymap = {}; | ||
this.items = []; | ||
this.keys = []; | ||
this.addChoices(choices); | ||
@@ -51,7 +55,9 @@ } | ||
} else { | ||
choice = new Choice(choice); | ||
this.keymap[choice.key || choice.name] = choice; | ||
choice = this.choice(choice); | ||
var key = choice.key || choice.name; | ||
this.keymap[key] = choice; | ||
this.keys.push(key); | ||
} | ||
// push normalized "choice" object onto array | ||
this.choices.push(choice); | ||
this.push(choice); | ||
} | ||
@@ -61,2 +67,32 @@ }; | ||
/** | ||
* Create a new `Choice` object. | ||
* | ||
* ```js | ||
* choices.choice('blue'); | ||
* ``` | ||
* @param {String|Object} `choice` | ||
* @return {Object} Returns a choice object. | ||
* @api public | ||
*/ | ||
Choices.prototype.choice = function(choice) { | ||
return new Choice(choice, this.answers); | ||
}; | ||
/** | ||
* Create a new `Separator` object. See [choices-separator][] for more details. | ||
* | ||
* ```js | ||
* choices.separator(); | ||
* ``` | ||
* @param {String} `separator` Optionally pass a string to use as the separator. | ||
* @return {Object} Returns a separator object. | ||
* @api public | ||
*/ | ||
Choices.prototype.separator = function(separator, options) { | ||
return new Separator(separator, options); | ||
}; | ||
/** | ||
* Get a non-separator choice from the collection. | ||
@@ -117,3 +153,3 @@ * | ||
} | ||
return this.choices[idx]; | ||
return this.items[idx]; | ||
}; | ||
@@ -162,3 +198,3 @@ | ||
Choices.prototype.toggleChoices = function(idx) { | ||
toggleArray(this.choices, 'checked', idx); | ||
toggleArray(this.items, 'checked', idx); | ||
return this; | ||
@@ -178,4 +214,4 @@ }; | ||
Choices.prototype.toggleChoice = function(idx) { | ||
var enabled = this.getChoice(idx).checked; | ||
this.getChoice(idx).checked = !enabled; | ||
var checked = this.getChoice(idx).checked; | ||
this.getChoice(idx).checked = !checked; | ||
return this; | ||
@@ -197,6 +233,11 @@ }; | ||
} | ||
if (typeof val === 'string') { | ||
return !!choice[val]; | ||
return choice.name === val || choice.key === val; | ||
} | ||
if (val instanceof RegExp) { | ||
return val.test(choice.name) || val.test(choice.key); | ||
} | ||
if (utils.isObject(val)) { | ||
@@ -229,12 +270,16 @@ for (var key in val) { | ||
/** | ||
* Convenience array methods | ||
*/ | ||
Choices.prototype.indexOf = function() { | ||
return this.choices.indexOf.apply(this.choices, arguments); | ||
return this.getChoice(this.keys.indexOf.apply(this.keys, arguments)); | ||
}; | ||
Choices.prototype.forEach = function() { | ||
return this.choices.forEach.apply(this.choices, arguments); | ||
return this.items.forEach.apply(this.items, arguments); | ||
}; | ||
Choices.prototype.filter = function() { | ||
return this.choices.filter.apply(this.choices, arguments); | ||
return this.items.filter.apply(this.items, arguments); | ||
}; | ||
@@ -249,3 +294,3 @@ | ||
var choice = choices[idx]; | ||
this.choices.push(new Choice(choice)); | ||
this.items.push(new Choice(choice)); | ||
if (choice.type !== 'separator') { | ||
@@ -255,6 +300,21 @@ this.realChoices.push(choice); | ||
} | ||
return this.choices; | ||
return this.items; | ||
}; | ||
/** | ||
* Getter for getting the length of the collection. | ||
* @name .length | ||
* @api public | ||
*/ | ||
Object.defineProperty(Choices.prototype, 'length', { | ||
set: function() { | ||
throw new Error('.length is a getter and cannot be defined'); | ||
}, | ||
get: function() { | ||
return this.items.length; | ||
} | ||
}); | ||
/** | ||
* Getter for getting all non-separator choices from the collection. | ||
@@ -273,3 +333,3 @@ * @name .realChoices | ||
while (++idx < this.length) { | ||
var choice = this.choices[idx]; | ||
var choice = this.items[idx]; | ||
if (choice.type !== 'separator' && !choice.disabled) { | ||
@@ -299,33 +359,18 @@ choices.push(choice); | ||
/** | ||
* Getter for getting the length of the collection. | ||
* @name .length | ||
* Create a new `Separator` object. See [choices-separator][] for more details. | ||
* | ||
* ```js | ||
* new Choices.Separator(); | ||
* ``` | ||
* @param {String} `separator` Optionally pass a string to use as the separator. | ||
* @return {Object} Returns a separator object. | ||
* @api public | ||
*/ | ||
Object.defineProperty(Choices.prototype, 'length', { | ||
set: function() { | ||
throw new Error('.length is a getter and cannot be defined'); | ||
}, | ||
get: function() { | ||
return this.choices.length; | ||
} | ||
}); | ||
Choices.Separator = Separator; | ||
/** | ||
* Getter for getting all choices from the collection. Alias to allow using | ||
* `.choices.all` instead of `.choices.choices`. | ||
* | ||
* @name .all | ||
* @api public | ||
* Expose `Choices` | ||
*/ | ||
Object.defineProperty(Choices.prototype, 'all', { | ||
set: function() { | ||
throw new Error('.all is a getter and cannot be defined'); | ||
}, | ||
get: function() { | ||
return this.choices; | ||
} | ||
}); | ||
module.exports = Choices; |
'use strict'; | ||
var util = require('util'); | ||
var utils = require('./utils'); | ||
@@ -11,5 +12,3 @@ | ||
function Choice(choice) { | ||
this.enabled = false; | ||
function Choice(choice, answers) { | ||
if (typeof choice === 'string') { | ||
@@ -28,2 +27,10 @@ choice = { name: choice }; | ||
if (choice.isChoice || choice instanceof Choice) { | ||
return choice; | ||
} | ||
utils.define(this, 'isChoice', true); | ||
this.disabled = false; | ||
this.checked = false; | ||
utils.extend(this, choice); | ||
@@ -35,3 +42,3 @@ this.name = choice.name || choice.value; | ||
if (typeof choice.disabled === 'function') { | ||
this.disabled = !!choice.disabled.call(this); | ||
this.disabled = !!choice.disabled.call(this, answers); | ||
} else { | ||
@@ -38,0 +45,0 @@ this.disabled = !!choice.disabled; |
@@ -16,3 +16,2 @@ 'use strict'; | ||
require('isobject', 'isObject'); | ||
require('log-utils', 'log'); | ||
require = fn; | ||
@@ -38,10 +37,2 @@ | ||
/** | ||
* Return true if `obj` contains the given `key` | ||
*/ | ||
utils.has = function(obj, key) { | ||
return utils.isObject(obj) && obj.hasOwnProperty(key); | ||
}; | ||
/** | ||
* Expose `utils` modules | ||
@@ -48,0 +39,0 @@ */ |
{ | ||
"name": "prompt-choices", | ||
"description": "Create an array of multiple choice objects for use in prompts.", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/enquirer/prompt-choices", | ||
@@ -33,3 +33,2 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"lazy-cache": "^2.0.1", | ||
"log-utils": "^0.2.1", | ||
"toggle-array": "^0.1.0" | ||
@@ -43,3 +42,4 @@ }, | ||
"gulp-mocha": "^3.0.1", | ||
"gulp-unused": "^0.2.0" | ||
"gulp-unused": "^0.2.0", | ||
"mocha": "^3.0.2" | ||
}, | ||
@@ -46,0 +46,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
# 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) | ||
# 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) [![Build Status](https://img.shields.io/travis/enquirer/prompt-choices.svg?style=flat)](https://travis-ci.org/enquirer/prompt-choices) | ||
@@ -37,3 +37,3 @@ > Create an array of multiple choice objects for use in prompts. | ||
### [.addChoices](index.js#L39) | ||
### [.addChoices](index.js#L42) | ||
@@ -52,4 +52,34 @@ 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. | ||
### [.getChoice](index.js#L70) | ||
### [.choice](index.js#L75) | ||
Create a new `Choice` object. | ||
**Params** | ||
* `choice` **{String|Object}** | ||
* `returns` **{Object}**: Returns a choice object. | ||
**Example** | ||
```js | ||
choices.choice('blue'); | ||
``` | ||
### [.separator](index.js#L90) | ||
Create a new `Separator` object. See [choices-separator](https://github.com/enquirer/choices-separator) for more details. | ||
**Params** | ||
* `separator` **{String}**: Optionally pass a string to use as the separator. | ||
* `returns` **{Object}**: Returns a separator object. | ||
**Example** | ||
```js | ||
choices.separator(); | ||
``` | ||
### [.getChoice](index.js#L105) | ||
Get a non-separator choice from the collection. | ||
@@ -68,3 +98,3 @@ | ||
### [.getIndex](index.js#L90) | ||
### [.getIndex](index.js#L125) | ||
@@ -84,3 +114,3 @@ Get the index of a non-separator choice from the collection. | ||
### [.get](index.js#L111) | ||
### [.get](index.js#L146) | ||
@@ -100,3 +130,3 @@ Get the choice or separator object at the specified index. | ||
### [.enable](index.js#L128) | ||
### [.enable](index.js#L163) | ||
@@ -115,3 +145,3 @@ Enable the choice at the given `idx`. | ||
### [.disable](index.js#L143) | ||
### [.disable](index.js#L178) | ||
@@ -130,3 +160,3 @@ Disable the choice at the given `idx`. | ||
### [.toggleChoices](index.js#L158) | ||
### [.toggleChoices](index.js#L193) | ||
@@ -145,3 +175,3 @@ Enable the choice at the given `index`, and disable all other choices. | ||
### [.toggleChoice](index.js#L173) | ||
### [.toggleChoice](index.js#L208) | ||
@@ -160,3 +190,3 @@ Toggle the choice at the given `idx`. | ||
### [.where](index.js#L187) | ||
### [.where](index.js#L222) | ||
@@ -170,3 +200,3 @@ Return choices that return truthy based on the given `val`. | ||
### [.pluck](index.js#L217) | ||
### [.pluck](index.js#L257) | ||
@@ -180,19 +210,29 @@ Pluck an object with the specified key from the choices collection. | ||
### [.realChoices](index.js#L256) | ||
### [.length](index.js#L300) | ||
Getter for getting the length of the collection. | ||
### [.realChoices](index.js#L315) | ||
Getter for getting all non-separator choices from the collection. | ||
### [.realLength](index.js#L279) | ||
### [.realLength](index.js#L338) | ||
Getter for getting the length of the collection excluding non-separator choices. | ||
### [.length](index.js#L294) | ||
### [.Separator](index.js#L358) | ||
Getter for getting the length of the collection. | ||
Create a new `Separator` object. See [choices-separator](https://github.com/enquirer/choices-separator) for more details. | ||
### [.all](index.js#L311) | ||
**Params** | ||
Getter for getting all choices from the collection. Alias to allow using | ||
`.choices.all` instead of `.choices.choices`. | ||
* `separator` **{String}**: Optionally pass a string to use as the separator. | ||
* `returns` **{Object}**: Returns a separator object. | ||
**Example** | ||
```js | ||
new Choices.Separator(); | ||
``` | ||
## Attribution | ||
@@ -240,2 +280,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.30, on August 29, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.30, on August 30, 2016._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18652
8
386
269
7
- Removedlog-utils@^0.2.1
- Removedansi-bgblack@0.1.1(transitive)
- Removedansi-bgblue@0.1.1(transitive)
- Removedansi-bgcyan@0.1.1(transitive)
- Removedansi-bggreen@0.1.1(transitive)
- Removedansi-bgmagenta@0.1.1(transitive)
- Removedansi-bgred@0.1.1(transitive)
- Removedansi-bgwhite@0.1.1(transitive)
- Removedansi-bgyellow@0.1.1(transitive)
- Removedansi-black@0.1.1(transitive)
- Removedansi-blue@0.1.1(transitive)
- Removedansi-bold@0.1.1(transitive)
- Removedansi-colors@0.2.0(transitive)
- Removedansi-cyan@0.1.1(transitive)
- Removedansi-dim@0.1.1(transitive)
- Removedansi-green@0.1.1(transitive)
- Removedansi-grey@0.1.1(transitive)
- Removedansi-hidden@0.1.1(transitive)
- Removedansi-inverse@0.1.1(transitive)
- Removedansi-italic@0.1.1(transitive)
- Removedansi-magenta@0.1.1(transitive)
- Removedansi-red@0.1.1(transitive)
- Removedansi-reset@0.1.1(transitive)
- Removedansi-strikethrough@0.1.1(transitive)
- Removedansi-underline@0.1.1(transitive)
- Removedansi-white@0.1.1(transitive)
- Removedansi-yellow@0.1.1(transitive)
- Removederror-symbol@0.1.0(transitive)
- Removedinfo-symbol@0.1.0(transitive)
- Removedlog-ok@0.1.1(transitive)
- Removedlog-utils@0.2.1(transitive)
- Removedsuccess-symbol@0.1.0(transitive)
- Removedtime-stamp@1.1.0(transitive)
- Removedwarning-symbol@0.1.0(transitive)