Add options.isTrue, options.isFalse.

This commit is contained in:
anseki 2015-04-05 23:14:48 +09:00
parent abdfcefbbb
commit 008f31fb6d
2 changed files with 39 additions and 15 deletions

View file

@ -29,7 +29,9 @@ var
noTrim: false,
encoding: 'utf8',
bufferSize: 1024,
print: void 0
print: void 0,
isTrue: [],
isFalse: []
},
useExt = false,
@ -203,7 +205,7 @@ function _readlineSync(options) {
options.encoding);
}
return options.noTrim || options.keyIn ? input : input.trim();
return (options.noTrim || options.keyIn ? input : input.trim());
}
function readlineExt(options) {
@ -439,6 +441,8 @@ function margeOptions() {
break;
// array
case 'limit': // * * readlineExt
case 'isTrue': // *
case 'isFalse': // *
/* jshint eqnull:true */
options[optionName] = value != null ?
flattenArray(value, function(value) {
@ -473,14 +477,14 @@ function flattenArray(array, validator) {
return flatArray;
}
function isMatchedLimit(res, limit, caseSensitive) {
return !limit.length || limit.some(function(valid) {
if (typeof valid === 'number') { valid += ''; }
return typeof valid === 'string' ? (
function isMatched(res, comps, caseSensitive) {
return comps.some(function(comp) {
if (typeof comp === 'number') { comp += ''; }
return (typeof comp === 'string' ? (
caseSensitive ?
res === valid : res.toLowerCase() === valid.toLowerCase()
res === comp : res.toLowerCase() === comp.toLowerCase()
) :
valid instanceof RegExp ? valid.test(res) : false;
comp instanceof RegExp ? comp.test(res) : false);
});
}
@ -490,13 +494,22 @@ function readlineWithOptions(options) {
options.limit = ''; // for readlineExt
while (true) {
res = _readlineSync(options);
if (isMatchedLimit(res, limitSave, options.caseSensitive)) { break; }
if (!limitSave.length ||
isMatched(res, limitSave, options.caseSensitive)) { break; }
options.display += (options.display ? '\n' : '') +
(options.limitMessage ? options.limitMessage + '\n' : '') + displaySave;
}
return res;
}
function toBool(res, options) {
return (
(options.isTrue.length &&
isMatched(res, options.isTrue, options.caseSensitive)) ? true :
(options.isFalse.length &&
isMatched(res, options.isFalse, options.caseSensitive)) ? false : res);
}
// for dev
exports._useExtSet = function(use) { useExt = use; };
@ -504,8 +517,7 @@ exports.prompt = function(options) {
var readOptions = margeOptions(true, options), res;
readOptions.display = readOptions.prompt;
res = readlineWithOptions(readOptions);
return res;
return toBool(res, readOptions);
};
exports.question = function(query, options) {
@ -513,8 +525,7 @@ exports.question = function(query, options) {
display: query
}),
res = readlineWithOptions(readOptions);
return res;
return toBool(res, readOptions);
};
exports.keyIn = function(query, options) {
@ -528,7 +539,20 @@ exports.keyIn = function(query, options) {
{ return typeof value === 'string' || typeof value === 'number'; })
.join('').replace(/\n/g, '').replace(/[^A-Za-z0-9_ ]/g, '\\$&');
res = _readlineSync(readOptions);
return res;
['isTrue', 'isFalse'].forEach(function(optionName) {
var comps = [];
readOptions[optionName].forEach(function(comp) {
if (typeof comp === 'string' || typeof comp === 'number') {
comps = comps.concat((comp + '').split(''));
} else if (comp instanceof RegExp) {
comps.push(comp);
}
});
readOptions[optionName] = comps;
});
return toBool(res, readOptions);
};
exports.setDefault = function(options) {

View file

@ -1,6 +1,6 @@
{
"name": "readline-sync",
"version": "0.11.0",
"version": "0.12.0",
"title": "readlineSync",
"description": "Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).",
"keywords": [