diff --git a/src/components/cardbuilder/cardBuilder.js b/src/components/cardbuilder/cardBuilder.js index b09247f613..dfbc0a6768 100644 --- a/src/components/cardbuilder/cardBuilder.js +++ b/src/components/cardbuilder/cardBuilder.js @@ -18,6 +18,7 @@ import browser from '../../scripts/browser'; import { playbackManager } from '../playback/playbackmanager'; import itemShortcuts from '../shortcuts'; import imageHelper from '../../scripts/imagehelper'; +import { randomInt } from '../../utils/number.ts'; import './card.scss'; import '../../elements/emby-button/paper-icon-button-light'; import '../guide/programs.scss'; @@ -640,16 +641,6 @@ import { appRouter } from '../appRouter'; }; } - /** - * Generates a random integer in a given range. - * @param {number} min - Minimum of the range. - * @param {number} max - Maximum of the range. - * @returns {number} Randomly generated number. - */ - function getRandomInt(min, max) { - return Math.floor(Math.random() * (max - min + 1)) + min; - } - /** * Generates an index used to select the default color of a card based on a string. * @param {?string} [str] - String to use for generating the index. @@ -669,7 +660,7 @@ import { appRouter } from '../appRouter'; return (index % numRandomColors) + 1; } else { - return getRandomInt(1, numRandomColors); + return randomInt(1, numRandomColors); } } diff --git a/src/components/playback/playqueuemanager.js b/src/components/playback/playqueuemanager.js index cc27b9beec..65f20a71a2 100644 --- a/src/components/playback/playqueuemanager.js +++ b/src/components/playback/playqueuemanager.js @@ -1,3 +1,5 @@ +import { randomInt } from '../../utils/number.ts'; + let currentId = 0; function addUniquePlaylistItemId(item) { if (!item.PlaylistItemId) { @@ -56,7 +58,7 @@ class PlayQueueManager { const currentPlaylistItem = this._playlist.splice(this.getCurrentPlaylistIndex(), 1)[0]; for (let i = this._playlist.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * i); + const j = randomInt(0, i - 1); const temp = this._playlist[i]; this._playlist[i] = this._playlist[j]; this._playlist[j] = temp; diff --git a/src/plugins/logoScreensaver/plugin.js b/src/plugins/logoScreensaver/plugin.js index b1168ab5bd..fc999ccccf 100644 --- a/src/plugins/logoScreensaver/plugin.js +++ b/src/plugins/logoScreensaver/plugin.js @@ -1,4 +1,5 @@ import { PluginType } from '../../types/plugin.ts'; +import { randomInt } from '../../utils/number.ts'; export default function () { const self = this; @@ -25,16 +26,12 @@ export default function () { const elem = document.querySelector('.logoScreenSaverImage'); if (elem && elem.animate) { - const random = getRandomInt(0, animations.length - 1); + const random = randomInt(0, animations.length - 1); animations[random](elem, 1); } } - function getRandomInt(min, max) { - return Math.floor(Math.random() * (max - min + 1)) + min; - } - function bounceInLeft(elem, iterations) { const keyframes = [ { transform: 'translate3d(-3000px, 0, 0)', opacity: '0', offset: 0 }, diff --git a/src/utils/number.ts b/src/utils/number.ts index 71d1129883..16797d7d51 100644 --- a/src/utils/number.ts +++ b/src/utils/number.ts @@ -2,6 +2,16 @@ function toLocaleStringSupportsOptions() { return !!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function'); } +/** + * Generates a random integer in a given range. + * @param {number} min - Minimum of the range. + * @param {number} max - Maximum of the range. + * @returns {number} Randomly generated number. + */ +export function randomInt(min: number, max: number): number { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + /** * Gets the value of a number formatted as a perentage. * @param {number} value The value as a number.