File size: 1,238 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
class SeedManager {
private localSeed: string | null = null;
static SESSION_STORAGE_KEY = 'goals-step-seed';
/**
* Clears the cached seed. DO NOT USE - only for testing purposes.
* @internal
*/
clearSeed() {
this.localSeed = null;
sessionStorage.removeItem( SeedManager.SESSION_STORAGE_KEY );
}
getSeed(): number {
const existingSeed =
this.localSeed ?? sessionStorage.getItem( SeedManager.SESSION_STORAGE_KEY );
if ( existingSeed ) {
return parseInt( existingSeed, 10 );
}
const seed = Math.floor( Math.random() * 100 );
try {
sessionStorage.setItem( SeedManager.SESSION_STORAGE_KEY, seed.toString() );
} catch {
} finally {
this.localSeed = seed.toString();
}
return seed;
}
}
/**
* A singleton instance of the SeedManager. This is only exported for testing purposes.
* @internal
*/
export const seedManager = new SeedManager();
export const shuffleArray = < T >( array: T[] ): T[] => {
let value = seedManager.getSeed();
const seededRandom = () => {
value = ( value * 9301 + 49297 ) % 233280;
return value / 233280;
};
return array
.map( ( value ) => ( { value, sort: seededRandom() } ) )
.sort( ( a, b ) => a.sort - b.sort )
.map( ( { value } ) => value );
};
|