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

1.0.2 • Public • Published

rotate-array

Installation

yarn add @bgoodman/rotate-array

Usage

Import package

import rotateArray from "@bgoodman/rotate-array";

Run tests

yarn test

API

rotateArray

Rotate a 1D or 2D array by n steps. Use n < 0 for left rotations and n > 0 for right rotations.

Throws error if attempting to rotate a jagged array.

rotateArray: <T>(array: Array<T> | Array<Array<T>>, steps: number) => Array<T> | Array<Array<T>>

Example

  • rotate a 1D array right
rotateArray([1,2,3,4], 1)
//[4,1,2,3]
  • Rotate a 1D array left
rotateArray([1,2,3,4], -1)
//[2,3,4,1]
  • Rotate a 2D array right
rotateArray([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], 1)
//([[10,7,4,1], [11,8,5,2], [12,9,6,3]])
  • Rotate a 2D array left
rotateArray([[1,2,3],[4,5,6],[7,8,9],[10,11,12]], -1)
//[[3,6,9,12], [2,5,8,11], [1,4,7,10]]

Readme

Keywords

none

Package Sidebar

Install

npm i @bgoodman/rotate-array

Weekly Downloads

5

Version

1.0.2

License

MIT

Unpacked Size

32 kB

Total Files

23

Last publish

Collaborators

  • bgoodman