From cabbc1ffeafdae60f2a020550e2a1fe0d548bd45 Mon Sep 17 00:00:00 2001 From: anseki Date: Sat, 12 Jul 2014 08:16:11 +0900 Subject: [PATCH] `setPrompt()` and `question()` accept the value which is not string too (e.g. number, Date, Object, etc.). --- README.md | 3 ++- lib/readline-sync.js | 20 ++++++++++++-------- package.json | 2 +- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bd88079..070e406 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ console.log('Oh, so your favorite food is ' + answer); ## Installation ``` -npm install -g readline-sync +npm install readline-sync ``` ## Usage @@ -100,6 +100,7 @@ try { + If `options.noEchoBack` is used, the text that input by user is saved to temporary file (e.g. `/tmp/readline-sync.stdout`). This file is removed immediately after reading is done, but you have to be careful about it because this text is *plain*. Removing the file might fail, or the file might be peeped before it is removed. ## Release History + * 2014-07-12 v0.4.1 `setPrompt()` and `question()` accept the value which is not string too (e.g. number, Date, Object, etc.). * 2014-07-12 v0.4.0 Add `options.noEchoBack`. * 2014-07-12 v0.3.0 Add `setPrint()`. * 2014-06-27 v0.2.3 Add alternative reading via shell on the environment which don't support interactively reading. diff --git a/lib/readline-sync.js b/lib/readline-sync.js index 372ce45..32382b9 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -20,16 +20,16 @@ var promptText = '> ', function _readlineSync(display, options) { var input = '', rsize, err; - if (display) { + if (display !== '') { if (typeof print === 'function') { print(display, encoding); } - stdout.write(display, encoding); + stdout.write(display + '', encoding); } if (options && options.noEchoBack) { // Try reading via shell input = _readlineShell(true); if (typeof input !== 'string') { - if (display) { stdout.write('\n', encoding); } // Return from prompt line. + if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line. throw new Error('Can\'t read via shell'); } @@ -57,7 +57,7 @@ function _readlineSync(display, options) { err.errno = e.errno; err.code = e.code; } - if (display) { stdout.write('\n', encoding); } // Return from prompt line. + if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line. throw err || e; } @@ -136,9 +136,9 @@ exports.useShellSet = function(use) { useShell = use; }; exports.setPrint = function(fnc) { print = fnc; }; exports.setPrompt = function(newPrompt) { - if (typeof newPrompt === 'string') { - promptText = newPrompt; - } +/* jshint eqnull:true */ + promptText = newPrompt != null ? newPrompt : ''; +/* jshint eqnull:false */ }; exports.setEncoding = function(newEncoding) { @@ -152,5 +152,9 @@ exports.prompt = function(options) { }; exports.question = function(query, options) { - return _readlineSync(typeof query === 'string' ? query : '', options); + return _readlineSync( +/* jshint eqnull:true */ + query != null ? query : '', +/* jshint eqnull:false */ + options); }; diff --git a/package.json b/package.json index 83f35d6..cb8a1df 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "readline-sync", "description": "Synchronous Readline", - "version": "0.4.0", + "version": "0.4.1", "homepage": "https://github.com/anseki/readline-sync", "author": { "name": "anseki"