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

Commit 51d6774

Browse files
quentinlgalfaso
quentin
authored andcommitted
feat($filter): Display Infinity symbol when number is Infinity
Infinity is a value and should not be treated as an empty string Closes #10421
1 parent e079111 commit 51d6774

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/ng/filter/filters.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ function currencyFilter($locale) {
8282
*
8383
* If the input is not a number an empty string is returned.
8484
*
85+
* If the input is an infinite (Infinity/-Infinity) the Infinity symbol '∞' is returned.
86+
*
8587
* @param {number|string} number Number to format.
8688
* @param {(number|string)=} fractionSize Number of decimal places to round the number to.
8789
* If this is not provided then the fraction size is computed from the current locale's number
@@ -138,16 +140,22 @@ function numberFilter($locale) {
138140

139141
var DECIMAL_SEP = '.';
140142
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
141-
if (!isFinite(number) || isObject(number)) return '';
143+
if (isObject(number)) return '';
142144

143145
var isNegative = number < 0;
144146
number = Math.abs(number);
147+
148+
var isInfinity = number === Infinity;
149+
if (!isInfinity && !isFinite(number)) return '';
150+
145151
var numStr = number + '',
146152
formatedText = '',
153+
hasExponent = false,
147154
parts = [];
148155

149-
var hasExponent = false;
150-
if (numStr.indexOf('e') !== -1) {
156+
if (isInfinity) formatedText = '\u221e';
157+
158+
if (!isInfinity && numStr.indexOf('e') !== -1) {
151159
var match = numStr.match(/([\d\.]+)e(-?)(\d+)/);
152160
if (match && match[2] == '-' && match[3] > fractionSize + 1) {
153161
number = 0;
@@ -157,7 +165,7 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
157165
}
158166
}
159167

160-
if (!hasExponent) {
168+
if (!isInfinity && !hasExponent) {
161169
var fractionLen = (numStr.split(DECIMAL_SEP)[1] || '').length;
162170

163171
// determine fractionSize if it is not specified

test/ng/filter/filtersSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ describe('filters', function() {
150150
expect(number(Number.NaN)).toEqual('');
151151
expect(number({})).toEqual('');
152152
expect(number([])).toEqual('');
153-
expect(number(+Infinity)).toEqual('');
154-
expect(number(-Infinity)).toEqual('');
153+
expect(number(+Infinity)).toEqual('');
154+
expect(number(-Infinity)).toEqual('-∞');
155155
expect(number("1234.5678")).toEqual('1,234.568');
156-
expect(number(1 / 0)).toEqual("");
156+
expect(number(1 / 0)).toEqual('∞');
157157
expect(number(1, 2)).toEqual("1.00");
158158
expect(number(.1, 2)).toEqual("0.10");
159159
expect(number(.01, 2)).toEqual("0.01");

0 commit comments

Comments
 (0)