@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
79 lines (77 loc) • 2.87 kB
JavaScript
/***
* d8888b. .d8b. d8b db d8888b. .d88b. .88b d88.
* 88 `8D d8' `8b 888o 88 88 `8D .8P Y8. 88'YbdP`88
* 88oobY' 88ooo88 88V8o 88 88 88 88 88 88 88 88
* 88`8b 88~~~88 88 V8o88 88 88 88 88 88 88 88
* 88 `88. 88 88 88 V888 88 .8D `8b d8' 88 88 88
* 88 YD YP YP VP V8P Y8888D' `Y88P' YP YP YP
*
*
import { getRandomInt, getRandomChance, getRandomFromArray, randomDate, generateVals, generateTitles }
from '@mikezimm/npmfunctions/dist/Services/randomServices';
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTitles = exports.generateVals = exports.randomDate = exports.getRandomFromArray = exports.getRandomChance = exports.getRandomInt = void 0;
/**
* https://stackoverflow.com/a/1527820
*
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
exports.getRandomInt = getRandomInt;
/**
* Gets a default number or a random chance to get number in range
* @param def
* @param chanceOther Enter whole number for %.... chanceOther = 49 for 49% Chance of getting number outside of default
* @param min
* @param max
*/
function getRandomChance(def, chanceOther, min, max) {
var result = def;
var thisChance = getRandomInt(1, 100);
//console.log('getRandomChance', thisChance);
if (thisChance <= chanceOther) {
//Get a randomized number instead of default
return getRandomInt(min, max);
}
else {
return def;
}
}
exports.getRandomChance = getRandomChance;
function getRandomFromArray(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
exports.getRandomFromArray = getRandomFromArray;
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
exports.randomDate = randomDate;
function generateVals(qty, min, max) {
var vals = [];
for (var i = 0; i < qty; i++) {
vals.push(getRandomInt(min, max));
}
return vals;
}
exports.generateVals = generateVals;
function generateTitles(lbl, qty) {
var titles = [];
for (var i = 0; i < qty; i++) {
//https://stackoverflow.com/a/3145054
var chr = String.fromCharCode(65 + i);
titles.push(lbl + ' - ' + chr);
}
return titles;
}
exports.generateTitles = generateTitles;
//# sourceMappingURL=randomServices.js.map
;