diff --git a/src/helpers.js b/src/helpers.js index 28f0e87..cc360c9 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -69,6 +69,15 @@ export function removeTags(html) { return html; } +export function shuffle(array) { + // Fisher-Yates shuffle + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + return array; +} + export function slugify(string) { return removeDiacritics(string).replace(/[^a-zA-Z0-9-_]/g, '-'); } diff --git a/src/js/ads.js b/src/js/ads.js index 8ad52cc..7306431 100644 --- a/src/js/ads.js +++ b/src/js/ads.js @@ -1,10 +1,10 @@ import { DISPLAY_AD_EVERY } from '../constants.js'; import ads from '../../ads.json'; +import { shuffle } from '../helpers.js'; export function setupAds() { - const shuffle = (a, b) => Math.random() > 0.5 ? 1 : -1; - const priority = ads.filter(ad => isActive(ad) && ad.isPriority).sort(shuffle); - const regular = ads.filter(ad => isActive(ad) && !ad.isPriority).sort(shuffle); + const priority = shuffle(ads.filter(ad => isActive(ad) && ad.isPriority)); + const regular = shuffle(ads.filter(ad => isActive(ad) && !ad.isPriority)); window.ads = [...priority, ...regular]; }