Skip to content
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

Merged
merged 5 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
1. [16430](https://github.com/influxdata/influxdb/pull/16430): Fixed table threshold bug that was defaulting set colors to the background.
1. [16435](https://github.com/influxdata/influxdb/pull/16435): Time labels are no longer squished to the left
1. [16427](https://github.com/influxdata/influxdb/pull/16427): Fixed underlying issue with disappearing queries made in Advanced Mode
1. [16439](https://github.com/influxdata/influxdb/pull/16439): Prevent negative zero and allow zero to have decimal places

### UI Improvements

Expand Down
21 changes: 16 additions & 5 deletions ui/src/shared/utils/formatStatValue.ts
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job organizing these


// Utils
import {preventNegativeZero} from 'src/shared/utils/preventNegativeZero'

interface FormatStatValueOptions {
decimalPlaces?: DecimalPlaces
prefix?: string
Expand All @@ -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
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
43 changes: 43 additions & 0 deletions ui/src/shared/utils/preventNegativeZero.test.ts
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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test! was going to ask why you can't just use Math.abs but this one answers it nicely.

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')
})
})
9 changes: 9 additions & 0 deletions ui/src/shared/utils/preventNegativeZero.ts
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}