Skip to content

Commit 691c9c0

Browse files
committed
Fix and test the TimeValue.pad() function
1 parent c4d163a commit 691c9c0

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/values/TimeValue.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,12 @@ SELF.getPrecisionById = function( id ) {
336336
* @return {string}
337337
*/
338338
function pad( number, digits ) {
339-
return ( ( 1e12 + Math.abs( number ) ).toString() ).slice( -digits );
339+
number = String( Math.abs( number ) );
340+
if ( number.length >= digits ) {
341+
return number;
342+
}
343+
344+
return new Array( digits - number.length + 1 ).join( '0' ) + number;
340345
}
341346

342347
dv.registerDataValue( SELF );

tests/src/values/TimeValue.tests.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ define( [
4343
precision: 9
4444
}]
4545
];
46+
},
47+
48+
/**
49+
* Tests the effect of the private pad() function, relevant in getSortKey() and toJSON().
50+
*
51+
* @since 0.7
52+
*
53+
* @param {QUnit} assert
54+
*/
55+
testPad: function( assert ) {
56+
var testCases = {
57+
'1-1-1T01:01:01Z': '+00000000001-01-01T01:01:01Z',
58+
'12-00-00T00:00:00Z': '+00000000012-00-00T00:00:00Z',
59+
'1234567890-00-00T00:00:00Z': '+01234567890-00-00T00:00:00Z',
60+
'12345678901-00-00T00:00:00Z': '+12345678901-00-00T00:00:00Z',
61+
'123456789012-00-00T00:00:00Z': '+123456789012-00-00T00:00:00Z',
62+
'1234567890123456-00-00T00:00:00Z': '+1234567890123456-00-00T00:00:00Z'
63+
};
64+
65+
for( var iso8601 in testCases ) {
66+
var expected = testCases[iso8601],
67+
actual = new dv.TimeValue( iso8601 ).getSortKey();
68+
69+
assert.ok(
70+
expected === actual,
71+
'Expected getSortKey() to return "' + expected + '", got "' + actual + '"'
72+
);
73+
74+
}
4675
}
4776

4877
} );

0 commit comments

Comments
 (0)