Code coverage report for src/Common/Chain/ChainFunction.js

Statements: 100% (15 / 15)      Branches: 100% (2 / 2)      Functions: 66.67% (2 / 3)      Lines: 100% (14 / 14)      Ignored: none     

All files » src/Common/Chain/ » ChainFunction.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64    1                                   1   704   704 704 704                                   1   742 742 742     742     742     742         1  
'use strict';
 
var _    = require("lodash"),
    Type = require("../Type"),
    Safe = require("../Safe");
 
/**
 * @class
 * The ChainFunction wraps the functionality of a function executed
 * on a chain context.
 *
 * @param {Object} options
 *
 * @example
 * new ChainFunction({
 *     fn   : function(){},
 *     args : []
 * });
 *
 */
var ChainFunction = function(options){
 
    options = Safe.object(options);
 
    this.name   = Safe.string(options.name, "?");
    this.fn     = Safe.function(options.fn, function(){});
    this.args   = Safe.array(options.args);
 
};
 
/**
 *
 * Execute the function within the context of the given scope and args.
 *
 * The arguments of the function will be the concatenation of the given
 * args and the instance arguments.
 *
 * @param  {*} scope
 * @param  {*} args
 * @param  {*} extraArgs
 *
 * @return {*}
 *
 */
ChainFunction.prototype.apply = function(scope, args, extraArgs) {
 
    scope       = Safe.object(scope);
    args        = Safe.array(args);
    extraArgs   = Safe.array(extraArgs);
 
    /// the given args should always exist
    if(args.length === 0) args = [ null ];
 
    /// concatenate the given arguments with the instance
    args = args.concat(this.args).concat(extraArgs);
 
    /// execute function with obj scope and the given arguments
    return this.fn.apply(scope, args);
 
};
 
 
module.exports = ChainFunction;