Add options.limitMessage.

This commit is contained in:
anseki 2015-04-05 20:05:23 +09:00
parent e521609b5e
commit acc9db05cb

View file

@ -24,6 +24,7 @@ var
noEchoBack: false, noEchoBack: false,
mask: '*', mask: '*',
limit: [], limit: [],
limitMessage: 'Input another, please.',
caseSensitive: false, caseSensitive: false,
noTrim: false, noTrim: false,
encoding: 'utf8', encoding: 'utf8',
@ -45,7 +46,7 @@ var
noTrim: boolean noTrim: boolean
encoding, bufferSize, print encoding, bufferSize, print
*/ */
function _readlineSync(options) {return options; function _readlineSync(options) {
var input = '', displaySave = options.display, var input = '', displaySave = options.display,
silent = !options.display && options.keyIn && options.noEchoBack && !options.mask; silent = !options.display && options.keyIn && options.noEchoBack && !options.mask;
@ -415,6 +416,7 @@ function margeOptions() {
// string // string
case 'mask': // * * case 'mask': // * *
case 'encoding': // * * case 'encoding': // * *
case 'limitMessage': // *
/* jshint eqnull:true */ /* jshint eqnull:true */
options[optionName] = value != null ? value + '' : ''; options[optionName] = value != null ? value + '' : '';
/* jshint eqnull:false */ /* jshint eqnull:false */
@ -458,28 +460,51 @@ function margeOptions() {
}, {}); }, {});
} }
function flattenArray(array, validate) { function flattenArray(array, validator) {
var flatArray = []; var flatArray = [];
function parseArray(array) { function parseArray(array) {
/* jshint eqnull:true */ /* jshint eqnull:true */
if (array == null) { return; } if (array == null) { return; }
/* jshint eqnull:false */ /* jshint eqnull:false */
else if (Array.isArray(array)) { array.forEach(parseArray); } else if (Array.isArray(array)) { array.forEach(parseArray); }
else if (!validate || validate(array)) { flatArray.push(array); } else if (!validator || validator(array)) { flatArray.push(array); }
} }
parseArray(array); parseArray(array);
return flatArray; return flatArray;
} }
function isMatchedLimit(res, limit, caseSensitive) {
return !limit.length || limit.some(function(valid) {
if (typeof valid === 'number') { valid += ''; }
return typeof valid === 'string' ? (
caseSensitive ?
res === valid : res.toLowerCase() === valid.toLowerCase()
) :
valid instanceof RegExp ? valid.test(res) : false;
});
}
function readlineWithOptions(options) {
var res, limitSave = options.limit, displaySave = options.display;
options.display += '';
options.limit = ''; // for readlineExt
while (true) {
res = _readlineSync(options);
if (isMatchedLimit(res, limitSave, options.caseSensitive)) { break; }
options.display += (options.display ? '\n' : '') +
(options.limitMessage ? options.limitMessage + '\n' : '') + displaySave;
}
return res;
}
// for dev // for dev
exports._useExtSet = function(use) { useExt = use; }; exports._useExtSet = function(use) { useExt = use; };
exports.prompt = function(options) { exports.prompt = function(options) {
var readOptions = margeOptions(true, options), var readOptions = margeOptions(true, options), res;
limit = readOptions.limit, res; readOptions.display = readOptions.prompt;
readOptions.display = readOptions.prompt + ''; res = readlineWithOptions(readOptions);
readOptions.limit = ''; // for readlineExt
res = _readlineSync(readOptions);
return res; return res;
}; };
@ -487,10 +512,8 @@ exports.question = function(query, options) {
var readOptions = margeOptions(margeOptions(true, options), { var readOptions = margeOptions(margeOptions(true, options), {
display: query display: query
}), }),
limit = readOptions.limit, res; res = readlineWithOptions(readOptions);
readOptions.display += '';
readOptions.limit = ''; // for readlineExt
res = _readlineSync(readOptions);
return res; return res;
}; };