rotate-array
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

rotate-array

Rotates the elements of an array in place. Supports rotation in both directions and automatically wraps rotations which are larger than the input array size.

Build status devDependency status NPM version

Browser support

Installation

npm i rotate-array

Alternatives

For usage via AMD / <script>, download a UMD bundle from wzrd.in.

Usage

var rotate = require('rotate-array');

rotate(array, num)

Rotates the array num places to the left, i.e. it shifts num items out of the array and pushes them back on the end. The reverse is done when num is negative. In addition, rotate automatically wraps rotations which are larger than array.length.

Examples:

var beatles = ['paul', 'john', 'ringo', 'george'];
rotate(beatles, 2);
console.log(beatles); // [ 'ringo', 'george', 'paul', 'john' ]
var beatles = ['paul', 'john', 'ringo', 'george'];
rotate(beatles, -3);
console.log(beatles); // [ 'john', 'ringo', 'george', 'paul' ]
var beatles = ['paul', 'john', 'ringo', 'george'];
rotate(beatles, 42);
console.log(beatles); // [ 'ringo', 'george', 'paul', 'john' ]

rotate.all(array)

Returns all rotations for the given array. It does not modify the passed in array.

Example:

var beatles = ['paul', 'john', 'ringo', 'george'];
console.log(rotate.all(beatles));
// [ [ 'paul', 'john', 'ringo', 'george' ],
//   [ 'john', 'ringo', 'george', 'paul' ],
//   [ 'ringo', 'george', 'paul', 'john' ],
//   [ 'george', 'paul', 'john', 'ringo' ] ]

Readme

Keywords

Package Sidebar

Install

npm i rotate-array

Weekly Downloads

879

Version

1.2.0

License

BSD-2-Clause

Unpacked Size

8.24 kB

Total Files

5

Last publish

Collaborators

  • cmtegner