BLOG | CODE | ABOUT

Overview

What does fSignatures do?

fSignatures allows you to wrap a function in another one which checks argument and return types at runtime. It also allows you to use the concept of interface, by checking the presence of certain functions in an object, along with proper signatures. I tried to keep it simple, and you may use it only on certain functions and not others, as suits you.

(By the way, thanks for stopping by!)

Why would this be useful?

The current version of JavaScript, as found in web browsers, does not allow for function signature specification with types (as in Java and C++, for example), at least not out of the box.

As a programming project grows larger, it becomes more complex. You might start calling functions with parameters in the wrong order, or misinterpret the meaning of a parameter name (e.g.: does 'file' mean a string path to the file or a 'FileClass' instance?). Being able to specify a function signatures with types will help in catching some of these errors.

Also, when trying to render your functions error-proof, you'll often find yourself in the need to check some properties of the parameters. For example, if you want to check if a zip code is valid, you'll begin by checking if it's a string at all. Checking parameter types at first reduces that kind of work.

There are also arguments against type checking. For more information, check out the links in the "On type checking" section.

Download

Grab the first version.

Warning: this is an alpha version. I've written 60 or so unit tests, but it lacks thorough review. Also, some basic TypeCheckers may be missing. I'm trying to gauge the interest in the lib before I invest more time in this or that direction. Feedback will be very appreciated!

Basic syntax examples

Basic argument and return type checking. Notice the chained function calls (args().returns().f()).

    var concatenateStrNum = $.tc          // concatenateStrNum is our function
        .args('str', 'num')
        .returns('str')
        .f(function(theStr, theNum){
            return theStr + theNum;
        });

Basic inteface specification and checking.

    // Methods that must be present in object implementing addableInterface
    // Notice the use of the same syntax as for function declaration, but
    // without the final "f()" of the function itself.
    var addableInterfaceMembers = {
        "addTo": $.tc
            .args('num')
            .returns('num')
    };
    
    // Interface object
    var addableInterface = $.tc.interfaceChecker(addableInterfaceMembers);
    
    // Object implementing addableInterface
    var addableObj = {
        innerNum : 42,
        addTo : $.tc
            .args('num')
            .returns('num')
            .f(function(theNum){
                return theNum + this.innerNum;
            })
        }
    
    // Function taking an addableInterface instance has parameter
    var addThose = $.tc
        .args('num', addableInterface)
        .returns('num')
        .f(function(theNum, addable){
            return addable.addTo(theNum);
        });
        
    // Call to the function with an object implementing said interface
    addThose(84, addableObj);

Features

Specific use cases

Status

To keep updates on the status of the project, you may suscribe to the fSignatures.js RSS feed, a category feed from my blog (general JavaScript category feed).