From 4879d8136f8a980cfba266bc1e567a3438bd2add Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Thu, 28 Jul 2011 17:02:18 -0400 Subject: [PATCH 1/2] [#668] renames t=>digitPairs; cleanup logic inside Popcorn.util.toSeconds() --- popcorn.js | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/popcorn.js b/popcorn.js index 1968f08d..a5941eec 100644 --- a/popcorn.js +++ b/popcorn.js @@ -1469,48 +1469,53 @@ // HH:MM:SS;FF // Hours and minutes are optional. They default to 0 toSeconds: function( timeStr, framerate ) { - //Hours and minutes are optional - //Seconds must be specified - //Seconds can be followed by milliseconds OR by the frame information + // Hours and minutes are optional + // Seconds must be specified + // Seconds can be followed by milliseconds OR by the frame information var validTimeFormat = /^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/, - errorMessage = "Invalid time format"; + errorMessage = "Invalid time format", + digitPairs, digitPairsLength, lastIndex, lastElement, + frameInfo, frameTime; if ( typeof timeStr === "number" ) { return timeStr; } else if ( typeof timeStr === "string" ) { - if ( ! validTimeFormat.test( timeStr ) ) { + if ( !validTimeFormat.test( timeStr ) ) { Popcorn.error( errorMessage ); } } else { Popcorn.error( errorMessage ); } - var t = timeStr.split( ":" ), - lastIndex = t.length - 1, - lastElement = t[ lastIndex ]; + digitPairs = timeStr.split( ":" ); + lastIndex = digitPairs.length - 1; + lastElement = digitPairs[ lastIndex ]; - //Fix last element: + // Fix last element: if ( lastElement.indexOf( ";" ) > -1 ) { - var frameInfo = lastElement.split( ";" ), - frameTime = 0; + + frameInfo = lastElement.split( ";" ); + frameTime = 0; if ( framerate && ( typeof framerate === "number" ) ) { - frameTime = parseFloat( frameInfo[ 1 ], 10 ) / framerate; + frameTime = parseFloat( frameInfo[ 1 ], 10 ) / framerate; } - t[ lastIndex ] = - parseInt( frameInfo[ 0 ], 10 ) + frameTime; + digitPairs[ lastIndex ] = parseInt( frameInfo[ 0 ], 10 ) + frameTime; } - if ( t.length === 1 ) { - return parseFloat( t[ 0 ], 10 ); - } else if ( t.length === 2 ) { - return ( parseInt( t[ 0 ], 10 ) * 60 ) + parseFloat( t[ 1 ], 10 ); - } else if ( t.length === 3 ) { - return ( parseInt( t[ 0 ], 10 ) * 3600 ) + - ( parseInt( t[ 1 ], 10 ) * 60 ) + - parseFloat( t[ 2 ], 10 ); - } + return { + + 1: parseFloat( digitPairs[ 0 ], 10 ), + + 2: ( parseInt( digitPairs[ 0 ], 10 ) * 60 ) + + parseFloat( digitPairs[ 1 ], 10 ), + + 3: ( parseInt( digitPairs[ 0 ], 10 ) * 3600 ) + + ( parseInt( digitPairs[ 1 ], 10 ) * 60 ) + + parseFloat( digitPairs[ 2 ], 10 ) + + }[ digitPairs.length || 1 ]; } }; From 46ea39bf6acbf348e3d1fb5be866badbf1d42901 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Fri, 29 Jul 2011 13:38:49 -0400 Subject: [PATCH 2/2] [#668] Cleanup unused vars and pointless conditions --- popcorn.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/popcorn.js b/popcorn.js index a5941eec..354e7cdf 100644 --- a/popcorn.js +++ b/popcorn.js @@ -1474,27 +1474,26 @@ // Seconds can be followed by milliseconds OR by the frame information var validTimeFormat = /^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/, errorMessage = "Invalid time format", - digitPairs, digitPairsLength, lastIndex, lastElement, + digitPairs, lastIndex, lastPair, firstPair, frameInfo, frameTime; if ( typeof timeStr === "number" ) { return timeStr; - } else if ( typeof timeStr === "string" ) { - if ( !validTimeFormat.test( timeStr ) ) { - Popcorn.error( errorMessage ); - } - } else { + } + + if ( typeof timeStr === "string" && + !validTimeFormat.test( timeStr ) ) { Popcorn.error( errorMessage ); } digitPairs = timeStr.split( ":" ); lastIndex = digitPairs.length - 1; - lastElement = digitPairs[ lastIndex ]; + lastPair = digitPairs[ lastIndex ]; // Fix last element: - if ( lastElement.indexOf( ";" ) > -1 ) { + if ( lastPair.indexOf( ";" ) > -1 ) { - frameInfo = lastElement.split( ";" ); + frameInfo = lastPair.split( ";" ); frameTime = 0; if ( framerate && ( typeof framerate === "number" ) ) { @@ -1504,14 +1503,16 @@ digitPairs[ lastIndex ] = parseInt( frameInfo[ 0 ], 10 ) + frameTime; } + firstPair = digitPairs[ 0 ]; + return { - 1: parseFloat( digitPairs[ 0 ], 10 ), + 1: parseFloat( firstPair, 10 ), - 2: ( parseInt( digitPairs[ 0 ], 10 ) * 60 ) + + 2: ( parseInt( firstPair, 10 ) * 60 ) + parseFloat( digitPairs[ 1 ], 10 ), - 3: ( parseInt( digitPairs[ 0 ], 10 ) * 3600 ) + + 3: ( parseInt( firstPair, 10 ) * 3600 ) + ( parseInt( digitPairs[ 1 ], 10 ) * 60 ) + parseFloat( digitPairs[ 2 ], 10 )