Improve ad shuffling
This commit is contained in:
parent
e2b5f1cd83
commit
386b2e626e
|
@ -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, '-');
|
||||
}
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue