New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

synchro-prompt

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

synchro-prompt - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

CHANGELOG.md

47

index.js

@@ -6,8 +6,26 @@ var readlineSync = require('readline-sync'),

color: 'yellow',
format: true,
validate: function(input) { return input; }
transform: function(input) { return input; }
};
// stipped down version of underscore's _.defaults
function defaults(dest, src) {
var defaults = _defaults;
module.exports = function(message, options) {
// reset to factory defaults
if (!arguments.length) {
defaults = _defaults;
return;
}
// override default options from here on out
var kind = typeof message;
if (kind !== 'string' && kind !== 'number' && !(message instanceof Array)) {
defaults = message;
return;
}
return prompt(message, options);
};
function merge(dest, src) {
for (var prop in src) {

@@ -19,21 +37,12 @@ if (dest[prop] === void 0) dest[prop] = src[prop];

/**
* Wrapper around [`readline-sync`](https://github.com/anseki/readline-sync) with
* [`chalk`](https://github.com/sindresorhus/chalk) coloring for synchronous cli prompting
*
* @param {String|Array<String>} message the prompt message / question
* @param {Object} options [optional] {color: <string>, format: <boolean>, validate: <function>}
* @return {String|Array<String>} user input string or array of strings depending on `message` type
*/
module.exports = function(message, options) {
options = options ? defaults(options, _defaults) : _defaults;
function prompt(message, options) {
options = options ? merge(options, defaults) : defaults;
if (message instanceof Array) {
console.log('...about to map...');
return message.map(function(mess) {
return module.exports(mess, options);
return prompt(mess, options);
});
}
var input = readlineSync.question(chalk[options.color](message));
if (options.format) input = input.trim().toLowerCase();
options.validate(input);
return input;
};
return options.transform(input);
}
{
"name": "synchro-prompt",
"version": "1.0.2",
"version": "1.1.0",
"description": "super simple synchronous cli prompt using readline-sync with chalk coloring",

@@ -5,0 +5,0 @@ "main": "index.js",

# synchro-prompt
Wrapper around [readline-sync](https://github.com/anseki/readline-sync) with
[chalk](https://github.com/sindresorhus/chalk) coloring for synchronous cli prompting.
Wrapper around [readline-sync][1] with
[chalk][2] coloring for synchronous cli prompting.

@@ -14,4 +14,2 @@ ## Install

#### prompt(&lt;String|Array&lt;String&gt;&gt;, [options])
```js

@@ -21,22 +19,59 @@ var prompt = require('synchro-prompt');

var answer = prompt('Enter value: ');
answer instanceof String; // true
// $ Enter Value: HELLO!
answer; // HELLO!
// or as an array
var answers = prompt(['Name: ', 'Age: ']);
// $ Name: Jane
// $ Age: Doe
answers instanceof Array; // true
answers; // ['Jane', 'Doe']
```
#### Options
## Options
By default, the prompt coloring is yellow. This can be overriden
with any valid [`chalk`][2] color (provided as string), along with an optional
transformation callback which can be used to format or validate input.
```js
// defaults
{
color: 'yellow',
format: true, // trim and toLowerCase
validate: function(input) {}
}
var options = {
color: 'magenta',
transform: function(input) {
return input.trim().toLowerCase();
}
};
prompt('Enter something: ', options);
// back to the default no-transform yellow
prompt('And again: ');
```
You can set the options from one point forward by passing the options
hash as the first argument.
```js
// prompt from here on will always use your options
prompt({
color: green,
transform: function(input) {
return Date.now() + '_' + input;
}
});
prompt('...'); // ie. 1426910954458_something
// but you can still override a single call
prompt('...', { transform: function(input) { return input; }});
// future calls without a second argument will go back to prepending a timestamp
// to restore the factory defaults, call the synchro-prompt fn with no arguments
prompt(); // yellow, no-transform
```
## License
[lokua.net/license-mit](http://lokua.net/license-mit.html)
[lokua.net/license-mit][2]
[1]: https://github.com/anseki/readline-sync
[2]: https://github.com/sindresorhus/chalk
[3]: http://lokua.net/license-mit.html
var prompt = require('../index'),
chalk = require('chalk');
chalk = require('chalk');
var options = {
console.log('TEST1 (default - should not format)')
var answer = prompt('Enter something: ');
console.log(chalk.green('You entered: ') + answer);
console.log();
console.log('TEST2 (transform=(trim>toLower),color=magenta)');
answer = prompt('Enter something: ', {
color: 'magenta',
format: false,
validate: function(input) {
if (isNaN(+input)) {
throw new Error(chalk.red(input + ' is not a number'));
}
}
};
transform: function(input) {
return input.trim().toLowerCase();
}
});
console.log(chalk.green('You entered: ') + answer);
console.log();
var answer = prompt('Enter a number: ', options);
console.log('TEST3 (prompt should prompt for each value in array)');
answer = prompt([1,2,3]);
console.log('answer: %j', answer);
console.log(chalk.green('You entered: ') + answer);
console.log();
var answers = prompt(['1st', '2nd', '3rd'].map(function(n) {
return 'Enter ' + n + ' value: ';
}));
console.log(chalk.green('You entered: ') + answers);
console.log();
console.log('TEST4 (all prompts should use provided default override)');
prompt({
color: 'blue',
transform: function(input) {
return Date.now() + ' ' + input;
}
});
for (var i = 0; i < 3; i++){
var answer = prompt('Enter something: ');
console.log(chalk.green('You entered: ') + answer);
}
console.log('TEST5 (restoring factory defaults)');
prompt();
answer = prompt('Am I yellow and case sensitive? ');
console.log(chalk.green('You entered: ') + answer);
console.log();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc