Skip to content

Commit

Permalink
chore: extract isnegativezero
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirivanoviliev authored and danielkaradachki committed Dec 6, 2019
1 parent bf7a72b commit 7da0ea4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/common/is-negative-zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isNegativeZero(value) {
return (1 / value === -Infinity);
}
3 changes: 2 additions & 1 deletion src/numbers/custom-number-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CURRENCY, PERCENT, LIST_SEPARATOR, GROUP_SEPARATOR, CURRENCY_PLACEHOLDER, PERCENT_PLACEHOLDER, POINT, EMPTY } from '../common/constants';
import isNegativeZero from '../common/is-negative-zero';
import formatCurrencySymbol from './format-currency-symbol';
import groupInteger from './group-integer';
import round from '../common/round';
Expand Down Expand Up @@ -273,7 +274,7 @@ export default function customNumberFormat(number, format, info) {
const formatOptions = {
negative: number < 0,
number: Math.abs(number),
negativeZero: (1 / number === -Infinity),
negativeZero: isNegativeZero(number),
format: format
};

Expand Down
3 changes: 2 additions & 1 deletion src/numbers/standard-number-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PERCENT, SCIENTIFIC, NUMBER_PLACEHOLDER, CURRENCY_PLACEHOLDER, PERCENT_PLACEHOLDER, EMPTY, POINT } from '../common/constants';
import isNegativeZero from '../common/is-negative-zero';
import formatCurrencySymbol from './format-currency-symbol';
import groupInteger from './group-integer';
import isCurrencyStyle from './is-currency-style';
Expand Down Expand Up @@ -95,7 +96,7 @@ export default function standardNumberFormat(number, options, info) {
value = round(value, maximumFractionDigits);

const negative = value < 0;
const negativeZero = (1 / number === -Infinity);
const negativeZero = isNegativeZero(number);

const parts = value.split(POINT);

Expand Down
8 changes: 8 additions & 0 deletions test/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ describe('standard accounting formatting', () => {
delete cldr.custom;
});

it('should support minus zero', () => {
expect(formatNumber(-0, 'a2')).toEqual("($0.00)");
});

it('should apply format', () => {
expect(formatNumber(10, 'a', 'custom')).toEqual("10.00$");
});
Expand Down Expand Up @@ -405,6 +409,10 @@ describe('custom formatting', () => {
expect(formatNumber(10.9, '#')).toEqual("11");
});

it('should support minus zero with custom format', () => {
expect(formatNumber(-0, '0.00;(0.00)')).toEqual("(0.00)");
});

it('replaces whole part of the number', () => {
expect(formatNumber(-0, '#')).toEqual("-0");
});
Expand Down

0 comments on commit 7da0ea4

Please sign in to comment.