-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ui): Prevent negative zero and allow zero with decimal places #16439
Changes from all commits
c357a36
9d3fd1b
7c68493
3481ed8
9f1d10d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import {MAX_DECIMAL_PLACES} from 'src/dashboards/constants' | ||
// Libraries | ||
import {isNumber, isString} from 'lodash' | ||
|
||
// Types | ||
import {DecimalPlaces} from 'src/types/dashboards' | ||
|
||
// Constants | ||
import {MAX_DECIMAL_PLACES} from 'src/dashboards/constants' | ||
|
||
// Utils | ||
import {preventNegativeZero} from 'src/shared/utils/preventNegativeZero' | ||
|
||
interface FormatStatValueOptions { | ||
decimalPlaces?: DecimalPlaces | ||
prefix?: string | ||
|
@@ -13,7 +20,7 @@ export const formatStatValue = ( | |
value: number | string = 0, | ||
{decimalPlaces, prefix, suffix}: FormatStatValueOptions = {} | ||
): string => { | ||
let localeFormattedValue = '' | ||
let localeFormattedValue | ||
|
||
if (isNumber(value)) { | ||
let digits: number | ||
|
@@ -26,15 +33,19 @@ export const formatStatValue = ( | |
|
||
const roundedValue = value.toFixed(digits) | ||
|
||
localeFormattedValue = Number(roundedValue).toLocaleString(undefined, { | ||
maximumFractionDigits: MAX_DECIMAL_PLACES, | ||
}) | ||
localeFormattedValue = | ||
Number(roundedValue) === 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you think of turning this code into a formatting function and including the negative zero check as part of it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This piece of code is actually inside a formatting function already. The ternary is to allow positive zero to have decimal places when user-specified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well then |
||
? roundedValue | ||
: Number(roundedValue).toLocaleString(undefined, { | ||
maximumFractionDigits: MAX_DECIMAL_PLACES, | ||
}) | ||
} else if (isString(value)) { | ||
localeFormattedValue = value | ||
} else { | ||
return 'Data cannot be displayed' | ||
} | ||
|
||
localeFormattedValue = preventNegativeZero(localeFormattedValue) | ||
const formattedValue = `${prefix || ''}${localeFormattedValue}${suffix || ''}` | ||
|
||
return formattedValue | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {preventNegativeZero} from 'src/shared/utils/preventNegativeZero' | ||
|
||
describe('preventNegativeZero', () => { | ||
it('should not alter non-zero numbers', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice test! was going to ask why you can't just use |
||
expect(preventNegativeZero(1)).toEqual(1) | ||
expect(preventNegativeZero(0.000000000001)).toEqual(0.000000000001) | ||
expect(preventNegativeZero(-456.78)).toEqual(-456.78) | ||
|
||
let nonZeroString = '0.000000000000000000000000001' | ||
expect(preventNegativeZero(nonZeroString)).toEqual(nonZeroString) | ||
|
||
nonZeroString = '1234567890' | ||
expect(preventNegativeZero(nonZeroString)).toEqual(nonZeroString) | ||
|
||
nonZeroString = '-123456789.0001' | ||
expect(preventNegativeZero(nonZeroString)).toEqual(nonZeroString) | ||
}) | ||
it('should handle negative zero as a number', () => { | ||
expect(preventNegativeZero(-0)).toEqual(0) | ||
expect(preventNegativeZero(-0.0)).toEqual(0) | ||
|
||
// prettier-ignore | ||
expect(preventNegativeZero(-0.00)).toEqual(0) | ||
// prettier-ignore | ||
expect(preventNegativeZero(-0.000)).toEqual(0) | ||
// prettier-ignore | ||
expect(preventNegativeZero(-0.0000)).toEqual(0) | ||
// prettier-ignore | ||
expect(preventNegativeZero(-0.00)).toEqual(0.00) | ||
// prettier-ignore | ||
expect(preventNegativeZero(-0.000)).toEqual(0.000) | ||
// prettier-ignore | ||
expect(preventNegativeZero(-0.0000)).toEqual(0.0000) | ||
}) | ||
it('should handle negative zero as a string', () => { | ||
expect(preventNegativeZero('-0')).toEqual('0') | ||
expect(preventNegativeZero('-0.0')).toEqual('0.0') | ||
expect(preventNegativeZero('-0.00000000')).toEqual('0.00000000') | ||
|
||
expect(preventNegativeZero('-0.0')).not.toEqual('0') | ||
expect(preventNegativeZero('-0.00000000')).not.toEqual('0') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const preventNegativeZero = ( | ||
value: number | string | ||
): number | string => { | ||
// eslint-disable-next-line no-compare-neg-zero | ||
if (Number(value) === -0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Literally didn't know this was possible |
||
return typeof value === 'number' ? 0 : value.replace(/-/g, '') | ||
} | ||
return value | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job organizing these