Shifts array items by `n` steps. If `n` is greater than 0, items will shift `n` steps to the right. If `n` is less than 0, items will shift `n` steps to the left.
shift([1, 2, 3], 1); // [3, 1, 2]shift([1, 2, 3], -1); // [2, 3, 1]
npx atmx add helper shift
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
/*** Shifts array items by `n` steps. If `n` is greater than 0, items* will shift `n` steps to the right. If `n` is less than 0, items* will shift `n` steps to the left.** @example* shift([1, 2, 3], 1) // [3, 1, 2]* shift([1, 2, 3], -1) // [2, 3, 1]*/export function shift<T>(arr: readonly T[], n: number): T[] {if (arr.length === 0) {return [...arr];} const shiftNumber = n % arr.length; if (shiftNumber === 0) {return [...arr];} return [...arr.slice(-shiftNumber, arr.length),...arr.slice(0, -shiftNumber),];}