Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat($filter): Display Infinity symbol when number is Infinity
Browse files Browse the repository at this point in the history
Infinity is a value and should not be treated as an empty string

Closes #10421
  • Loading branch information
quentin authored and lgalfaso committed Jan 8, 2015
1 parent e079111 commit 51d6774
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 51d6774

Please sign in to comment.