1 | /*! |
||
2 | * utilities.js | v0.6.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 | (function(root, factory) { |
||
7 | if (typeof define === "function" && define.amd) { |
||
0 ignored issues
–
show
|
|||
8 | define(["utilities"], factory); |
||
9 | } else if (typeof module === "object" && module.exports) { |
||
10 | module.exports = factory(); |
||
11 | } else { |
||
12 | root.utilities = factory(); |
||
13 | } |
||
14 | }(this, function(undefined) { |
||
15 | |||
16 | /** |
||
17 | * inherit the prototype of the SuperConstructor |
||
18 | * |
||
19 | * Warning: Changing the prototype of an object is, by the nature of how modern JavaScript engines |
||
20 | * optimize property accesses, a very slow operation, in every browser and JavaScript engine. |
||
21 | * So instead of using Object.setPrototypeOf or messing with __proto__, we create a new object |
||
22 | * with the desired prototype using Object.create(). |
||
23 | * |
||
24 | * @see https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf |
||
25 | * |
||
26 | * @param {Object} Constructor |
||
27 | * @param {Object} SuperConstructor |
||
28 | * |
||
29 | * @throws {TypeError} if arguments are null/undefined, or SuperConstructor has no prototype |
||
30 | * |
||
31 | * @returns {Void} |
||
32 | */ |
||
33 | var inherits = function(Constructor, SuperConstructor) { |
||
34 | if (Constructor === undefined || Constructor === null) { |
||
35 | throw new TypeError('Constructor argument is undefined or null'); |
||
36 | } |
||
37 | |||
38 | if (SuperConstructor === undefined || SuperConstructor === null) { |
||
39 | throw new TypeError('SuperConstructor argument is undefined or null'); |
||
40 | } |
||
41 | |||
42 | if (SuperConstructor.prototype === undefined) { |
||
43 | throw new TypeError('SuperConstructor.prototype is undefined'); |
||
44 | } |
||
45 | |||
46 | // for convenience, SuperConstructor will be accessible through the Constructor.super_ property |
||
47 | Constructor.super_ = SuperConstructor; |
||
48 | |||
49 | Constructor.prototype = Object.create(SuperConstructor.prototype); |
||
50 | Constructor.prototype.constructor = Constructor; |
||
51 | }; |
||
52 | |||
53 | /** |
||
54 | * Turns Array (of Objects) into associative Object (by given mapBy when given) |
||
55 | * |
||
56 | * @param {Array} arr |
||
57 | * @param {String} mapBy optional mapping key |
||
58 | * |
||
59 | * @throws {TypeError} if arr is not an Array or mapBy is set but not a String |
||
60 | * |
||
61 | * @returns {Object} |
||
62 | */ |
||
63 | var toObject = function(arr, mapBy) { |
||
64 | var obj = {}; |
||
65 | |||
66 | if (typeof mapBy !== 'string' && mapBy != null) { |
||
67 | throw new TypeError('mapBy argument is not of type String'); |
||
68 | } |
||
69 | |||
70 | if (!Array.isArray(arr)) { |
||
71 | throw new TypeError('arr argument is not of type Array'); |
||
72 | } |
||
73 | |||
74 | arr.forEach(function(val, i) { |
||
75 | if (!mapBy) { |
||
76 | obj[i] = val; |
||
77 | } else if ( |
||
78 | typeof val[mapBy] === 'string' |
||
79 | || typeof val[mapBy] === 'number' |
||
80 | ) { |
||
81 | obj[val[mapBy]] = val; |
||
82 | } |
||
83 | }); |
||
84 | |||
85 | return obj; |
||
86 | }; |
||
87 | |||
88 | return { |
||
89 | inherits: inherits, |
||
90 | toObject: toObject |
||
91 | }; |
||
92 | })); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.