X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2Fmisc.js;h=4a2abf7188d777fd5d6cc635c83583c2818bdcbe;hb=8d61fc4ab7373b4a576f3f9108cdf7768ae27096;hp=178f3cfe2e129dcfa730b23740069d68aa602017;hpb=760865ac92508676c0047b84c5ba3e12d41d7c20;p=vchess.git diff --git a/client/src/utils/misc.js b/client/src/utils/misc.js index 178f3cfe..4a2abf71 100644 --- a/client/src/utils/misc.js +++ b/client/src/utils/misc.js @@ -23,6 +23,42 @@ export const util = return defaut; //cookie not found }, + random: function(min, max) + { + if (!max) + { + max = min; + min = 0; + } + return Math.floor(Math.random() * (max - min) ) + min; + }, + + // Inspired by https://github.com/jashkenas/underscore/blob/master/underscore.js + sample: function(arr, n) + { + n = n || 1; + let cpArr = arr.map(e => e); + for (let index = 0; index < n; index++) + { + const rand = getRandInt(index, n); + const temp = cpArr[index]; + cpArr[index] = cpArr[rand]; + cpArr[rand] = temp; + } + return cpArr.slice(0, n); + }, + + shuffle: function(arr) + { + return sample(arr, arr.length); + }, + + range: function(max) + { + return [...Array(max).keys()]; + } + + // TODO: rename into "cookie" et supprimer les deux ci-dessous // Random (enough) string for socket and game IDs getRandString: function() { @@ -35,9 +71,4 @@ export const util = { document.getElementById(elemId).click(); //or ".checked = true" }, - - translate: function(msg) - { - return translations[msg]; - }, };