Make shuffle reorder and restore the play queue, add off/all/one repeat playback, and remove Resonant Radio UI.
Hop-State: A_06FNNZKG8MKNEJA6QXQ1ZER Hop-Proposal: R_06FNNZJMKS1Z9RBS9ADD3ER Hop-Task: T_06FNNXTJEGA5TEAZ2GA55X0 Hop-Attempt: AT_06FNNXTJEHPH9SQPGKC0P9G
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { nextRepeatMode, shuffleTracks } from "./playback";
|
||||
|
||||
describe("shuffleTracks", () => {
|
||||
it("shuffles a copy without changing the source queue", () => {
|
||||
const source = ["first", "second", "third", "fourth"];
|
||||
const randomValues = [0, 0.5, 0.25];
|
||||
let call = 0;
|
||||
|
||||
const shuffled = shuffleTracks(source, () => randomValues[call++]);
|
||||
|
||||
expect(shuffled).toEqual(["third", "fourth", "second", "first"]);
|
||||
expect(source).toEqual(["first", "second", "third", "fourth"]);
|
||||
expect(shuffled).toHaveLength(source.length);
|
||||
expect(new Set(shuffled)).toEqual(new Set(source));
|
||||
});
|
||||
});
|
||||
|
||||
describe("nextRepeatMode", () => {
|
||||
it("cycles through off, all, one, and back to off", () => {
|
||||
expect(nextRepeatMode("off")).toBe("all");
|
||||
expect(nextRepeatMode("all")).toBe("one");
|
||||
expect(nextRepeatMode("one")).toBe("off");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
export type RepeatMode = "off" | "all" | "one";
|
||||
|
||||
export function nextRepeatMode(mode: RepeatMode): RepeatMode {
|
||||
if (mode === "off") return "all";
|
||||
if (mode === "all") return "one";
|
||||
return "off";
|
||||
}
|
||||
|
||||
export function shuffleTracks<T>(items: readonly T[], random: () => number = Math.random): T[] {
|
||||
const shuffled = [...items];
|
||||
|
||||
for (let index = shuffled.length - 1; index > 0; index -= 1) {
|
||||
const swapIndex = Math.floor(random() * (index + 1));
|
||||
[shuffled[index], shuffled[swapIndex]] = [shuffled[swapIndex], shuffled[index]];
|
||||
}
|
||||
|
||||
return shuffled;
|
||||
}
|
||||
Reference in New Issue
Block a user