Nodejs Array Rotate rotate( dir )

Here you can find the source of rotate( dir )

Method Source Code

Array.prototype.rotate = function ( dir ) {
    var that = this,
        direction = dir !== "ccw",
        columns = that[0].length,
        flat = [],/*from  w  w  w  . j a v a 2s . co m*/
        tmp = [];

    /* check if used array has proper structure */
    if ( !(that.length) || !columns ) {
        console.error("incompatible array lengths");
        return false;
    }

    /* flatten array entries */
    for ( var i = 0, l = that.length; i < l; i++ ) {
        for ( var j = 0, m = that[i].length; j < m; j++ ) {
            if (direction) {
                flat.push(that[i][j]);
            } else {
                flat.unshift(that[i][j]);
            }
        }
    }

    /* set up modified array structure */
    for ( var k = 0, n = flat.length; k < n; k++ ) {
        var index = k % columns;
        if ( !tmp[index] ) {
            tmp[index] = [flat[k]];
        } else {
            tmp[index].unshift(flat[k]);
        }
    }

    return tmp;
};

Related

  1. roll()
    Array.prototype.roll = function() {
        var rand = randomNumber(this.length);
        var tem = [];
        for (var i = rand; i < this.length; i++) {
            tem.push(this[i]);
        for (var i = 0; i < rand; i++) {
            tem.push(this[i]);
        return tem;
    
  2. rotate( ()
    Array.prototype.rotate = (function () {
        var push = Array.prototype.push,
            splice = Array.prototype.splice;
        return function (count) {
            var len = this.length >>> 0,
                count = count >> 0;
            count = ((count % len) + len) % len;
            push.apply(this, splice.call(this, 0, count));
            return this;
    ...
    
  3. rotate( n )
    Array.prototype.rotate = function( n ) {
      this.unshift.apply( this, this.splice( n, this.length ) )
      return this;
    
  4. rotate(()
    Array.prototype.rotate = (function(){
      var unshift = Array.prototype.unshift,
      push=Array.prototype.push,
      splice=Array.prototype.splice;
      return function(count){
        var len=this.length >>>0,
        count = count >> 0;
        count = count % len;
        push.apply(this,splice.call(this,count,len));
    ...
    
  5. rotate(()
    'use strict';
    Array.prototype.rotate = (function() {
        var unshift = Array.prototype.unshift;
        var splice = Array.prototype.splice;
        return function(count) {
            var len = this.length >>> 0;
            count = count >> 0;
            unshift.apply(this, splice.call(this, count % len, len));
            return this;
    ...
    
  6. rotate(()
    export function zeroOrOne() {
      return Math.round(Math.random() * 1)
    export function arrayShuffle(a) {
      let j, x, i;
      for (i = a.length; i; i -= 1) {
        j = Math.floor(Math.random() * i);
        x = a[i - 1];
        a[i - 1] = a[j];
    ...