Skip to content
Browse files

Use non-strict comparison in the choice validator.

  • Loading branch information...
1 parent f1b3de2 commit cdb7c67ecf0bb1cd0f70e7c4eef236d533995916 @satazor satazor committed Jun 4, 2013
Showing with 23 additions and 4 deletions.
  1. +1 −1 .jshintrc
  2. +8 −3 index.js
  3. +14 −0 test/test.js
View
2 .jshintrc
@@ -16,7 +16,7 @@
"bitwise": true,
"curly": true,
- "eqeqeq": true,
+ "eqeqeq": false,
"forin": false,
"immed": true,
"latedef": false,
View
11 index.js
@@ -161,12 +161,17 @@ promptly.choose = function (message, choices, opts, fn) {
}
// Push the choice validator
+ var nrChoices = choices.length;
var validator = function (value) {
- if (choices.indexOf(value) === -1) {
- throw new Error('Invalid choice: ' + value);
+ var x;
+
+ for (x = 0; x < nrChoices; x++) {
+ if (choices[x] == value) {
+ return choices[x];
+ }
}
- return value;
+ throw new Error('Invalid choice: ' + value);
};
opts.validator.push(validator);
View
14 test/test.js
@@ -214,6 +214,20 @@ describe('choose()', function () {
process.stdin.emit('data', 'apple\n');
});
+
+ it('should not use strict comparison when matching against valid choices', function (next) {
+ stdout = '';
+
+ promptly.choose('choices: ', [1, 2, 3], function (err, value) {
+ expect(err).to.be(null);
+ expect(typeof value).to.equal('number');
+ expect(value).to.be(1);
+ expect(stdout).to.contain('choices: ');
+ next();
+ });
+
+ process.stdin.emit('data', '1\n');
+ });
});
describe('confirm()', function () {

0 comments on commit cdb7c67

Please sign in to comment.
Something went wrong with that request. Please try again.