Skip to content

Commit 02b1951

Browse files
committed
Merge pull request #67 from wmde/pad
Fix and test the TimeValue.pad() function
2 parents e8093d0 + 9bfaff2 commit 02b1951

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/values/TimeValue.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,12 @@ SELF.getPrecisionById = function( id ) {
333333
* @return {string}
334334
*/
335335
function pad( number, digits ) {
336-
return ( ( 1e12 + Math.abs( number ) ).toString() ).slice( -digits );
336+
number = String( Math.abs( number ) );
337+
if ( number.length >= digits ) {
338+
return number;
339+
}
340+
341+
return new Array( digits - number.length + 1 ).join( '0' ) + number;
337342
}
338343

339344
dv.registerDataValue( SELF );

tests/src/values/TimeValue.tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ define( [
6060
!timeValue1.equals( timeValue2 ),
6161
'instances encapsulating different values are not equal'
6262
);
63+
},
64+
65+
/**
66+
* Tests the effect of the private pad() function, relevant in getSortKey() and toJSON().
67+
*
68+
* @since 0.7
69+
*
70+
* @param {QUnit} assert
71+
*/
72+
testPad: function( assert ) {
73+
var testCases = {
74+
'-123456789012-00-00T00:00:00Z': '-123456789012-00-00T00:00:00Z',
75+
'-12345678901-00-00T00:00:00Z': '-12345678901-00-00T00:00:00Z',
76+
'-1-1-1T01:01:01Z': '-00000000001-01-01T01:01:01Z',
77+
'1-1-1T01:01:01Z': '+00000000001-01-01T01:01:01Z',
78+
'12-00-00T00:00:00Z': '+00000000012-00-00T00:00:00Z',
79+
'1234567890-00-00T00:00:00Z': '+01234567890-00-00T00:00:00Z',
80+
'12345678901-00-00T00:00:00Z': '+12345678901-00-00T00:00:00Z',
81+
'123456789012-00-00T00:00:00Z': '+123456789012-00-00T00:00:00Z',
82+
'1234567890123456-00-00T00:00:00Z': '+1234567890123456-00-00T00:00:00Z'
83+
};
84+
85+
for( var iso8601 in testCases ) {
86+
var expected = testCases[iso8601],
87+
actual = new dv.TimeValue( iso8601 ).getSortKey();
88+
89+
assert.ok(
90+
expected === actual,
91+
'Expected getSortKey() to return "' + expected + '", got "' + actual + '"'
92+
);
93+
94+
}
6395
}
6496

6597
} );

0 commit comments

Comments
 (0)