prompt-choices
Advanced tools
Comparing version 0.4.0 to 0.4.1
78
index.js
@@ -164,9 +164,6 @@ 'use strict'; | ||
Choices.prototype.getIndex = function(key) { | ||
if (utils.isNumber(key) && key !== -1 && key < this.items.length) { | ||
return key; | ||
} | ||
if (typeof key === 'string') { | ||
return this.pluck('value').indexOf(key); | ||
} | ||
return -1; | ||
return this.isValidIndex(key) ? key : -1; | ||
}; | ||
@@ -203,2 +200,5 @@ | ||
Choices.prototype.enable = function(idx) { | ||
if (Array.isArray(idx)) { | ||
return idx.forEach(this.enable.bind(this)); | ||
} | ||
this.getChoice(idx).enable('checked'); | ||
@@ -219,2 +219,5 @@ return this; | ||
Choices.prototype.disable = function(idx) { | ||
if (Array.isArray(idx)) { | ||
return idx.forEach(this.disable.bind(this)); | ||
} | ||
this.getChoice(idx).disable('checked'); | ||
@@ -240,3 +243,3 @@ return this; | ||
} else { | ||
this.items[idx].toggle(); | ||
this.getChoice(idx).toggle(); | ||
} | ||
@@ -249,3 +252,3 @@ return this; | ||
* | ||
* @param {Object|Function|String} `val` | ||
* @param {Object|Function|String|RegExp} `val` | ||
* @return {Array} Matching choices or empty array | ||
@@ -256,16 +259,22 @@ * @api public | ||
Choices.prototype.where = function(val) { | ||
return this.items.filter(function(choice) { | ||
if (typeof val === 'function') { | ||
return val(choice); | ||
} | ||
var res = []; | ||
if (typeof val === 'string') { | ||
if (typeof val === 'function') { | ||
return this.filter(val); | ||
} | ||
if (typeof val === 'string') { | ||
return this.filter(function(choice) { | ||
return choice.name === val || choice.key === val; | ||
} | ||
}); | ||
} | ||
if (val instanceof RegExp) { | ||
if (utils.typeOf(val) === 'regexp') { | ||
return this.filter(function(choice) { | ||
return val.test(choice.name) || val.test(choice.key); | ||
} | ||
}); | ||
} | ||
if (utils.isObject(val)) { | ||
if (utils.isObject(val)) { | ||
return this.filter(function(choice) { | ||
for (var key in val) { | ||
@@ -275,12 +284,41 @@ if (!choice.hasOwnProperty(key)) { | ||
} | ||
if (val[key] !== choice[key]) { | ||
return false; | ||
} | ||
return val[key] === choice[key]; | ||
} | ||
}); | ||
} | ||
if (Array.isArray(val)) { | ||
var acc = []; | ||
for (var i = 0; i < val.length; i++) { | ||
acc = acc.concat(this.where.call(this, val[i])); | ||
} | ||
return true; | ||
}); | ||
return acc; | ||
} | ||
return []; | ||
}; | ||
/** | ||
* Returns true if the given `index` is a valid choice index. | ||
* @param {String} `key` Property name to use for plucking objects. | ||
* @return {Array} Plucked objects | ||
* @api public | ||
*/ | ||
Choices.prototype.isValidIndex = function(idx) { | ||
return utils.isNumber(idx) && idx !== -1 && idx < this.items.length; | ||
}; | ||
/** | ||
* Return the `.key` property from the choice at the given index. | ||
* @param {String} `key` Property name to use for plucking objects. | ||
* @return {Array} Plucked objects | ||
* @api public | ||
*/ | ||
Choices.prototype.key = function(key) { | ||
return this.getChoice(key).key; | ||
}; | ||
/** | ||
* Pluck an object with the specified key from the choices collection. | ||
@@ -287,0 +325,0 @@ * @param {String} `key` Property name to use for plucking objects. |
@@ -17,3 +17,3 @@ 'use strict'; | ||
require('extend-shallow', 'extend'); | ||
require('isobject', 'isObject'); | ||
require('kind-of', 'typeOf'); | ||
require('terminal-paginator', 'Paginator'); | ||
@@ -51,2 +51,11 @@ require('toggle-array'); | ||
/** | ||
* Returns true if `val` is a number (also ensures that val | ||
* is not whitespace, which is cast to `0`) | ||
*/ | ||
utils.isObject = function(val) { | ||
return utils.typeOf(val) === 'object'; | ||
}; | ||
/** | ||
* Cast `val` to an array. | ||
@@ -53,0 +62,0 @@ */ |
{ | ||
"name": "prompt-choices", | ||
"description": "Create an array of multiple choice objects for use in prompts.", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"homepage": "https://github.com/enquirer/prompt-choices", | ||
@@ -32,2 +32,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"isobject": "^2.1.0", | ||
"kind-of": "^3.0.4", | ||
"lazy-cache": "^2.0.1", | ||
@@ -34,0 +35,0 @@ "log-utils": "^0.2.1", |
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
24239
579
15
+ Addedkind-of@^3.0.4