Permalink
Browse files

feat($filter): Display Infinity symbol when number is Infinity

Infinity is a value and should not be treated as an empty string

Closes #10421
  • Loading branch information...
1 parent e079111 commit 51d6774286202b55ade402ca097e417e70fd546b quentin committed with lgalfaso Dec 11, 2014
Showing with 15 additions and 7 deletions.
  1. +12 −4 src/ng/filter/filters.js
  2. +3 −3 test/ng/filter/filtersSpec.js
@@ -82,6 +82,8 @@ function currencyFilter($locale) {
*
* If the input is not a number an empty string is returned.
*
+ * If the input is an infinite (Infinity/-Infinity) the Infinity symbol '∞' is returned.
+ *
* @param {number|string} number Number to format.
* @param {(number|string)=} fractionSize Number of decimal places to round the number to.
* If this is not provided then the fraction size is computed from the current locale's number
@@ -138,16 +140,22 @@ function numberFilter($locale) {
var DECIMAL_SEP = '.';
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
- if (!isFinite(number) || isObject(number)) return '';
+ if (isObject(number)) return '';
var isNegative = number < 0;
number = Math.abs(number);
+
+ var isInfinity = number === Infinity;
+ if (!isInfinity && !isFinite(number)) return '';
+
var numStr = number + '',
formatedText = '',
+ hasExponent = false,
parts = [];
- var hasExponent = false;
- if (numStr.indexOf('e') !== -1) {
+ if (isInfinity) formatedText = '\u221e';
+
+ if (!isInfinity && numStr.indexOf('e') !== -1) {
var match = numStr.match(/([\d\.]+)e(-?)(\d+)/);
if (match && match[2] == '-' && match[3] > fractionSize + 1) {
number = 0;
@@ -157,7 +165,7 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
}
}
- if (!hasExponent) {
+ if (!isInfinity && !hasExponent) {
var fractionLen = (numStr.split(DECIMAL_SEP)[1] || '').length;
// determine fractionSize if it is not specified
@@ -150,10 +150,10 @@ describe('filters', function() {
expect(number(Number.NaN)).toEqual('');
expect(number({})).toEqual('');
expect(number([])).toEqual('');
- expect(number(+Infinity)).toEqual('');
- expect(number(-Infinity)).toEqual('');
+ expect(number(+Infinity)).toEqual('');
+ expect(number(-Infinity)).toEqual('-∞');
expect(number("1234.5678")).toEqual('1,234.568');
- expect(number(1 / 0)).toEqual("");
+ expect(number(1 / 0)).toEqual('');
expect(number(1, 2)).toEqual("1.00");
expect(number(.1, 2)).toEqual("0.10");
expect(number(.01, 2)).toEqual("0.01");

0 comments on commit 51d6774

Please sign in to comment.