Add options.limitMessage
.
This commit is contained in:
parent
e521609b5e
commit
acc9db05cb
1 changed files with 35 additions and 12 deletions
|
@ -24,6 +24,7 @@ var
|
|||
noEchoBack: false,
|
||||
mask: '*',
|
||||
limit: [],
|
||||
limitMessage: 'Input another, please.',
|
||||
caseSensitive: false,
|
||||
noTrim: false,
|
||||
encoding: 'utf8',
|
||||
|
@ -45,7 +46,7 @@ var
|
|||
noTrim: boolean
|
||||
encoding, bufferSize, print
|
||||
*/
|
||||
function _readlineSync(options) {return options;
|
||||
function _readlineSync(options) {
|
||||
var input = '', displaySave = options.display,
|
||||
silent = !options.display && options.keyIn && options.noEchoBack && !options.mask;
|
||||
|
||||
|
@ -415,6 +416,7 @@ function margeOptions() {
|
|||
// string
|
||||
case 'mask': // * *
|
||||
case 'encoding': // * *
|
||||
case 'limitMessage': // *
|
||||
/* jshint eqnull:true */
|
||||
options[optionName] = value != null ? value + '' : '';
|
||||
/* jshint eqnull:false */
|
||||
|
@ -458,28 +460,51 @@ function margeOptions() {
|
|||
}, {});
|
||||
}
|
||||
|
||||
function flattenArray(array, validate) {
|
||||
function flattenArray(array, validator) {
|
||||
var flatArray = [];
|
||||
function parseArray(array) {
|
||||
/* jshint eqnull:true */
|
||||
if (array == null) { return; }
|
||||
/* jshint eqnull:false */
|
||||
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);
|
||||
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
|
||||
exports._useExtSet = function(use) { useExt = use; };
|
||||
|
||||
exports.prompt = function(options) {
|
||||
var readOptions = margeOptions(true, options),
|
||||
limit = readOptions.limit, res;
|
||||
readOptions.display = readOptions.prompt + '';
|
||||
readOptions.limit = ''; // for readlineExt
|
||||
res = _readlineSync(readOptions);
|
||||
var readOptions = margeOptions(true, options), res;
|
||||
readOptions.display = readOptions.prompt;
|
||||
res = readlineWithOptions(readOptions);
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
|
@ -487,10 +512,8 @@ exports.question = function(query, options) {
|
|||
var readOptions = margeOptions(margeOptions(true, options), {
|
||||
display: query
|
||||
}),
|
||||
limit = readOptions.limit, res;
|
||||
readOptions.display += '';
|
||||
readOptions.limit = ''; // for readlineExt
|
||||
res = _readlineSync(readOptions);
|
||||
res = readlineWithOptions(readOptions);
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue