Skip to content

Commit

Permalink
feat(Formatters): shorten numbers greater than septillion (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegesse-w authored Jul 18, 2022
1 parent a19da41 commit e72ed22
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion giraffe/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@influxdata/giraffe",
"version": "2.31.1",
"version": "2.32.0",
"main": "dist/index.js",
"module": "dist/index.js",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions giraffe/src/utils/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ describe('siPrefixFormatter', () => {
expect(f(37)).toEqual('37.00')
expect(f(37.1234)).toEqual('37.12')
})

it('can shorten a positive number larger than septillion', () => {
const f = siPrefixFormatter()
expect(f(1.7e308)).toEqual('1.7e+284Y')
})

it('can shorten a negative number smaller than septillion', () => {
const f = siPrefixFormatter()
expect(f(-1.7e308)).toEqual('-1.7e+284Y')
})
})

describe('binaryPrefixFormatter', () => {
Expand Down
17 changes: 16 additions & 1 deletion giraffe/src/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,22 @@ export const siPrefixFormatter = ({
if (format !== true) {
formatter = (x: number): string => `${prefix}${x}${suffix}`
} else {
formatter = (x: number): string => `${prefix}${formatSIPrefix(x)}${suffix}`
formatter = (x: number): string => {
// code below shortens extremely large or small numbers (greater than septillion+) by
// first converting number to SI format, then removing the SI unit to convert
// number to scientific notation, and finally
// adding yotta (Y) back
if (x >= 1e30 || x <= -1e30) {
const siFormattedValue = String(formatSIPrefix(Math.abs(x))).slice(
0,
-1
)
return `${prefix}${x < 0 ? '-' : ''}${d3Format(`.${significantDigits}`)(
Number(siFormattedValue)
)}Y${suffix}`
}
return `${prefix}${formatSIPrefix(x)}${suffix}`
}
}

formatter._GIRAFFE_FORMATTER_TYPE = FormatterType.SIPrefix as FormatterType.SIPrefix
Expand Down
2 changes: 1 addition & 1 deletion stories/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@influxdata/giraffe-stories",
"version": "2.31.1",
"version": "2.32.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit e72ed22

Please sign in to comment.