Skip to content

Fix and test the TimeValue.pad() function #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/values/TimeValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,12 @@ SELF.getPrecisionById = function( id ) {
* @return {string}
*/
function pad( number, digits ) {
return ( ( 1e12 + Math.abs( number ) ).toString() ).slice( -digits );
number = String( Math.abs( number ) );
if ( number.length >= digits ) {
return number;
}

return new Array( digits - number.length + 1 ).join( '0' ) + number;
}

dv.registerDataValue( SELF );
Expand Down
32 changes: 32 additions & 0 deletions tests/src/values/TimeValue.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ define( [
!timeValue1.equals( timeValue2 ),
'instances encapsulating different values are not equal'
);
},

/**
* Tests the effect of the private pad() function, relevant in getSortKey() and toJSON().
*
* @since 0.7
*
* @param {QUnit} assert
*/
testPad: function( assert ) {
var testCases = {
'-123456789012-00-00T00:00:00Z': '-123456789012-00-00T00:00:00Z',
'-12345678901-00-00T00:00:00Z': '-12345678901-00-00T00:00:00Z',
'-1-1-1T01:01:01Z': '-00000000001-01-01T01:01:01Z',
'1-1-1T01:01:01Z': '+00000000001-01-01T01:01:01Z',
'12-00-00T00:00:00Z': '+00000000012-00-00T00:00:00Z',
'1234567890-00-00T00:00:00Z': '+01234567890-00-00T00:00:00Z',
'12345678901-00-00T00:00:00Z': '+12345678901-00-00T00:00:00Z',
'123456789012-00-00T00:00:00Z': '+123456789012-00-00T00:00:00Z',
'1234567890123456-00-00T00:00:00Z': '+1234567890123456-00-00T00:00:00Z'
};

for( var iso8601 in testCases ) {
var expected = testCases[iso8601],
actual = new dv.TimeValue( iso8601 ).getSortKey();

assert.ok(
expected === actual,
'Expected getSortKey() to return "' + expected + '", got "' + actual + '"'
);

}
}

} );
Expand Down