Skip to content
Myr

support for RegExp coercion

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Mike Reinstein · committed by Mike Reinstein · parent 46f6628 · Browse files · 2 files+29 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−2 removed

  • other:+29 added−2 removed

2 files+29 added−2 removed

modified:index.js+23 added-2 removed
809 unmodified lines
810 fn(val);811 });812};813814815/**816 * Prompt for a `Regular Expression`.817 *818 * @param {String} str819 * @param {Object} pattern regular expression object to test820 * @param {Function} fn821 * @api private822 */823824Command.prototype.promptForRegexp = function(str, pattern, fn){825 var self = this;826 this.promptSingleLine(str, function parseRegexp(val){827 if(!pattern.test(val)) return self.promptSingleLine(str + '(regular expression mismatch) ', parseRegexp);828 fn(val);829 });830};831832833/**834 * Single-line prompt.835 *836 * @param {String} str837 * @param {Function} fn838 * @api private839 */840841Command.prototype.promptSingleLine = function(str, fn){842 // determine if the 2nd argument is a regular expression843 if (arguments[1].global !== undefined && arguments[1].multiline !== undefined) {844 return this.promptForRegexp(str, arguments[1], arguments[2]);823 if ('function' == typeof arguments[2]) {845 } else if ('function' == typeof arguments[2]) {846 return this['promptFor' + (fn.name || fn)](str, arguments[2]);847 }848849 process.stdout.write(str);
50 unmodified lines
900 */901902Command.prototype.prompt = function(str, fn){903 var self = this;882904 if ('string' == typeof str) {905 if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);906 this.promptMultiLine(str, fn);907 } else {
245 unmodified lines
modified:Readme.md+6 added
183 unmodified lines
184 console.log('date: %s', date);185});186```187188```js189program.prompt('Email: ', /^.+@.+\..+$/, function(email){190 console.log('email: %j', email);191});192```193194## .password(msg[, mask], fn)195196Prompt for password without echoing:197
79 unmodified lines