Implemented new set playlist position mode

This commit is contained in:
patben8 committed 2026-10-15 10:48:14 +02:00
1 parent 8230722105
commit 7b2d6de411
6 files changed
+129 -26

No files matched your search

+19
View File
@@ -27,6 +27,25 @@ describe('moveToPosition', () => {
})
})
describe('moveToPosition with a view order', () => {
const sorted = (ids: string[], position: number, view: string[]) =>
moveToPosition(order, new Set(ids), position, view).join('')
it('moves the block in the order the view shows it', () => {
expect(sorted(['B', 'E', 'F'], 1, ['F', 'B', 'E'])).toBe('FBEACDG')
expect(sorted(['B', 'E', 'F'], 3, ['E', 'F', 'B'])).toBe('ACEFBDG')
})
it('ignores view ids that are not moving', () => {
expect(sorted(['B', 'D'], 1, ['G', 'D', 'A', 'B'])).toBe('DBACEFG')
})
it('keeps a twice-listed track together, and appends a track the view lacks', () => {
expect(moveToPosition(['A', 'B', 'A', 'C'], new Set(['A', 'C']), 1, ['C', 'A']).join('')).toBe('CAAB')
expect(sorted(['B', 'C'], 1, ['C'])).toBe('CBADEFG')
})
})
describe('maxPosition', () => {
it('leaves room for the block behind its first track', () => {
expect(maxPosition(order, new Set(['B']))).toBe(7)
+29 -7
View File
@@ -1,10 +1,17 @@
/** A playlist's entry order with `moving` lifted out as one block, kept in
* their PLAYLIST order, and put back so the block's first track lands at the
* 1-based `position` of the result. A position past the end puts the block
* last. A track listed twice in the playlist moves with both entries, since a
* selection is by track id. */
export function moveToPosition(order: string[], moving: ReadonlySet<string>, position: number): string[] {
const block = order.filter((id) => moving.has(id))
/** A playlist's entry order with `moving` lifted out as one block and put
* back so the block's first track lands at the 1-based `position` of the
* result. A position past the end puts the block last. The block keeps the
* tracks' PLAYLIST order, or with `blockOrder` (the ids as a sorted view
* shows them) that order instead — so a sort can be written into the
* playlist. A track listed twice in the playlist moves with both entries,
* since a selection is by track id; in `blockOrder` they stay together. */
export function moveToPosition(
order: string[],
moving: ReadonlySet<string>,
position: number,
blockOrder?: readonly string[],
): string[] {
const block = blockOrder ? orderBlock(order, moving, blockOrder) : order.filter((id) => moving.has(id))
const rest = order.filter((id) => !moving.has(id))
const at = Math.min(Math.max(Math.floor(position), 1) - 1, rest.length)
return [...rest.slice(0, at), ...block, ...rest.slice(at)]
@@ -15,3 +22,18 @@ export function moveToPosition(order: string[], moving: ReadonlySet<string>, pos
export function maxPosition(order: string[], moving: ReadonlySet<string>): number {
return order.length - order.filter((id) => moving.has(id)).length + 1
}
/** The moving entries in `blockOrder`'s order, each track's repeats together.
* A moving track `blockOrder` lacks (none should) follows in playlist order. */
function orderBlock(order: string[], moving: ReadonlySet<string>, blockOrder: readonly string[]): string[] {
const counts = new Map<string, number>()
for (const id of order) if (moving.has(id)) counts.set(id, (counts.get(id) ?? 0) + 1)
const block: string[] = []
for (const id of [...blockOrder, ...order]) {
const n = counts.get(id)
if (!n) continue
for (let i = 0; i < n; i++) block.push(id)
counts.delete(id)
}
return block
}