29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
import type { Prisma } from '@prisma/client';
|
|
|
|
type PositionUpdate = Prisma.PrismaPromise<unknown>;
|
|
|
|
/** کارت را در ایندکس خواستهشده میگذارد. ایندکس نسبت به فهرست بدون خود کارت است. */
|
|
export function placeId(ids: string[], movingId: string, index: number): string[] {
|
|
const next = ids.filter((id) => id !== movingId);
|
|
const at = Math.max(0, Math.min(Math.trunc(index), next.length));
|
|
next.splice(at, 0, movingId);
|
|
return next;
|
|
}
|
|
|
|
export function sameIds(left: string[], right: string[]): boolean {
|
|
return left.length === right.length && left.every((id, index) => id === right[index]);
|
|
}
|
|
|
|
/** ترتیب ستون را ۰ تا n ذخیره میکند. */
|
|
export async function writePositions(
|
|
transaction: (ops: PositionUpdate[]) => Promise<unknown>,
|
|
ids: string[],
|
|
update: (id: string, position: number) => PositionUpdate,
|
|
) {
|
|
const size = 60;
|
|
for (let start = 0; start < ids.length; start += size) {
|
|
const slice = ids.slice(start, start + size);
|
|
await transaction(slice.map((id, offset) => update(id, start + offset)));
|
|
}
|
|
}
|