diff --git a/src/js/utils.js b/js/utils.js similarity index 88% rename from src/js/utils.js rename to js/utils.js index eca740c543cbcfbf5729fa9fbd766671d7152e05..cf39ddc1d09fa1ff3447db4e4f225850370d1e47 100644 --- a/src/js/utils.js +++ b/js/utils.js @@ -1,3 +1,4 @@ +/* eslint-disable no-unused-vars */ /* This file contains only JS functions without dependencies on FC specific variables/conventions and do not rely on * custom functions outside this file */ @@ -7,17 +8,17 @@ * @param {any} x * @returns {boolean} */ -window.jsDef = function(x) { +function jsDef(x) { return (typeof x !== "undefined" && x !== null && x !== undefined); -}; +} /** * @param {number} n * @returns {boolean} */ -window.isFloat = function(n) { +function isFloat(n) { return Number.isFinite(n) && Math.floor(n) !== n; -}; +} /** * Determines if a is between low and high @@ -26,17 +27,17 @@ window.isFloat = function(n) { * @param {number} high * @returns {boolean} */ -window.between = function(a, low, high) { +function between(a, low, high) { if (low === null) { low = -Infinity; } if (high === null) { high = Infinity; } return (a > low && a < high); -}; +} /** * @param {number[]} obj * @returns {number} */ -window.hashChoice = function hashChoice(obj) { +function hashChoice(obj) { let randint = Math.floor(Math.random() * hashSum(obj)); let ret; Object.keys(obj).some((key) => { @@ -49,37 +50,37 @@ window.hashChoice = function hashChoice(obj) { } }); return ret; -}; +} /** * @param {number[]} obj * @returns {number} */ -window.hashSum = function hashSum(obj) { +function hashSum(obj) { let sum = 0; Object.keys(obj).forEach((key) => { sum += obj[key]; }); return sum; -}; +} /** * @param {Array} arr * @returns {Object} */ -window.arr2obj = function arr2obj(arr) { +function arr2obj(arr) { const obj = {}; arr.forEach((item) => { obj[item] = 1; }); return obj; -}; +} /** * @param {{}} object * @param rest */ -window.hashPush = function hashPush(object, ...rest) { +function hashPush(object, ...rest) { rest.forEach((item) => { if (object[item] === undefined) { object[item] = 1; @@ -87,13 +88,13 @@ window.hashPush = function hashPush(object, ...rest) { object[item] += 1; } }); -}; +} /** * @param {[]} array * @return {{}} */ -window.weightedArray2HashMap = function weightedArray2HashMap(array) { +function weightedArray2HashMap(array) { const obj = {}; array.forEach((item) => { if (obj[item] === undefined) { @@ -103,14 +104,14 @@ window.weightedArray2HashMap = function weightedArray2HashMap(array) { } }); return obj; -}; +} /** * generate a random, almost unique ID that is compliant (possibly) with RFC 4122 * * @return {string} */ -window.generateNewID = function generateNewID() { +function generateNewID() { let date = Date.now(); // high-precision timer let uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) { let r = (date + Math.random() * 16) % 16 | 0; @@ -118,44 +119,44 @@ window.generateNewID = function generateNewID() { return (c === "x" ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; -}; +} /** * @param {Array} array * @param {number} indexA * @param {number} indexB */ -window.arraySwap = function arraySwap(array, indexA, indexB) { +function arraySwap(array, indexA, indexB) { const tmp = array[indexA]; array[indexA] = array[indexB]; array[indexB] = tmp; -}; +} /** * @param {string} string * @returns {string} */ -window.capFirstChar = function capFirstChar(string) { +function capFirstChar(string) { return string.charAt(0).toUpperCase() + string.substr(1); -}; +} /** * @param {string} word * @returns {string} */ -window.addA = function(word) { +function addA(word) { let vocal = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']; if (vocal.includes(word.charAt(0))) { return `an ${word}`; } return `a ${word}`; -}; +} /** * @param {number} i * @returns {string} */ -window.ordinalSuffix = function ordinalSuffix(i) { +function ordinalSuffix(i) { let j = i % 10; let k = i % 100; if (j === 1 && k !== 11) { @@ -168,27 +169,27 @@ window.ordinalSuffix = function ordinalSuffix(i) { return `${i}rd`; } return `${i}th`; -}; +} /** * @param {number} i * @returns {string} */ -window.ordinalSuffixWords = function(i) { +function ordinalSuffixWords(i) { const text = ["zeroth", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth"]; if (i < text.length) { return text[i]; } return ordinalSuffix(i); -}; +} /** * @param {Iterable<any>} array * @returns {any[]} */ -window.removeDuplicates = function removeDuplicates(array) { +function removeDuplicates(array) { return [...new Set(array)]; -}; +} /** * Maps an index from one list onto a matching index on the other. @@ -230,13 +231,13 @@ App.Utils.escapeHtml = function(text) { * @param {Iterable} list * @return {{}} */ -window.mapIdList = function(list) { +function mapIdList(list) { let mappedList = {}; for (const item of list) { mappedList[item.id] = item; } return mappedList; -}; +} /** * Topological sorting algorithm