src/compatibility/function.js

Maintainability

73.93

Lines of code

54

Created with Raphaël 2.1.002550751002019-3-122018-8-22018-5-92018-4-32018-2-282017-11-12017-6-72017-4-292017-3-22017-2-52016-12-3

2019-10-30
Maintainability: 73.93

Created with Raphaël 2.1.002550751002019-3-122018-8-22018-5-92018-4-32018-2-282017-11-12017-6-72017-4-292017-3-22017-2-52016-12-3

2019-10-30
Lines of Code: 54

Difficulty

11.37

Estimated Errors

0.24

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>2

By SLOC

Created with Raphaël 2.1.0<anonymous>14
1
/**
2
 * @file Function methods polyfill
3
 * @since 0.1.5
4
 */
5
/*#ifndef(UMD)*/
6
"use strict";
7
/*global _gpfArraySlice*/ // [].slice.call
8
/*global _gpfArrayTail*/ // [].slice.call(,1)
9
/*global _gpfBuildFunctionParameterList*/ // Builds an array of parameters
10
/*global _gpfCompatibilityInstallMethods*/ // Define and install compatible methods on standard objects
11
/*global _gpfFunc*/ // Create a new function using the source
12
/*#endif*/
13
 
14
function _generateBindBuilderSource (length) {
15
    return "var me = this;\n"
16
        + "return function (" + _gpfBuildFunctionParameterList(length).join(", ") + ") {\n"
17
        + "    var args = _gpfArraySlice(arguments);\n"
18
        + "    return me.apply(thisArg, prependArgs.concat(args));\n"
19
        + "};";
20
}
21
 
22
function _generateSimpleBindBuilderSource (length) {
23
    return "var me = this;\n"
24
        + "return function (" + _gpfBuildFunctionParameterList(length).join(", ") + ") {\n"
25
        + "    return me.apply(thisArg, arguments);\n"
26
        + "};";
27
}
28
 
29
var _GPF_COMPATIBILITY_FUNCTION_MIN_LENGTH = 0;
30
 
31
_gpfCompatibilityInstallMethods("Function", {
32
    on: Function,
33
 
34
    methods: {
35
 
36
        // Introduced with JavaScript 1.8.5
37
        bind: function (thisArg) {
38
            var me = this,
39
                prependArgs = _gpfArrayTail(arguments),
40
                length = Math.max(this.length - prependArgs.length, _GPF_COMPATIBILITY_FUNCTION_MIN_LENGTH),
41
                builderSource;
42
            if (prependArgs.length) {
43
                builderSource = _generateBindBuilderSource(length);
44
                return _gpfFunc(["thisArg", "prependArgs", "_gpfArraySlice"], builderSource)
45
                    .call(me, thisArg, prependArgs, _gpfArraySlice);
46
            }
47
            builderSource = _generateSimpleBindBuilderSource(length);
48
            return _gpfFunc(["thisArg"], builderSource)
49
                .call(me, thisArg);
50
        }
51
 
52
    }
53
 
54
});