Passed
Pull Request — master (#19)
by Eric
02:50 queued 01:09
created

utilities.js (1 issue)

Severity
1
/*!
2
 * utilities.js | v0.7.0 | Utility functions for front-end JavaScript development
3
 * Copyright (c) 2017 Eric Zieger (MIT license)
4
 * https://github.com/theZieger/utilitiesjs/blob/master/LICENSE
5
 */
6 1
(function(root, factory) {
7
    /** global: define */
8 4
    if (typeof define === "function" && define.amd) {
9
        define(["utilities"], factory);
10
    /** global: module */
11 4
    } else if (typeof module === "object" && module.exports) {
12 1
        module.exports = factory();
13
    } else {
14
        root.utilities = factory();
15
    }
16
}(this, function(undefined) {
17
18
    /**
19
     * inherit the prototype of the SuperConstructor
20
     * 
21
     * Warning: Changing the prototype of an object is, by the nature of how modern JavaScript engines
22
     * optimize property accesses, a very slow operation, in every browser and JavaScript engine.
23
     * So instead of using Object.setPrototypeOf or messing with __proto__, we create a new object
24
     * with the desired prototype using Object.create().
25
     *
26
     * @see https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
27
     *
28
     * @param {Object} Constructor
29
     * @param {Object} SuperConstructor
30
     *
31
     * @throws {TypeError} if arguments are null/undefined, or SuperConstructor has no prototype
32
     *
33
     * @returns {Void}
34
     */
35 1
    var inherits = function(Constructor, SuperConstructor) {
36 6
        if (Constructor === undefined || Constructor === null) {
37 2
            throw new TypeError('Constructor argument is undefined or null');
38
        }
39
40 4
        if (SuperConstructor === undefined || SuperConstructor === null) {
41 2
            throw new TypeError('SuperConstructor argument is undefined or null');
42
        }
43
44 2
        if (SuperConstructor.prototype === undefined) {
45 1
            throw new TypeError('SuperConstructor.prototype is undefined');
46
        }
47
48
        // for convenience, SuperConstructor will be accessible through the Constructor.super_ property
49 1
        Constructor.super_ = SuperConstructor;
50
51 1
        Constructor.prototype = Object.create(SuperConstructor.prototype);
52 1
        Constructor.prototype.constructor = Constructor;
53
    };
54
55
    /**
56
     * Turns Array (of Objects) into associative Object (by given mapBy when given)
57
     *
58
     * @param {Array}  arr
59
     * @param {String} mapBy  optional mapping key
60
     *
61
     * @throws {TypeError} if arr is not an Array or mapBy is set but not a String
62
     *
63
     * @returns {Object}
64
     */
65 1
    var toObject = function(arr, mapBy) {
66 5
        var obj = {};
67
68 5
        if (!Array.isArray(arr)) {
69 2
            throw new TypeError('arr argument is not of type Array');
70
        }
71
72 4
        if (typeof mapBy !== 'string' && mapBy != null) {
0 ignored issues
show
Comparing mapBy to null using the != operator is not safe. Consider using !== instead.
Loading history...
73 1
            throw new TypeError('mapBy argument is not of type String');
74
        }
75
76 2
        arr.forEach(function(val, i) {
77 7
            if (!mapBy) {
78 4
                obj[i] = val;
79 3
            } else if (
80
                typeof val[mapBy] === 'string'
81
                || typeof val[mapBy] === 'number'
82
            ) {
83 3
                obj[val[mapBy]] = val;
84
            }
85
        });
86
      
87 2
        return obj;
88
    };
89
90 1
    return {
91
        inherits: inherits,
92
        toObject: toObject
93
    };
94
}));
95