all files / lib/ mapCallbacks.js

9.09% Statements 1/11
0% Branches 0/10
0% Functions 0/2
9.09% Lines 1/11
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                                                       
/**
 * Maps a function to an array of functions, otherwise returns an empty array.
 * @param {function|function[]|undefined|null} v - initial value to map to an array
 * @returns {function[]|array} - array of functions or empty array
 */
module.exports = function mapCallbacks(v) {
    "use strict";
 
    if (typeof v === "function") {
        return [v];
    }
 
    if (Array.isArray(v)) {
        v.forEach(function(fn) {
            if (typeof fn !== "function") {
                throw new TypeError("expected function or array of functions for callbacks. Provided\n" + fn + " (" + typeof fn + ")");
            }
        });
 
        return v;
    }
 
    if (v === null || v === undefined) {
        return [];
    } else {
        throw new TypeError("expected function or array of functions for callbacks. Provided\n" + v + " (" + typeof v + ")");
    }
};