Skip to content

Instantly share code, notes, and snippets.

@robinduckett
Created June 16, 2010 15:16
Show Gist options
  • Save robinduckett/440834 to your computer and use it in GitHub Desktop.
Save robinduckett/440834 to your computer and use it in GitHub Desktop.
// I have no idea what anyone would want to use this for.
function ask(questions, passcb, failcb) {
var require = [];
var pass = [];
var fail = [];
for (var question in questions) {
if (question.search('required') > -1) {
require.push(question);
}
if (questions[question]) {
pass.push(question);
} else {
fail.push(question);
}
}
for (var i = 0, len = fail.length; i < len; i++) {
for (var j = 0, reqlen = require.length; j < reqlen; j++) if (fail[i] == require[j]) {
failcb.apply({}, [{'passed': pass, 'failed': fail, 'required': require}]);
return;
}
}
var passed = pass.length > fail.length ? true : false;
if (passed) {
passcb.apply({}, [{'passed': pass, 'failed': fail, 'required': require}]);
return;
} else {
failcb.apply({}, [{'passed': pass, 'failed': fail, 'required': require}]);
return;
}
}
var sys = require('sys');
data = {
Car: {
registration: 'CA'
}
};
ask({
data: data !== null,
car_data: data.Car.registration !== '',
required_whatever: 2==1
}, function(status) {
sys.puts('We have passed ('+status.passed.length+' pass / '+status.failed.length+' fail) (' + status.required.length + ' required)');
}, function(status) {
sys.puts('We have failed ('+status.passed.length+' pass / '+status.failed.length+' fail) (' + status.required.length + ' required)');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment