-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: big num new formatting rules (#6106)
* feat: big num new formatting rules * solidify ranges * Format higher magnitudes as per PRD * Treat 0 int as count of 0 * Update tests * Update big num values in tests * Leftover test values * Update comparison test * Update range
- Loading branch information
Showing
9 changed files
with
226 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
web-common/src/lib/number-formatting/strategies/per-range-bignum-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { | ||
type FormatterOptionsCommon, | ||
type FormatterRangeSpecsStrategy, | ||
NumberKind, | ||
type RangeFormatSpec, | ||
} from "../humanizer-types"; | ||
|
||
const bigNumberRangeSpec: RangeFormatSpec[] = [ | ||
{ | ||
minMag: -10, | ||
supMag: -4, | ||
maxDigitsRight: 2, | ||
baseMagnitude: 0, | ||
overrideValue: { | ||
int: "0", | ||
dot: ".", | ||
frac: "0001", | ||
prefix: "<", | ||
suffix: "", | ||
}, | ||
}, | ||
{ | ||
minMag: -4, | ||
supMag: 0, | ||
maxDigitsLeft: 0, | ||
maxDigitsRight: 4, | ||
baseMagnitude: 0, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 0, | ||
supMag: 3, | ||
maxDigitsLeft: 3, | ||
maxDigitsRight: 2, | ||
baseMagnitude: 0, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 3, | ||
supMag: 5, | ||
maxDigitsRight: 0, | ||
useTrailingDot: false, | ||
baseMagnitude: 0, | ||
maxDigitsLeft: 5, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 5, | ||
supMag: 6, | ||
maxDigitsRight: 0, | ||
useTrailingDot: false, | ||
baseMagnitude: 3, | ||
maxDigitsLeft: 3, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 6, | ||
supMag: 7, | ||
maxDigitsRight: 2, | ||
baseMagnitude: 6, | ||
maxDigitsLeft: 1, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 7, | ||
supMag: 8, | ||
maxDigitsRight: 1, | ||
baseMagnitude: 6, | ||
maxDigitsLeft: 2, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 8, | ||
supMag: 9, | ||
maxDigitsRight: 0, | ||
baseMagnitude: 6, | ||
useTrailingDot: false, | ||
maxDigitsLeft: 3, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 9, | ||
supMag: 10, | ||
maxDigitsRight: 2, | ||
baseMagnitude: 9, | ||
maxDigitsLeft: 1, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 10, | ||
supMag: 11, | ||
maxDigitsRight: 1, | ||
baseMagnitude: 9, | ||
maxDigitsLeft: 2, | ||
padWithInsignificantZeros: false, | ||
}, | ||
{ | ||
minMag: 11, | ||
supMag: 12, | ||
maxDigitsRight: 0, | ||
baseMagnitude: 9, | ||
useTrailingDot: false, | ||
maxDigitsLeft: 3, | ||
padWithInsignificantZeros: false, | ||
}, | ||
]; | ||
|
||
export const bigNumDefaultFormattingOptions: FormatterOptionsCommon & | ||
FormatterRangeSpecsStrategy = { | ||
numberKind: NumberKind.ANY, | ||
rangeSpecs: bigNumberRangeSpec, | ||
defaultMaxDigitsRight: 2, | ||
upperCaseEForExponent: true, | ||
}; | ||
|
||
export const bigNumPercentOptions: FormatterOptionsCommon & | ||
FormatterRangeSpecsStrategy = { | ||
rangeSpecs: bigNumberRangeSpec, | ||
defaultMaxDigitsRight: 2, | ||
upperCaseEForExponent: true, | ||
numberKind: NumberKind.PERCENT, | ||
}; | ||
|
||
export const bigNumCurrencyOptions = ( | ||
numberKind: NumberKind, | ||
): FormatterOptionsCommon & FormatterRangeSpecsStrategy => ({ | ||
rangeSpecs: bigNumberRangeSpec, | ||
defaultMaxDigitsRight: 2, | ||
upperCaseEForExponent: true, | ||
numberKind, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export const countDigits = (numStr: string) => | ||
numStr.replace(/[^0-9]/g, "").length; | ||
export const countDigits = (numStr: string) => { | ||
if (numStr === "0") return 0; | ||
return numStr.replace(/[^0-9]/g, "").length; | ||
}; | ||
|
||
export const countNonZeroDigits = (numStr: string) => | ||
numStr.replace(/[^1-9]/g, "").length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c24cd4e
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.
🎉 Published on https://ui.rilldata.in as production
🚀 Deployed on https://673ed9df0aced31584732609--rill-ui-dev.netlify.app