code refactor
This commit is contained in:
@@ -83,3 +83,33 @@ export function getExchangedAmount(amount, fromRate, toRate) {
|
||||
|
||||
return amount * exchangeRate;
|
||||
}
|
||||
|
||||
export function getConvertedAmount(baseAmount, fromExchangeRate, toExchangeRate) {
|
||||
if (!fromExchangeRate || !toExchangeRate) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (baseAmount === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return getExchangedAmount(baseAmount, fromExchangeRate.rate, toExchangeRate.rate);
|
||||
}
|
||||
|
||||
export function getDisplayExchangeRateAmount(rateStr, isEnableThousandsSeparator) {
|
||||
if (rateStr.indexOf('.') < 0) {
|
||||
return appendThousandsSeparator(rateStr, isEnableThousandsSeparator);
|
||||
} else {
|
||||
let firstNonZeroPos = 0;
|
||||
|
||||
for (let i = 0; i < rateStr.length; i++) {
|
||||
if (rateStr.charAt(i) !== '.' && rateStr.charAt(i) !== '0') {
|
||||
firstNonZeroPos = Math.min(i + 4, rateStr.length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const trimmedRateStr = rateStr.substring(0, Math.max(6, Math.max(firstNonZeroPos, rateStr.indexOf('.') + 2)));
|
||||
return appendThousandsSeparator(trimmedRateStr, isEnableThousandsSeparator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,6 +627,30 @@ function getAllTransactionEditScopeTypes(translateFn) {
|
||||
}];
|
||||
}
|
||||
|
||||
function getAllDisplayExchangeRates(exchangeRatesData, translateFn) {
|
||||
if (!exchangeRatesData || !exchangeRatesData.exchangeRates) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const availableExchangeRates = [];
|
||||
|
||||
for (let i = 0; i < exchangeRatesData.exchangeRates.length; i++) {
|
||||
const exchangeRate = exchangeRatesData.exchangeRates[i];
|
||||
|
||||
availableExchangeRates.push({
|
||||
currencyCode: exchangeRate.currency,
|
||||
currencyDisplayName: translateFn(`currency.${exchangeRate.currency}`),
|
||||
rate: exchangeRate.rate
|
||||
});
|
||||
}
|
||||
|
||||
availableExchangeRates.sort(function(c1, c2) {
|
||||
return c1.currencyDisplayName.localeCompare(c2.currencyDisplayName);
|
||||
})
|
||||
|
||||
return availableExchangeRates;
|
||||
}
|
||||
|
||||
function getEnableDisableOptions(translateFn) {
|
||||
return [{
|
||||
value: true,
|
||||
@@ -906,6 +930,7 @@ export function i18nFunctions(i18nGlobal) {
|
||||
getAllStatisticsChartDataTypes: () => getAllStatisticsChartDataTypes(i18nGlobal.t),
|
||||
getAllStatisticsSortingTypes: () => getAllStatisticsSortingTypes(i18nGlobal.t),
|
||||
getAllTransactionEditScopeTypes: () => getAllTransactionEditScopeTypes(i18nGlobal.t),
|
||||
getAllDisplayExchangeRates: (exchangeRatesData) => getAllDisplayExchangeRates(exchangeRatesData, i18nGlobal.t),
|
||||
getEnableDisableOptions: () => getEnableDisableOptions(i18nGlobal.t),
|
||||
getDisplayCurrency: (value, currencyCode, options) => getDisplayCurrency(value, currencyCode, options, i18nGlobal.t),
|
||||
setLanguage: (locale, force) => setLanguage(i18nGlobal, locale, force),
|
||||
|
||||
@@ -84,8 +84,7 @@ import { useSettingsStore } from '@/stores/setting.js';
|
||||
import { useUserStore } from '@/stores/user.js';
|
||||
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
|
||||
|
||||
import { appendThousandsSeparator } from '@/lib/common.js';
|
||||
import { getExchangedAmount } from '@/lib/currency.js';
|
||||
import { getConvertedAmount, getDisplayExchangeRateAmount } from '@/lib/currency.js';
|
||||
|
||||
import {
|
||||
mdiRefresh
|
||||
@@ -117,27 +116,7 @@ export default {
|
||||
return exchangeRatesLastUpdateTime ? this.$locale.formatUnixTimeToLongDate(this.userStore, exchangeRatesLastUpdateTime) : '';
|
||||
},
|
||||
availableExchangeRates() {
|
||||
if (!this.exchangeRatesData || !this.exchangeRatesData.exchangeRates) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const availableExchangeRates = [];
|
||||
|
||||
for (let i = 0; i < this.exchangeRatesData.exchangeRates.length; i++) {
|
||||
const exchangeRate = this.exchangeRatesData.exchangeRates[i];
|
||||
|
||||
availableExchangeRates.push({
|
||||
currencyCode: exchangeRate.currency,
|
||||
currencyDisplayName: this.$t(`currency.${exchangeRate.currency}`),
|
||||
rate: exchangeRate.rate
|
||||
});
|
||||
}
|
||||
|
||||
availableExchangeRates.sort(function(c1, c2) {
|
||||
return c1.currencyDisplayName.localeCompare(c2.currencyDisplayName);
|
||||
})
|
||||
|
||||
return availableExchangeRates;
|
||||
return this.$locale.getAllDisplayExchangeRates(this.exchangeRatesData);
|
||||
}
|
||||
},
|
||||
created() {
|
||||
@@ -184,38 +163,15 @@ export default {
|
||||
|
||||
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency];
|
||||
|
||||
if (!fromExchangeRate) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (this.baseAmount === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
return getExchangedAmount(parseFloat(this.baseAmount), fromExchangeRate.rate, toExchangeRate.rate);
|
||||
return getConvertedAmount(parseFloat(this.baseAmount), fromExchangeRate, toExchangeRate);
|
||||
} catch (e) {
|
||||
return 0;
|
||||
}
|
||||
},
|
||||
getDisplayConvertedAmount(toExchangeRate) {
|
||||
const rateStr = this.getConvertedAmount(toExchangeRate).toString();
|
||||
|
||||
if (rateStr.indexOf('.') < 0) {
|
||||
return appendThousandsSeparator(rateStr, this.isEnableThousandsSeparator);
|
||||
} else {
|
||||
let firstNonZeroPos = 0;
|
||||
|
||||
for (let i = 0; i < rateStr.length; i++) {
|
||||
if (rateStr.charAt(i) !== '.' && rateStr.charAt(i) !== '0') {
|
||||
firstNonZeroPos = Math.min(i + 4, rateStr.length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const trimmedRateStr = rateStr.substring(0, Math.max(6, Math.max(firstNonZeroPos, rateStr.indexOf('.') + 2)));
|
||||
return appendThousandsSeparator(trimmedRateStr, this.isEnableThousandsSeparator);
|
||||
}
|
||||
return getDisplayExchangeRateAmount(rateStr, this.isEnableThousandsSeparator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,11 +96,12 @@ import { useUserStore } from '@/stores/user.js';
|
||||
import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
|
||||
|
||||
import transactionConstants from '@/consts/transaction.js';
|
||||
import { isNumber, appendThousandsSeparator } from '@/lib/common.js';
|
||||
import { isNumber } from '@/lib/common.js';
|
||||
import {
|
||||
numericCurrencyToString,
|
||||
stringCurrencyToNumeric,
|
||||
getExchangedAmount,
|
||||
getConvertedAmount,
|
||||
getDisplayExchangeRateAmount
|
||||
} from '@/lib/currency.js';
|
||||
|
||||
export default {
|
||||
@@ -128,27 +129,7 @@ export default {
|
||||
return exchangeRatesLastUpdateTime ? this.$locale.formatUnixTimeToLongDate(this.userStore, exchangeRatesLastUpdateTime) : '';
|
||||
},
|
||||
availableExchangeRates() {
|
||||
if (!this.exchangeRatesData || !this.exchangeRatesData.exchangeRates) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const availableExchangeRates = [];
|
||||
|
||||
for (let i = 0; i < this.exchangeRatesData.exchangeRates.length; i++) {
|
||||
const exchangeRate = this.exchangeRatesData.exchangeRates[i];
|
||||
|
||||
availableExchangeRates.push({
|
||||
currencyCode: exchangeRate.currency,
|
||||
currencyDisplayName: this.$t(`currency.${exchangeRate.currency}`),
|
||||
rate: exchangeRate.rate
|
||||
});
|
||||
}
|
||||
|
||||
availableExchangeRates.sort(function(c1, c2) {
|
||||
return c1.currencyDisplayName.localeCompare(c2.currencyDisplayName);
|
||||
})
|
||||
|
||||
return availableExchangeRates;
|
||||
return this.$locale.getAllDisplayExchangeRates(this.exchangeRatesData);
|
||||
},
|
||||
displayBaseAmount() {
|
||||
return numericCurrencyToString(this.baseAmount, this.isEnableThousandsSeparator);
|
||||
@@ -228,31 +209,11 @@ export default {
|
||||
},
|
||||
getConvertedAmount(toExchangeRate) {
|
||||
const fromExchangeRate = this.exchangeRatesStore.latestExchangeRateMap[this.baseCurrency];
|
||||
|
||||
if (!fromExchangeRate) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return getExchangedAmount(this.baseAmount / 100, fromExchangeRate.rate, toExchangeRate.rate);
|
||||
return getConvertedAmount(this.baseAmount / 100, fromExchangeRate, toExchangeRate);
|
||||
},
|
||||
getDisplayConvertedAmount(toExchangeRate) {
|
||||
const rateStr = this.getConvertedAmount(toExchangeRate).toString();
|
||||
|
||||
if (rateStr.indexOf('.') < 0) {
|
||||
return appendThousandsSeparator(rateStr, this.isEnableThousandsSeparator);
|
||||
} else {
|
||||
let firstNonZeroPos = 0;
|
||||
|
||||
for (let i = 0; i < rateStr.length; i++) {
|
||||
if (rateStr.charAt(i) !== '.' && rateStr.charAt(i) !== '0') {
|
||||
firstNonZeroPos = Math.min(i + 4, rateStr.length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const trimmedRateStr = rateStr.substring(0, Math.max(6, Math.max(firstNonZeroPos, rateStr.indexOf('.') + 2)));
|
||||
return appendThousandsSeparator(trimmedRateStr, this.isEnableThousandsSeparator);
|
||||
}
|
||||
return getDisplayExchangeRateAmount(rateStr, this.isEnableThousandsSeparator);
|
||||
},
|
||||
setAsBaseline(currency, amount) {
|
||||
if (!isNumber(amount)) {
|
||||
|
||||
Reference in New Issue
Block a user