Created
June 16, 2010 15:16
-
-
Save robinduckett/440834 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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