Permalink
Browse files

new features, multiplex bug limiting by slice

  • Loading branch information...
Kaique da Silva
Kaique da Silva committed Aug 6, 2012
1 parent db8c182 commit 72c9f6725f4bd9490434b4615328edc7c1c01fe0
Showing with 145 additions and 0 deletions.
  1. +91 −0 lib/vodevil.js
  2. +54 −0 test/vodevil.test.js
View
@@ -104,6 +104,52 @@ var Vodevil = (function () {
return output;
};
+ var __sail__ = function ( target, pointer ) {
+ var output = [];
+
+ if ( target && typeof pointer === 'string' ) {
+ if ( pointer.indexOf(':') > -1 && pointer.indexOf('::') === -1 ) {
+ pointer = pointer.split(':');
+
+ var p = 0,
+ end = target.length;
+
+ do {
+ if ( p >= parseInt( pointer[0], 10 ) && p <= parseInt( pointer[1], 10 ) ) {
+ output.push( target[p] );
+ } else if ( p >= parseInt( pointer[0], 10 ) && !pointer[1] ) {
+ output.push( target[p] );
+ } else if ( !pointer[0] && p <= parseInt( pointer[1], 10 ) ) {
+ output.push( target[p] );
+ }
+
+ p++;
+ } while ( p < end );
+ } else if ( pointer.indexOf('::') > -1 ) {
+ target = target.reverse();
+
+ pointer = pointer.split('::');
+
+ var p = 0,
+ end = target.length;
+
+ do {
+ if ( p >= parseInt( pointer[0], 10 ) && p <= parseInt( pointer[1], 10 ) ) {
+ output.push( target[p] );
+ } else if ( p >= parseInt( pointer[0], 10 ) && !pointer[1] ) {
+ output.push( target[p] );
+ } else if ( !pointer[0] && p <= parseInt( pointer[1], 10 ) ) {
+ output.push( target[p] );
+ }
+
+ p++;
+ } while ( p < end );
+ }
+ }
+
+ return output;
+ };
+
// Public methods and properties.
core.prototype = {
isArray: function ( target ) {
@@ -242,6 +288,51 @@ var Vodevil = (function () {
output = __intersect__( target, fn );
}
+ return output;
+ },
+
+ sail: function ( target, pointer ) {
+ var output = [];
+
+ output = __sail__( target, pointer );
+
+ return output;
+ },
+
+ multiplex: function ( target, slice ) {
+ var output = [],
+ self = this,
+ slice = slice || 3;
+
+ if ( self.isArray( target ) && target ) {
+ var piece = [],
+ s = 0,
+ e = target.length;
+
+ do {
+ piece.push( target[s] );
+
+ if ( piece.length === slice ) {
+ output.push( piece );
+
+ piece = [];
+ }
+
+ s++;
+ } while ( s < e );
+ }
+
+ return output;
+ },
+
+ flush: function ( target ) {
+ var output = [],
+ self = this;
+
+ if ( self.isArray( target ) && target ) {
+ output = target.toString().split(',');
+ }
+
return output;
}
};
View
@@ -202,4 +202,58 @@ describe('Vodevil Section of Tests: ', function () {
Vodevil.intersect( [1, 2, 3], function ( x, i, o ) {}).should.have.length( 0 );
});
});
+
+ describe('Array sail', function () {
+ it('from a point to end', function () {
+ var output = [3, 4, 5].toString();
+
+ Vodevil.sail( [1, 2, 3, 4, 5], '2:' ).toString().should.equal( output );
+ });
+
+ it('from a point to start', function () {
+ var output = [1, 2].toString();
+
+ Vodevil.sail( [1, 2, 3, 4, 5], ':1' ).toString().should.equal( output );
+ });
+
+ it('from two spec points', function () {
+ var output = [3, 4].toString();
+
+ Vodevil.sail( [1, 2, 3, 4, 5, 6], '2:3' ).toString().should.equal( output );
+ });
+
+ it('reverse travel', function () {
+ var output = [4, 3, 2, 1].toString();
+
+ Vodevil.sail( [1, 2, 3, 4], '0::' ).toString().should.equal( output );
+ });
+
+ it('reverse travel by two points', function () {
+ var output = [5, 4].toString();
+
+ Vodevil.sail( [1, 2, 3, 4, 5], '0::1' ).toString().should.equal( output );
+ });
+
+ it('start to end', function () {
+ var output = [3, 2, 1].toString();
+
+ Vodevil.sail( [1, 2, 3, 4, 5], '2::' ).toString().should.equal( output );
+ });
+ });
+
+ describe('Array multiplex', function () {
+ it('return default behavior', function () {
+ var output = [[1, 2, 3], [4, 5, 6], [7, 8, 9]].toString();
+
+ Vodevil.multiplex( Vodevil.range('1..9') ).toString().should.equal( output );
+ });
+ });
+
+ describe('Array flush', function () {
+ it('all deep flush', function () {
+ var output = [1, 2, 3, 4, 5, 6].toString();
+
+ Vodevil.flush( [1, [2, [3, [4, [5, [6]]]]]] ).toString().should.equal( output );
+ });
+ });
});

0 comments on commit 72c9f67

Please sign in to comment.