Files
medusa-store/packages/admin/dashboard/src/lib/money-amount-helpers.ts
T
Kasper Fabricius Kristensen 0fe1201435 feat(admin-sdk,admin-bundler,admin-shared,medusa): Restructure admin packages (#8988)
**What**
- Renames /admin-next -> /admin
- Renames @medusajs/admin-sdk -> @medusajs/admin-bundler
- Creates a new package called @medusajs/admin-sdk that will hold all tooling relevant to creating admin extensions. This is currently `defineRouteConfig` and `defineWidgetConfig`, but will eventually also export methods for adding custom fields, register translation, etc. 
  - cc: @shahednasser we should update the examples in the docs so these functions are imported from `@medusajs/admin-sdk`. People will also need to install the package in their project, as it's no longer a transient dependency.
  - cc: @olivermrbl we might want to publish a changelog when this is merged, as it is a breaking change, and will require people to import the `defineXConfig` from the new package instead of `@medusajs/admin-shared`.
- Updates CODEOWNERS so /admin packages does not require a review from the UI team.
2024-09-04 19:00:25 +00:00

54 lines
1.7 KiB
TypeScript

import { currencies } from "./data/currencies"
export const getDecimalDigits = (currency: string) => {
return currencies[currency.toUpperCase()]?.decimal_digits ?? 0
}
/**
* Returns a formatted amount based on the currency code using the browser's locale
* @param amount - The amount to format
* @param currencyCode - The currency code to format the amount in
* @returns - The formatted amount
*
* @example
* getFormattedAmount(10, "usd") // '$10.00' if the browser's locale is en-US
* getFormattedAmount(10, "usd") // '10,00 $' if the browser's locale is fr-FR
*/
export const getLocaleAmount = (amount: number, currencyCode: string) => {
const formatter = new Intl.NumberFormat([], {
style: "currency",
currencyDisplay: "narrowSymbol",
currency: currencyCode,
})
return formatter.format(amount)
}
export const getNativeSymbol = (currencyCode: string) => {
const formatted = new Intl.NumberFormat([], {
style: "currency",
currency: currencyCode,
currencyDisplay: "narrowSymbol",
}).format(0)
return formatted.replace(/\d/g, "").replace(/[.,]/g, "").trim()
}
/**
* In some cases we want to display the amount with the currency code and symbol,
* in the format of "symbol amount currencyCode". This breaks from the
* user's locale and is only used in cases where we want to display the
* currency code and symbol explicitly, e.g. for totals.
*/
export const getStylizedAmount = (amount: number, currencyCode: string) => {
const symbol = getNativeSymbol(currencyCode)
const decimalDigits = getDecimalDigits(currencyCode)
const total = amount.toLocaleString(undefined, {
minimumFractionDigits: decimalDigits,
maximumFractionDigits: decimalDigits,
})
return `${symbol} ${total} ${currencyCode.toUpperCase()}`
}