diff --git a/src/shared/helpers/functions.ts b/src/shared/helpers/functions.ts new file mode 100644 index 0000000..c971d91 --- /dev/null +++ b/src/shared/helpers/functions.ts @@ -0,0 +1,13 @@ +export function getRandomNumber(min: number, max: number, numAfterDigit: number = 0) { + return +((Math.random() * (max - min)) + min).toFixed(numAfterDigit); +} + +export function getRandomItems(items: T[]):T[] { + const startPosition = getRandomNumber(0, items.length - 1); + const endPosition = startPosition + getRandomNumber(startPosition, items.length); + return items.slice(startPosition, endPosition); +} + +export function getRandomElement(items: T[]):T { + return items[getRandomNumber(0, items.length - 1)]; +} diff --git a/src/shared/helpers/index.ts b/src/shared/helpers/index.ts new file mode 100644 index 0000000..d67b765 --- /dev/null +++ b/src/shared/helpers/index.ts @@ -0,0 +1 @@ +export {getRandomElement, getRandomNumber, getRandomItems} from './functions.js';