From 0e3fe968d4562ad2bc49c00ef77eaf4a6deafb8d Mon Sep 17 00:00:00 2001 From: Arlando Battle Date: Tue, 9 Dec 2014 15:08:03 -0500 Subject: [PATCH] Added an example using when.# --- examples/when.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/when.js diff --git a/examples/when.js b/examples/when.js new file mode 100644 index 000000000..db6324000 --- /dev/null +++ b/examples/when.js @@ -0,0 +1,46 @@ +/** + * When example + */ + +"use strict"; +var inquirer = require("../lib/inquirer"); + +var questions = [ + { + type: "confirm", + name: "bacon", + message: "Do you like bacon?" + }, + { + type: "input", + name: "favorite", + message: "Bacon lover, what is your favorite type of bacon?", + when: function ( answers ) { + return answers.bacon; + } + }, + { + type: "confirm", + name: "pizza", + message: "Ok... Do you like pizza?", + when: function (answers) { + return !likesFood( "bacon" )(answers); + } + }, + { + type: "input", + name: "favorite", + message: "Whew! What is your favorite type of pizza?", + when: likesFood( "pizza" ) + } +]; + +function likesFood ( aFood ) { + return function ( answers ) { + return answers[ aFood ]; + } +} + +inquirer.prompt(questions, function (answers) { + console.log( JSON.stringify(answers, null, " ") ); +}); \ No newline at end of file