prompt-question
Advanced tools
Comparing version 1.0.0 to 2.0.0
63
index.js
'use strict'; | ||
var debug = require('debug')('prompt-question'); | ||
var Choices = require('prompt-choices'); | ||
var koalas = require('koalas'); | ||
var Choices = require('prompt-choices'); | ||
var utils = require('./lib/utils'); | ||
@@ -39,2 +39,11 @@ | ||
if (Array.isArray(message)) { | ||
options = { choices: message }; | ||
message = name; | ||
} | ||
if (Array.isArray(options)) { | ||
options = { choices: options }; | ||
} | ||
utils.define(this, 'Choices', Choices); | ||
@@ -60,4 +69,4 @@ utils.define(this, 'isQuestion', true); | ||
Question.prototype.clone = function() { | ||
var cached = utils.clone(this.cache); | ||
return new this.constructor(cached); | ||
var cache = utils.clone(this.cache); | ||
return new this.constructor(cache); | ||
}; | ||
@@ -77,4 +86,4 @@ | ||
Question.prototype.addChoices = function(choices) { | ||
utils.define(this, '_choices', new Choices(utils.arrayify(choices), this)); | ||
Question.prototype.addChoices = function() { | ||
this.choices.addChoices.apply(this.choices, arguments); | ||
return this; | ||
@@ -84,10 +93,9 @@ }; | ||
/** | ||
* Toggle the `checked` value of the the choice at the given `idx`. | ||
* Add a choice to `question.choices` array. | ||
* See [prompt-choices][] for more details. | ||
* | ||
* ```js | ||
* question.toggle(1); | ||
* // "radio" mode | ||
* question.toggle(1, true); | ||
* question.addChoice('foo'); | ||
* ``` | ||
* @param {Number} `idx` The index of the choice to toggle. | ||
* @param {String|Object} `choice` | ||
* @return {Object} Returns the question instance for chaining | ||
@@ -97,6 +105,4 @@ * @api public | ||
Question.prototype.toggle = function(idx, radio) { | ||
if (typeof this.choices.toggle === 'function') { | ||
this.choices.toggle(idx, radio); | ||
} | ||
Question.prototype.addChoice = function() { | ||
this.choices.addChoice.apply(this.choices, arguments); | ||
return this; | ||
@@ -152,3 +158,3 @@ }; | ||
Question.prototype.getChoice = function() { | ||
return this.choices.getChoice.apply(this.choices, arguments); | ||
return this.choices.get.apply(this.choices, arguments); | ||
}; | ||
@@ -180,3 +186,4 @@ | ||
/** | ||
* Getter that returns the list of choices for the current question, if applicable. | ||
* Getter that returns the list of choices for the | ||
* current question, if applicable. | ||
* | ||
@@ -189,6 +196,11 @@ * @name .choices | ||
Object.defineProperty(Question.prototype, 'choices', { | ||
configurable: true, | ||
enumerable: true, | ||
set: function(choices) { | ||
this.addChoices(choices); | ||
this._choices = new Choices(choices, this.options); | ||
}, | ||
get: function() { | ||
if (typeof this._choices === 'undefined') { | ||
this._choices = new Choices(this.options.choices, this.options); | ||
} | ||
return this._choices; | ||
@@ -211,5 +223,20 @@ } | ||
/** | ||
* Create a new `Separator` object. See [choices-separator][] for more details. | ||
* Create a new `Choices` object. See [prompt-choices][] | ||
* for more details. | ||
* | ||
* ```js | ||
* var choices = new Question.Choices(['foo', 'bar', 'baz']); | ||
* ``` | ||
* @param {Array} `choices` Array of choices | ||
* @return {Object} Returns an intance of Choices. | ||
* @api public | ||
*/ | ||
Question.Choices = Choices; | ||
/** | ||
* Create a new `Separator` object. See [choices-separator][] | ||
* for more details. | ||
* | ||
* ```js | ||
* new Question.Separator(); | ||
@@ -216,0 +243,0 @@ * ``` |
@@ -22,3 +22,2 @@ 'use strict'; | ||
utils.assign = function(obj, options) { | ||
options = options || {}; | ||
var cache = {}; | ||
@@ -47,55 +46,2 @@ for (var key in options) { | ||
/** | ||
* Cast `val` to an array. | ||
*/ | ||
utils.arrayify = function(val) { | ||
return val ? (Array.isArray(val) ? val : [val]) : []; | ||
}; | ||
/** | ||
* Return true if `obj` contains the given `key` | ||
*/ | ||
utils.has = function(obj, key) { | ||
return utils.isObject(obj) && obj.hasOwnProperty(key); | ||
}; | ||
/** | ||
* Return true if `arr` contains any of the given `keys` | ||
*/ | ||
utils.hasAny = function(obj, keys) { | ||
if (!utils.isObject(obj)) { | ||
return false; | ||
} | ||
keys = utils.arrayify(keys); | ||
for (var i = 0; i < keys.length; i++) { | ||
if (utils.has(obj, keys[i])) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
/** | ||
* Return true if `obj` is a Question object | ||
*/ | ||
utils.isQuestion = function(obj) { | ||
if (!utils.isObject(obj) || !obj.name || !obj.type) { | ||
return false; | ||
} | ||
return utils.hasAny(obj, [ | ||
'message', | ||
'default', | ||
'choices', | ||
'filter', | ||
'paginated', | ||
'validate', | ||
'when' | ||
]); | ||
}; | ||
/** | ||
* Expose `utils` modules | ||
@@ -102,0 +48,0 @@ */ |
{ | ||
"name": "prompt-question", | ||
"description": "Question object, used by Enquirer and prompt plugins.", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/enquirer/prompt-question", | ||
@@ -31,3 +31,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"lazy-cache": "^2.0.2", | ||
"prompt-choices": "^1.0.0" | ||
"prompt-choices": "^2.0.0" | ||
}, | ||
@@ -90,3 +90,5 @@ "devDependencies": { | ||
"verb", | ||
"verb-generate-readme" | ||
"verb-generate-readme", | ||
"base-prompt", | ||
"prompt-choices" | ||
], | ||
@@ -93,0 +95,0 @@ "lint": { |
@@ -13,8 +13,2 @@ # prompt-question [![NPM version](https://img.shields.io/npm/v/prompt-question.svg?style=flat)](https://www.npmjs.com/package/prompt-question) [![NPM monthly downloads](https://img.shields.io/npm/dm/prompt-question.svg?style=flat)](https://npmjs.org/package/prompt-question) [![NPM total downloads](https://img.shields.io/npm/dt/prompt-question.svg?style=flat)](https://npmjs.org/package/prompt-question) [![Linux Build Status](https://img.shields.io/travis/enquirer/prompt-question.svg?style=flat&label=Travis)](https://travis-ci.org/enquirer/prompt-question) | ||
Install with [yarn](https://yarnpkg.com): | ||
```sh | ||
$ yarn add prompt-question | ||
``` | ||
## Usage | ||
@@ -41,2 +35,8 @@ | ||
## Release history | ||
### v2.0.0 | ||
* bumps [prompt-choices](https://github.com/enquirer/prompt-choices). A major bump was warranted due to potentially breaking changes in prompt-choices. Please see that library for more details. | ||
## About | ||
@@ -89,2 +89,2 @@ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 29, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 10, 2017._ |
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
13524
253
+ Addedchoices-separator@1.1.0(transitive)
+ Addedcollection-visit@1.0.0(transitive)
+ Addedmap-visit@1.0.0(transitive)
+ Addedobject-visit@1.0.1(transitive)
+ Addedprompt-choices@2.0.0(transitive)
+ Addedterminal-paginator@1.1.0(transitive)
+ Addedtoggle-array@1.0.1(transitive)
- Removedchoices-separator@0.1.2(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisobject@2.1.0(transitive)
- Removedprompt-choices@1.0.1(transitive)
- Removedterminal-paginator@0.2.2(transitive)
- Removedtoggle-array@0.1.0(transitive)
Updatedprompt-choices@^2.0.0