Shifts array elements (or string characters) circularly.
$ npm install compute-circshift
For use in the browser, use browserify.
To use the module,
var circshift = require( 'compute-circshift' );
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:
circshift( arr, 2 );
// returns [ 4, 5, 1, 2, 3 ]
// Circularly shift the mutated array 3 positions to the left:
circshift( arr, -3 );
// returns [ 2, 3, 4, 5, 1 ]
// Strings...
var str = 'beepboop';
str = circshift( str, 3 );
// returns 'oopbeepb'
str = circshift( str, -4 );
// returns 'eepboopb'
Note: mutates an input array
.
var circshift = require( 'compute-circshift' );
// Simulate some data...
var data = new Array( 10 ),
len = data.length;
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.random() - 0.5;
k = Math.round( rand * len * 2 );
circshift( data, k );
console.log( data.join( ',' ) );
}
To run the example code from the top-level application directory,
$ node ./examples/index.js
If provided an input array
, the array
is mutated. If mutation is undesired,
var data = [ 1, 2, 3, 4, 5 ],
copy = data.slice();
circshift( copy, 2 );
console.log( copy.join( '\n' ) );
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.
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
Copyright © 2014. Athan Reines.