Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: dissolve
currency
file (#5057)
- Loading branch information
1 parent
66c596d
commit a6edaef
Showing
47 changed files
with
408 additions
and
468 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
2 changes: 1 addition & 1 deletion
2
packages/desktop/views/dashboard/settings/views/general/Currency.svelte
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
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
1 change: 1 addition & 0 deletions
1
packages/shared/lib/core/i18n/constants/decimal-separators.constant.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 @@ | ||
export const DECIMAL_SEPARATORS = ['.', ','] |
9 changes: 9 additions & 0 deletions
9
packages/shared/lib/core/i18n/constants/default-locale-options.constant.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,9 @@ | ||
import { LocaleOptions } from '../types' | ||
|
||
export const DEFAULT_LOCALE_OPTIONS: LocaleOptions = { | ||
fallbackLocale: 'en', | ||
initialLocale: null, | ||
loadingDelay: 200, | ||
formats: {}, | ||
warnOnMissingMessages: true, | ||
} |
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 +1,3 @@ | ||
export * from './locale' | ||
export * from './decimal-separators.constant' | ||
export * from './default-locale-options.constant' | ||
export * from './supported-locales.constant' |
17 changes: 0 additions & 17 deletions
17
.../shared/lib/core/i18n/constants/locale.ts → ...n/constants/supported-locales.constant.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { getDecimalSeparator } from './getDecimalSeparator' | ||
|
||
export function ensureZeroes(val: string, maxZeros: number): string { | ||
const decimalSeparator = getDecimalSeparator() | ||
|
||
const parts = val.split(decimalSeparator) | ||
|
||
if (parts.length === 1) { | ||
parts[1] = '' | ||
if (maxZeros > 0) { | ||
parts[1].padEnd(maxZeros, '0') | ||
} | ||
} | ||
|
||
// If there are more then decimal places and it is just 0s remove them | ||
if (parts[1].length > maxZeros) { | ||
parts[1] = `${parts[1].slice(0, maxZeros)}${parts[1].slice(maxZeros).replace(/0+$/, '')}` | ||
} | ||
|
||
if (parts[1].length > 0) { | ||
return `${parts[0]}${decimalSeparator}${parts[1]}` | ||
} else { | ||
return parts[0] | ||
} | ||
} |
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,46 @@ | ||
import { get } from 'svelte/store' | ||
|
||
import { appSettings } from '@core/app' | ||
import { activeProfile } from '@core/profile' | ||
|
||
export function formatCurrency( | ||
value: number, | ||
currency: string | undefined = undefined, | ||
minDecimals: number | undefined = undefined, | ||
maxDecimals: number | undefined = undefined, | ||
grouped: boolean = false | ||
): string { | ||
if (Number.isNaN(value)) { | ||
return '' | ||
} | ||
|
||
const appLanguage = get(appSettings).language | ||
|
||
if (!currency) { | ||
currency = get(activeProfile)?.settings?.currency | ||
} | ||
|
||
const parts = Intl.NumberFormat(appLanguage, { | ||
style: 'currency', | ||
currency: currency ?? 'USD', | ||
currencyDisplay: 'symbol', | ||
minimumFractionDigits: minDecimals ?? 2, | ||
maximumFractionDigits: maxDecimals, | ||
useGrouping: grouped, | ||
}).formatToParts(value) | ||
|
||
// Default symbol usage does not always include a literal beside | ||
// the | ||
const curIndex = parts.findIndex((p) => p.type === 'currency') | ||
if (curIndex >= 0) { | ||
if (curIndex === 0) { | ||
if (parts[curIndex + 1].type !== 'literal') { | ||
parts.splice(curIndex + 1, 0, { type: 'literal', value: ' ' }) | ||
} | ||
} else if (parts[curIndex - 1].type !== 'literal') { | ||
parts.splice(curIndex, 0, { type: 'literal', value: ' ' }) | ||
} | ||
} | ||
|
||
return parts.map((p) => p.value).join('') | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/shared/lib/core/i18n/utils/formatCurrencyValue.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,23 @@ | ||
import { Currency, formatIotaUnitBestMatch } from '@core/utils' | ||
|
||
import { replaceCurrencyDecimal } from './replaceCurrencyDecimal' | ||
|
||
export function formatCurrencyValue( | ||
data: number | string, | ||
currency: string, | ||
fiatFixed: number = 2, | ||
btcFixed: number = 7, | ||
ethFixed: number = 6 | ||
): string { | ||
const parsedData: number = parseFloat(data.toString()) | ||
switch (currency.toLowerCase()) { | ||
case Currency.IOTA: | ||
return formatIotaUnitBestMatch(parsedData) | ||
case Currency.BTC: | ||
return replaceCurrencyDecimal(parsedData.toFixed(btcFixed), 'USD') | ||
case Currency.ETH: | ||
return replaceCurrencyDecimal(parsedData.toFixed(ethFixed), 'USD') | ||
default: | ||
return replaceCurrencyDecimal(parsedData.toFixed(fiatFixed), currency) | ||
} | ||
} |
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,34 @@ | ||
import { get } from 'svelte/store' | ||
|
||
import { appSettings } from '@core/app' | ||
|
||
import { ensureZeroes } from './ensureZeroes' | ||
|
||
export function formatNumber( | ||
value: number, | ||
minDecimals: number | undefined = undefined, | ||
maxDecimals: number | undefined = undefined, | ||
maxZeros: number = 2, | ||
grouped: boolean = false | ||
): string { | ||
// The decimals are truncated anyway if the value is larger than what JS can represent safely. | ||
if (value > Number.MAX_SAFE_INTEGER) { | ||
return String(value) | ||
} | ||
|
||
// The maximum decimals are equal to the max decimals of Ethereum. | ||
// Larger values throw an error when trying to format. | ||
if (maxDecimals > 18) { | ||
return String(value) | ||
} | ||
|
||
const appLanguage = get(appSettings).language | ||
|
||
const formatted = Intl.NumberFormat(appLanguage, { | ||
minimumFractionDigits: minDecimals ?? 2, | ||
maximumFractionDigits: maxDecimals, | ||
useGrouping: grouped, | ||
}).format(value) | ||
|
||
return ensureZeroes(formatted, maxZeros) | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/shared/lib/core/i18n/utils/getCurrencyPosition.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,14 @@ | ||
import { get } from 'svelte/store' | ||
|
||
import { appSettings } from '@core/app' | ||
|
||
export function getCurrencyPosition(): 'left' | 'right' { | ||
const appLanguage = get(appSettings).language | ||
|
||
const format = Intl.NumberFormat(appLanguage, { | ||
style: 'currency', | ||
currency: 'USD', | ||
}).formatToParts(1.1) | ||
|
||
return format.findIndex((p) => p.type === 'currency') === 0 ? 'left' : 'right' | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/shared/lib/core/i18n/utils/getDecimalSeparator.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,21 @@ | ||
import { get } from 'svelte/store' | ||
|
||
import { appSettings } from '@core/app' | ||
import { activeProfile } from '@core/profile' | ||
|
||
export function getDecimalSeparator(currency: string | undefined = undefined): string { | ||
const appLanguage = get(appSettings).language | ||
|
||
if (!currency) { | ||
currency = get(activeProfile)?.settings?.currency | ||
} | ||
|
||
return ( | ||
Intl.NumberFormat(appLanguage, { | ||
style: 'currency', | ||
currency: currency ?? 'USD', | ||
}) | ||
.formatToParts(1.1) | ||
.find((part) => part.type === 'decimal')?.value ?? '.' | ||
) | ||
} |
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,21 @@ | ||
import { get } from 'svelte/store' | ||
|
||
import { appSettings } from '@core/app' | ||
import { activeProfile } from '@core/profile' | ||
|
||
export function getGroupSeparator(currency: string | undefined = undefined): string { | ||
const appLanguage = get(appSettings).language | ||
|
||
if (!currency) { | ||
currency = get(activeProfile)?.settings?.currency | ||
} | ||
|
||
return ( | ||
Intl.NumberFormat(appLanguage, { | ||
style: 'currency', | ||
currency: currency ?? 'USD', | ||
}) | ||
.formatToParts(1111111) | ||
.find((part) => part.type === 'group')?.value ?? ',' | ||
) | ||
} |
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,9 @@ | ||
export * from './ensureZeroes' | ||
export * from './formatCurrency' | ||
export * from './formatCurrencyValue' | ||
export * from './formatNumber' | ||
export * from './getCurrencyPosition' | ||
export * from './getDecimalSeparator' | ||
export * from './getGroupSeparator' | ||
export * from './parseCurrency' | ||
export * from './replaceCurrencyDecimal' |
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,8 @@ | ||
import { getDecimalSeparator } from './getDecimalSeparator' | ||
import { getGroupSeparator } from './getGroupSeparator' | ||
|
||
export function parseCurrency(valueString: string, currency: string | undefined = undefined): number { | ||
// Need to escape the character in the regex in case it is . otherwise it will replace all characters | ||
const v = valueString?.replace(new RegExp(`\\${getGroupSeparator()}`, 'g'), '') | ||
return Number.parseFloat(v?.replace(getDecimalSeparator(currency), '.')) | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/shared/lib/core/i18n/utils/replaceCurrencyDecimal.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,5 @@ | ||
import { getDecimalSeparator } from './getDecimalSeparator' | ||
|
||
export function replaceCurrencyDecimal(value: string, currency: string | undefined = undefined): string { | ||
return value.replace('.', getDecimalSeparator(currency)) | ||
} |
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
4 changes: 2 additions & 2 deletions
4
packages/shared/lib/core/profile/interfaces/profile-settings.interface.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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { AvailableExchangeRates } from '@lib/typings/currency' | ||
import { ExchangeRate } from '@core/utils' | ||
|
||
export interface IProfileSettings { | ||
currency: AvailableExchangeRates | ||
currency: ExchangeRate | ||
lockScreenTimeoutInMinutes: number | ||
hideNetworkStatistics: boolean | ||
} |
Oops, something went wrong.