compute-circshift

Shifts array elements (or string characters) circularly.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
compute-circshift
1.0.010 years ago10 years agoMinified + gzip package size for compute-circshift in KB

Readme

circshift
!NPM versionnpm-imagenpm-url !Build Statustravis-imagetravis-url !Coverage Statuscoveralls-imagecoveralls-url !Dependenciesdependencies-imagedependencies-url
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 = require( 'compute-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:
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.

Examples

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

Notes

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' ) );

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

MIT license.

Copyright

Copyright © 2014. Athan Reines.