All files / src/utils/components/class-card-banner generateClassCardShapes.js

100% Statements 14/14
100% Branches 3/3
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 313x             91x 91x 552x 552x   91x 3341x 3341x 3341x 3341x         85x 667x                
const SHAPE_TYPES = ["circle", "ring", "square", "squareOutline", "triangle"];
 
// Deterministic seeded PRNG (mulberry32) so a given seed always produces the
// same shape layout across reloads, without needing to persist anything.
// Exported so other seed-driven decorative pieces (e.g. the banner's accent
// color) can share the same hashing behavior instead of duplicating it.
export function seededRandom(seed) {
    let h = 1779033703 ^ seed.length;
    for (let i = 0; i < seed.length; i++) {
        h = Math.imul(h ^ seed.charCodeAt(i), 3432918353);
        h = (h << 13) | (h >>> 19);
    }
    return () => {
        h = Math.imul(h ^ (h >>> 16), 2246822519);
        h = Math.imul(h ^ (h >>> 13), 3266489917);
        h ^= h >>> 16;
        return (h >>> 0) / 4294967296;
    };
}
 
export function generateClassCardShapes(seed, count = 4) {
    const rand = seededRandom(seed || "class");
    return Array.from({ length: count }, () => ({
        type: SHAPE_TYPES[Math.floor(rand() * SHAPE_TYPES.length)],
        cx: 8 + rand() * 84,
        cy: 8 + rand() * 84,
        r: 10 + rand() * 20,
        rotate: rand() * 360,
    }));
}