node-deadline
Version:
Module to interface with Deadline Compute Management System by Thinkbox Software
104 lines (82 loc) • 2.38 kB
JavaScript
var FrameRange = module.exports = function( string ) {
this.raw = "";
this.count = 0;
this.frames = [];
if ( "undefined" != typeof string ) {
this.parse( string );
}
};
FrameRange.prototype.parse = function( fString ) {
var i, f, range, ranges,
frames, bkwd;
if ( "string" != typeof fString ) {
return;
}
this.frames = [];
this.count = 0;
this.ranges = [];
ranges = fString.split( / |,/g );
for ( i in ranges ) {
//skip empty ranges
if ( ranges[ i ] === "" ) {
continue;
}
//if one number, simply parse it
if ( !isNaN( ranges[ i ] ) ) {
f = parseInt( ranges[ i ] );
this.frames.push( f );
this.count++;
this.ranges.push( f );
continue;
}
//look for a range specifyer
range = ranges[ i ].match( /(.*)(-|:)/ );
this.ranges.push( range );
if ( null === range ) {
console.log( "unknown range:", ranges[ i ] );
continue;
}
//extract what's left as the second variable
range[ 2 ] = ranges[ i ].substr( range[ 0 ].length );
//make sure the starting frame is valid
if ( isNaN( range[ 1 ] ) ) {
console.log( "unknown range start:", range[ 1 ] );
continue;
}
range[ 1 ] = parseInt( range[ 1 ] );
//check for a step
range.step = 1;
if ( isNaN( range[ 2 ] ) ) {
//parse out step specifier
range.step = range[ 2 ].match( /(.*)(x|:|step|by|every)/ );
if ( null === range.step || isNaN( range.step[ 1 ] ) ) {
console.log( "unknown range end:", range[ 2 ] );
continue;
}
//extract what's left as the second variable
range.step[ 2 ] = range[ 2 ].substr( range.step[ 0 ].length );
range[ 2 ] = parseInt( range.step[ 1 ] );
if ( isNaN( range.step[ 2 ] ) ) {
console.log( "unknown range step:", range.step[ 2 ] );
continue;
}
range.step = parseInt( range.step[ 2 ] );
}
else {
range[ 2 ] = parseInt( range[ 2 ] );
}
frames = [];
bkwd = range[ 1 ] > range[ 2 ];
for ( i = range[ 1 ]; bkwd ? i >= range[ 2 ] : i <= range[ 2 ]; bkwd ? i -= range.step : i += range.step ) {
frames.push( i );
this.count++;
}
if ( bkwd ) {
frames.reverse();
}
this.frames = this.frames.concat( frames );
}
};
FrameRange.toArray = function() {
return this.frames;
};