2017-04-19 21:19:33 +02:00
|
|
|
//General helper functions
|
|
|
|
|
|
|
|
//Returns the size (number of keys) of an object
|
|
|
|
function sizeOfObject(obj) {
|
|
|
|
var size = 0, key;
|
|
|
|
for (key in obj) {
|
|
|
|
if (obj.hasOwnProperty(key)) size++;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Adds a random offset to a number within a certain percentage
|
2017-08-30 19:44:29 +02:00
|
|
|
//e.g. addOffset(100, 5) will return anything from 95 to 105.
|
2017-04-19 21:19:33 +02:00
|
|
|
//The percentage argument must be between 0 and 100;
|
|
|
|
function addOffset(n, percentage) {
|
2017-05-06 08:24:01 +02:00
|
|
|
if (percentage < 0 || percentage > 100) {return;}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-04-19 21:19:33 +02:00
|
|
|
var offset = n * (percentage / 100);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-05-06 08:24:01 +02:00
|
|
|
return n + ((Math.random() * (2 * offset)) - offset);
|
2017-05-05 23:27:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Given an element by its Id(usually an 'a' element), removes all event listeners
|
|
|
|
//from that element by cloning and replacing. Then returns the new cloned element
|
|
|
|
function clearEventListeners(elemId) {
|
|
|
|
var elem = document.getElementById(elemId);
|
|
|
|
if (elem == null) {console.log("ERR: Could not find element for: " + elemId); return null;}
|
|
|
|
var newElem = elem.cloneNode(true);
|
|
|
|
elem.parentNode.replaceChild(newElem, elem);
|
2017-05-06 08:24:01 +02:00
|
|
|
return newElem;
|
2017-06-02 06:15:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 23:27:31 +02:00
|
|
|
//Same as clearEventListeners except it takes a DOM element object rather than an ID
|
|
|
|
function clearEventListenersEl(el) {
|
|
|
|
if (el == null) {console.log("ERR: element passed into clearEventListenersEl is null"); return null;}
|
|
|
|
var newElem = el.cloneNode(true);
|
|
|
|
el.parentNode.replaceChild(newElem, el);
|
|
|
|
return newElem;
|
|
|
|
}
|
|
|
|
|
2017-06-02 06:15:45 +02:00
|
|
|
function getRandomInt(min, max) {
|
2017-06-19 16:54:11 +02:00
|
|
|
if (min > max) {return getRandomInt(max, min);}
|
2017-06-02 06:15:45 +02:00
|
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
2017-06-17 04:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Returns true if all elements are equal, and false otherwise
|
|
|
|
//Assumes both arguments are arrays and that there are no nested arrays
|
|
|
|
function compareArrays(a1, a2) {
|
|
|
|
if (a1.length != a2.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-06-17 04:53:57 +02:00
|
|
|
for (var i = 0; i < a1.length; ++i) {
|
|
|
|
if (a1[i] != a2[i]) {return false;}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function printArray(a) {
|
|
|
|
return "[" + a.join(", ") + "]";
|
2017-07-13 18:54:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Returns bool indicating whether or not its a power of 2
|
|
|
|
function powerOfTwo(n) {
|
|
|
|
if (isNaN(n)) {return false;}
|
|
|
|
return n && (n & (n-1)) === 0;
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export {sizeOfObject, addOffset, clearEventListeners, getRandomInt,
|
2017-09-21 23:27:31 +02:00
|
|
|
compareArrays, printArray, powerOfTwo, clearEventListenersEl};
|