-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support locale formatting using d3 locale (#6119)
* Better validation * review
- Loading branch information
Showing
5 changed files
with
117 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
45 changes: 45 additions & 0 deletions
45
web-common/src/lib/number-formatting/utils/d3-format-utils.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,45 @@ | ||
import type { MetricsViewSpecMeasureV2FormatD3Locale } from "@rilldata/web-common/runtime-client"; | ||
import type { FormatLocaleDefinition } from "d3-format"; | ||
|
||
export function isValidD3Locale( | ||
config: MetricsViewSpecMeasureV2FormatD3Locale | undefined, | ||
): boolean { | ||
if (!config) return false; | ||
if (config.currency) { | ||
// currency is an array of 2 strings | ||
if (!Array.isArray(config.currency) || config.currency.length !== 2) | ||
return false; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
export function getLocaleFromConfig( | ||
config: FormatLocaleDefinition, | ||
): FormatLocaleDefinition { | ||
const base: FormatLocaleDefinition = { | ||
currency: ["$", ""], | ||
thousands: ",", | ||
grouping: [3], | ||
decimal: ".", | ||
}; | ||
|
||
return { ...base, ...config }; | ||
} | ||
|
||
export function currencyHumanizer( | ||
currency: [string, string], | ||
humanizedValue: string, | ||
): string { | ||
const [prefix, suffix] = currency; | ||
// Replace the "$" symbol with the appropriate currency prefix/suffix | ||
return `${prefix}${humanizedValue.replace(/\$/g, "")}${suffix}`; | ||
} | ||
|
||
/** | ||
* Parse the currency symbol from a d3 format string. | ||
* For d3 the currency symbol is always "$" in the format string | ||
*/ | ||
export function includesCurrencySymbol(formatString: string): boolean { | ||
return formatString.includes("$"); | ||
} |
34c0952
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://674058f045d8ec46c60153ae--rill-ui-dev.netlify.app