Bug 1029540 - ViewHelpers.L10N.numberWithDecimals doesn't properly handle NaN and numbers that can't be localized, r=bgrins
authorVictor Porof <vporof@mozilla.com>
Tue, 24 Jun 2014 21:45:52 -0400
changeset 190691 bb723b1668ecd2f86666e4ccdd9ca58f3010a518
parent 190690 c7e9cd887d3d9e3a6913adf2e51495834163b7cf
child 190692 9ab9f3f22d1c61f2ab0db0d1f22a296c74613a16
push id8358
push usercbook@mozilla.com
push dateWed, 25 Jun 2014 14:22:36 +0000
treeherderb2g-inbound@56ebaa70ca72 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbgrins
bugs1029540
milestone33.0a1
Bug 1029540 - ViewHelpers.L10N.numberWithDecimals doesn't properly handle NaN and numbers that can't be localized, r=bgrins
browser/devtools/shared/test/browser.ini
browser/devtools/shared/test/browser_num-l10n.js
browser/devtools/shared/widgets/ViewHelpers.jsm
--- a/browser/devtools/shared/test/browser.ini
+++ b/browser/devtools/shared/test/browser.ini
@@ -24,16 +24,17 @@ support-files =
 [browser_graphs-10a.js]
 [browser_graphs-10b.js]
 [browser_graphs-11.js]
 [browser_graphs-12.js]
 [browser_graphs-13.js]
 [browser_graphs-14.js]
 [browser_layoutHelpers.js]
 [browser_layoutHelpers-getBoxQuads.js]
+[browser_num-l10n.js]
 [browser_observableobject.js]
 [browser_outputparser.js]
 [browser_prefs.js]
 [browser_require_basic.js]
 [browser_spectrum.js]
 [browser_tableWidget_basic.js]
 [browser_tableWidget_keyboard_interaction.js]
 [browser_tableWidget_mouse_interaction.js]
new file mode 100644
--- /dev/null
+++ b/browser/devtools/shared/test/browser_num-l10n.js
@@ -0,0 +1,25 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Tests that ViewHelpers.Prefs work properly.
+
+let {ViewHelpers} = Cu.import("resource:///modules/devtools/ViewHelpers.jsm", {});
+
+function test() {
+  let l10n = new ViewHelpers.L10N();
+
+  is(l10n.numberWithDecimals(1234.56789, 2), "1,234.56",
+    "The first number was properly localized.");
+  is(l10n.numberWithDecimals(0.0001, 2), "0",
+    "The second number was properly localized.");
+  is(l10n.numberWithDecimals(1.0001, 2), "1",
+    "The third number was properly localized.");
+  is(l10n.numberWithDecimals(NaN, 2), "0",
+    "NaN was properly localized.");
+  is(l10n.numberWithDecimals(null, 2), "0",
+    "`null` was properly localized.");
+  is(l10n.numberWithDecimals(undefined, 2), "0",
+    "`undefined` was properly localized.");
+
+  finish();
+}
--- a/browser/devtools/shared/widgets/ViewHelpers.jsm
+++ b/browser/devtools/shared/widgets/ViewHelpers.jsm
@@ -360,21 +360,31 @@ ViewHelpers.L10N.prototype = {
    * @return string
    *         The localized number as a string.
    */
   numberWithDecimals: function(aNumber, aDecimals = 0) {
     // If this is an integer, don't do anything special.
     if (aNumber == (aNumber | 0)) {
       return aNumber;
     }
+    if (isNaN(aNumber) || aNumber == null) {
+      return "0";
+    }
     // Remove {n} trailing decimals. Can't use toFixed(n) because
     // toLocaleString converts the number to a string. Also can't use
     // toLocaleString(, { maximumFractionDigits: n }) because it's not
     // implemented on OS X (bug 368838). Gross.
     let localized = aNumber.toLocaleString(); // localize
+
+    // If no grouping or decimal separators are available, bail out, because
+    // padding with zeros at the end of the string won't make sense anymore.
+    if (!localized.match(/[^\d]/)) {
+      return localized;
+    }
+
     let padded = localized + new Array(aDecimals).join("0"); // pad with zeros
     let match = padded.match("([^]*?\\d{" + aDecimals + "})\\d*$");
     return match.pop();
   }
 };
 
 /**
  * Shortcuts for lazily accessing and setting various preferences.