Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I wrote a function that verifies the arguments against an expected set of arguments.

test('hello', 'world');
function test() {
    verifySignature(arguments, String, [Number], String)
    // returns true
}
function test() {
    verifySignature(arguments, String, Number)
    // returns false
}

test('hello', 'world');
function test() {
  alert(verifySignature(arguments, String, Number));
}

function verifySignature() {
    var args = arguments[0];
    var expectations = Array.prototype.slice.call(arguments, 1);
    for (var i = 0; i < expectations.length; i++) {
        // Going through each expectation
        if (expectations[i] instanceof Array) {
            console.log('Comparing [', args[i], '] with [', Object.prototype.toString.call(new expectations[i][0]));
            // optional expectation
            if (Object.prototype.toString.call(args[i]) == Object.prototype.toString.call(new expectations[i][0])) {
                // good
            } else {
                // or maybe the next one will be good
                expectations.shift();
                i--;
            }
        } else {
            console.log('Comparing [', args[i], '] with [', Object.prototype.toString.call(new expectations[i]));
            // required! expectation
            if (Object.prototype.toString.call(args[i]) == Object.prototype.toString.call(new expectations[i])) {
                // good
            } else {
                // error!
                return false;
            }
        }
    };
    return true;
}

I was actually looking for a library that already does this, if I missed one, please point me to it.

I initially thought something like this would be a big project in itself. So I'm not really confident and fully expect to improve a lot on it as I use it more and more. Please review and suggest corrections and improvements.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.