added a function to pad a number with zeros. waiting for the helpers to land into popcorn-port
authorhamidouk
Wed, 16 Nov 2011 16:06:18 +0100
branchpopcorn-port
changeset 256 8e1181b855bd
parent 255 af3adcf7cb20
child 257 d62fe14973a9
added a function to pad a number with zeros. waiting for the helpers to land into the main mustache.js repo to use it in the number display.
src/js/utils.js
unittests/tests/utils.js
--- a/src/js/utils.js	Wed Nov 16 12:30:04 2011 +0100
+++ b/src/js/utils.js	Wed Nov 16 16:06:18 2011 +0100
@@ -31,6 +31,13 @@
 	return Math.floor((time/timetotal) * 100);
 };
 
+IriSP.padWithZeros = function(num) {
+  if (Math.abs(num) < 10) {
+    return "0" + num.toString();
+  } else {
+    return num.toString();
+  }
+};
 /* convert a number of seconds to a tuple of the form 
    [hours, minutes, seconds]
 */
--- a/unittests/tests/utils.js	Wed Nov 16 12:30:04 2011 +0100
+++ b/unittests/tests/utils.js	Wed Nov 16 16:06:18 2011 +0100
@@ -24,6 +24,10 @@
     equal(IriSP.timeToPourcent(2, 3), 66, "the function is immune to negative numbers");            
   });
   
+  test("test padding function", function() {
+    equal(IriSP.padWithZeros(3), "03", "function works correctly");
+  });
+  
   test("test function to convert from seconds to a time", function() {
     var h = 13, m = 7, s = 41;
     var t = 13 * 3600 + 7* 60 + 41;