funcify

    1.0.1 • Public • Published

    funcify.js: Super funky function overloading

    Funcify is a straight forward, easy to use function overloading library written in JavaScript that works in browsers and JS backends.

    It allows you to easily create overloaded functions that perform different actions based on the number and types of arguments.

    Installing

    To install funcify, either:

    Install via NPM

    npm install funcify
    

    Install via Bower

    bower install funcify
    

    Download a release

    https://github.com/garydouble/funcify/releases

    Getting started

    Each overload is defined as an Array of parameter types (filters) and a callback.

    The general signature looks like this

    funcify(function|array, ...): function
    

    The funcify function returns a new function that handles the overloading.

    Here are some examples:

    // fn will be an overloaded function.
    var fn = funcify(
      
      // fn()
      function() {
        // This is a "catch-all" function and
        // will be called if no specific override exists.
      },
    
      // fn(string)
      ['string', function(someString) {
        doSomethingWith(someString);
      }],
      
      // fn(*, int)
      ['any', 'int', function(o, i) { /* ... */ }]
    );
    

    Built-in type checking filters

    Wildcard / Any type filter

    '*' or 'any'

    Allows anything and everything, except undefined.

    String filter

    's' or 'string' or /regexp/

    Validates the parameter is a String.

    var fn = funcify(['string', function(str) { /* ... */ }]);
    
    // yea
    fn('hello');
    
    // nay
    fn(123);
    

    When a /regexp/ is provided an additional check will be made to ensure the parameter tests positive against the pattern.

    var fn = funcify([/^[abc]$/, function(str) { /* ... */ }]);
    
    // yea
    fn('a');
    fn('b');
    fn('c');
    
    // nay
    fn('xyz');
    fn('abc');
    

    Function filter

    'f' or 'function'

    Validates the parameter is a Function.

    var fn = funcify(['function', function(func) { /* ... */ }]);
    
    // yea
    fn(function() {});
    fn(myPreDefinedFunction);
    
    // nay
    fn('hello');
    

    Object filter

    'o' or 'object'

    Validates the parameter is an Object literal, explicitly.

    var fn = funcify(['object', function(obj) { /* ... */ }]);
    
    // yea
    fn({});
    
    // nay
    fn('hello');
    fn([]);
    fn(new Date());
    

    Array filter

    'a' or 'array'

    Validates the parameter is an Array or "Array-like".

    var fn = funcify(['array', function(arr) { /* ... */ }]);
    
    // yea
    fn([1, 2, 3]);
    fn(new Float32Array())
    fn(arguments)
    
    // nay
    fn('hello');
    fn({});
    

    RegExp filter

    'r' or 'regexp'

    Validates the parameter is a RegExp.

    Not to be confused with pattern matching, for that see the String filter.

    var fn = funcify(['regexp', function(rx) { /* ... */ }]);
    
    // yea
    fn(/abc/);
    fn(new RegExp());
    
    // nay
    fn('hello');
    fn({});
    

    Boolean filter

    'b' or 'boolean'

    Validates the parameter is either true or false.

    var fn = funcify(['boolean', function(b) { /* ... */ }]);
    
    // yea
    fn(true);
    fn(false);
    
    // nay
    fn([]);
    fn('true');
    fn(0);
    

    Date filter

    'd' or 'date'

    Validates the parameter is a Date.

    var fn = funcify(['date', function(date) { /* ... */ }]);
    
    // yea
    fn(new Date());
    
    // nay (sorry not yet)
    fn(moment()); // 
    fn('2017-12-17');
    fn(5873242342);
    

    Number filter

    'n' or 'number'

    Validates the parameter is a Number.

    var fn = funcify(['number', function(n) { /* ... */ }]);
    
    // yea
    fn(123);
    fn(4.56);
    fn(NaN);
    fn(Infinity);
    

    Int filter

    'int'

    Validates the parameter does not equal NaN when passed through parseInt.

    var fn = funcify(['int', function(i) { /* ... */ }]);
    
    // yea
    fn(123);
    fn(4.56);
    
    // nay
    fn(NaN);
    fn(Infinity);
    

    Float filter

    'float'

    Validates the parameter does not equal NaN when passed through parseFloat.

    var fn = funcify(['float', function(f) { /* ... */ }]);
    
    // yea
    fn(123);
    fn(4.56);
    
    // nay
    fn(NaN);
    fn(Infinity);
    

    Class filter

    Class

    Validates the parameter is an instance of a particular class.

    var fn = funcify([MyClass, function(inst) { /* ... */ }]);
    
    var inst = new MyClass();
    
    // yea
    fn(inst);
    

    Defining your own filters

    Filters are defined as factory functions that return new functions that act upon a single argument parameter and return true or false depending on whether said parameter meets the specific filter requirements.

    To define a custom filter simply attach a factory function to a property on the funcify.filters object.

    // Register your shiney new filter.
    funcify.filters['binary'] = function() {
      return function(arg) {
        return /^[01 ]+$/.test(arg);
      };
    };
    
    // Configure an overload that uses it
    var fn = funcify(['binary', function(bitString) {
      return convertToAscii(bitString); // or whatever!
    }]);
    

    Once a new filter has been defined you can then refer to it in your signature specifications.

    // Call your function with the new override
    var howCoolIsThis = fn('01000101 01010000 01001001 01000011');
    

    Issues and Contributions

    https://github.com/garydouble/funcify/issues

    Issues, feedback and pull requests all welcome.
    Happy Coding!

    Install

    npm i funcify

    DownloadsWeekly Downloads

    0

    Version

    1.0.1

    License

    MIT

    Last publish

    Collaborators

    • avatar