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

View file

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