Merge pull request #4413 from thornbill/random-int-util

This commit is contained in:
Bill Thornton 2023-03-14 08:04:11 -04:00 committed by GitHub
commit c90a933f56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 17 deletions

View file

@ -18,6 +18,7 @@ import browser from '../../scripts/browser';
import { playbackManager } from '../playback/playbackmanager'; import { playbackManager } from '../playback/playbackmanager';
import itemShortcuts from '../shortcuts'; import itemShortcuts from '../shortcuts';
import imageHelper from '../../scripts/imagehelper'; import imageHelper from '../../scripts/imagehelper';
import { randomInt } from '../../utils/number.ts';
import './card.scss'; import './card.scss';
import '../../elements/emby-button/paper-icon-button-light'; import '../../elements/emby-button/paper-icon-button-light';
import '../guide/programs.scss'; 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. * 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. * @param {?string} [str] - String to use for generating the index.
@ -669,7 +660,7 @@ import { appRouter } from '../appRouter';
return (index % numRandomColors) + 1; return (index % numRandomColors) + 1;
} else { } else {
return getRandomInt(1, numRandomColors); return randomInt(1, numRandomColors);
} }
} }

View file

@ -1,3 +1,5 @@
import { randomInt } from '../../utils/number.ts';
let currentId = 0; let currentId = 0;
function addUniquePlaylistItemId(item) { function addUniquePlaylistItemId(item) {
if (!item.PlaylistItemId) { if (!item.PlaylistItemId) {
@ -56,7 +58,7 @@ class PlayQueueManager {
const currentPlaylistItem = this._playlist.splice(this.getCurrentPlaylistIndex(), 1)[0]; const currentPlaylistItem = this._playlist.splice(this.getCurrentPlaylistIndex(), 1)[0];
for (let i = this._playlist.length - 1; i > 0; i--) { 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]; const temp = this._playlist[i];
this._playlist[i] = this._playlist[j]; this._playlist[i] = this._playlist[j];
this._playlist[j] = temp; this._playlist[j] = temp;

View file

@ -1,4 +1,5 @@
import { PluginType } from '../../types/plugin.ts'; import { PluginType } from '../../types/plugin.ts';
import { randomInt } from '../../utils/number.ts';
export default function () { export default function () {
const self = this; const self = this;
@ -25,16 +26,12 @@ export default function () {
const elem = document.querySelector('.logoScreenSaverImage'); const elem = document.querySelector('.logoScreenSaverImage');
if (elem && elem.animate) { if (elem && elem.animate) {
const random = getRandomInt(0, animations.length - 1); const random = randomInt(0, animations.length - 1);
animations[random](elem, 1); animations[random](elem, 1);
} }
} }
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function bounceInLeft(elem, iterations) { function bounceInLeft(elem, iterations) {
const keyframes = [ const keyframes = [
{ transform: 'translate3d(-3000px, 0, 0)', opacity: '0', offset: 0 }, { transform: 'translate3d(-3000px, 0, 0)', opacity: '0', offset: 0 },

View file

@ -2,6 +2,16 @@ function toLocaleStringSupportsOptions() {
return !!(typeof Intl === 'object' && Intl && typeof Intl.NumberFormat === 'function'); 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. * Gets the value of a number formatted as a perentage.
* @param {number} value The value as a number. * @param {number} value The value as a number.