circshift
Shifts array elements (or string characters) circularly.
Installation
$ npm install compute-circshift
For use in the browser, use browserify.
Usage
To use the module,
var circshift = ;
circshift( x, k )
Shifts elements. x
may be either an array
or a string
. k
is an integer
specifying how many positions to shift.
// Arrays...var arr = 1 2 3 4 5 ;// Circularly shift the array 2 positions to the right:;// returns [ 4, 5, 1, 2, 3 ]// Circularly shift the mutated array 3 positions to the left:;// returns [ 2, 3, 4, 5, 1 ]// Strings...var str = 'beepboop';str = ;// returns 'oopbeepb'str = ;// returns 'eepboopb'
Note: mutates an input array
.
Examples
var circshift = ;// Simulate some data...var data = 10len = datalength;for var i = 0; i < len; i++data i = i;// Repeatedly shift elements a random number of positions...var rand k;for var j = 0; j < 20; j++rand = Math - 05;k = Math;;console;
To run the example code from the top-level application directory,
$ node ./examples/index.js
Notes
If provided an input array
, the array
is mutated. If mutation is undesired,
var data = 1 2 3 4 5copy = data;;console;
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2014. Athan Reines.