Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

to-rotated

sindresorhus93.7kMIT1.0.0TypeScript support: included

Rotate an array by a given number of steps

array, rotate, rotation, rotated, shift, immutable, steps, cycle, cycling, cycled, move

readme

to-rotated

Rotate an array by a given number of steps

Array rotation shifts the elements left or right by a certain number of steps. Elements that move past the end reappear at the beginning. For example, rotating [1, 2, 3, 4, 5] by 2 steps to the right results in [4, 5, 1, 2, 3].

Install

npm install to-rotated

Usage

import toRotated from 'to-rotated';

console.log(toRotated([1, 2, 3, 4, 5], 2));
//=> [4, 5, 1, 2, 3]

console.log(toRotated([1, 2, 3, 4, 5], -2));
//=> [3, 4, 5, 1, 2]

API

toRotated(array, steps)

Returns a new array that is rotated by the given number of steps.