export function buildCycleIndexList({ length, current }: { length: number, current: number }) { const sourceList = new Array(length).fill(null).map((_, index) => index); const cycleIndexList = (() => { if (sourceList.length <= 2) { return sourceList; } const leftList = sourceList.slice(0, current); const rightList = sourceList.slice(current + 1); while (Math.abs(leftList.length - rightList.length) >= 2) { if (leftList.length > rightList.length) { rightList.push(leftList.shift()!); } else { leftList.unshift(rightList.pop()!); } } return [...leftList, current, ...rightList]; })(); const currentIndexInCycle = cycleIndexList.indexOf(current); const index2distance = cycleIndexList.reduce((prev, item, cycleIndex) => { prev[item] = cycleIndex - currentIndexInCycle; return prev; }, {} as Record); return { cycleIndexList, currentIndexInCycle, index2distance }; }