From acc9db05cb16d58bef458c5f57bf2ae3e923cb3d Mon Sep 17 00:00:00 2001 From: anseki Date: Sun, 5 Apr 2015 20:05:23 +0900 Subject: [PATCH] Add `options.limitMessage`. --- lib/readline-sync.js | 47 +++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/readline-sync.js b/lib/readline-sync.js index be94c6d..7aeb492 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -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; };