Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow default:false value for confirm. #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/prompts/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ module.exports = Prompt;
function Prompt() {
Base.apply(this, arguments);

var defaultValue = this.opt.default;

if (_.isString(defaultValue)) {
defaultValue = defaultValue !== "false";
} else if (_.isUndefined(this.opt.default)) {
defaultValue = true;
}

_.extend(this.opt, {
filter: function(input) {
var value = true;
var value = defaultValue;
if (input != null && input !== "") {
value = /^y(es)?/i.test(input);
}
Expand Down
62 changes: 62 additions & 0 deletions test/specs/prompts/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,66 @@ describe("`confirm` prompt", function() {
this.rl.emit("line", "bla bla foo");
});

describe("should accept a default value", function() {

it("should allow a default value", function(done) {

var falseConfirm = new Confirm({
message: "foo bar",
default: false
}, this.rl);

falseConfirm.run(function(answer) {
expect(answer).to.be.false;
done();
});

this.rl.emit("line", "");
});

it("should default \"true\" to boolean true", function(done) {

var falseConfirm = new Confirm({
message: "foo bar",
default: "true"
}, this.rl);

falseConfirm.run(function(answer) {
expect(answer).to.be.true;
done();
});

this.rl.emit("line", "");
});

it("should default \"false\" to boolean false", function(done) {
var falseConfirm = new Confirm({
message: "foo bar",
default: "false"
}, this.rl);

falseConfirm.run(function(answer) {
expect(answer).to.be.false;
done();
});

this.rl.emit("line", "");
});

it("should default any other string than \"true\" to boolean true", function(done) {
var falseConfirm = new Confirm({
message: "foo bar",
default: "asdfdsa"
}, this.rl);

falseConfirm.run(function(answer) {
expect(answer).to.be.true;
done();
});

this.rl.emit("line", "");
});

});

});