code refactor

This commit is contained in:
MaysWind
2025-01-15 23:15:42 +08:00
parent dc4310c301
commit 922c338387
11 changed files with 65 additions and 64 deletions
+4 -4
View File
@@ -41,11 +41,11 @@ export function useItemIconBase(props: CommonIconProps) {
iconId = iconId.toString();
}
if (!ALL_ACCOUNT_ICONS[iconId as string]) {
if (!ALL_ACCOUNT_ICONS[iconId]) {
return DEFAULT_ACCOUNT_ICON.icon;
}
return ALL_ACCOUNT_ICONS[iconId as string].icon;
return ALL_ACCOUNT_ICONS[iconId].icon;
}
function getCategoryIcon(iconId: string | number): string {
@@ -53,11 +53,11 @@ export function useItemIconBase(props: CommonIconProps) {
iconId = iconId.toString();
}
if (!ALL_CATEGORY_ICONS[iconId as string]) {
if (!ALL_CATEGORY_ICONS[iconId]) {
return DEFAULT_CATEGORY_ICON.icon;
}
return ALL_CATEGORY_ICONS[iconId as string].icon;
return ALL_CATEGORY_ICONS[iconId].icon;
}
function getAccountIconStyle(color?: ColorValue | string, defaultColor?: ColorValue | string, additionalColorAttr?: string): Record<IconItemStyleName, IconItemStyleValue> {
+4 -4
View File
@@ -57,15 +57,15 @@ function open(titleOrText: string, textOrOptions?: string | Record<string, unkno
} else if (isString(textOrOptions)) { // second parameter is text
if (!options) {
titleContent.value = tt(titleOrText);
textContent.value = tt(textOrOptions as string);
textContent.value = tt(textOrOptions);
} else {
titleContent.value = tt(titleOrText, options);
textContent.value = tt(textOrOptions as string, options);
textContent.value = tt(textOrOptions, options);
}
}
if (options && isString(options.color)) {
finalColor.value = (options.color as string) || 'primary';
if (options && isString(options['color'])) {
finalColor.value = (options['color'] as string) || 'primary';
}
return new Promise((resolve, reject) => {
+5 -5
View File
@@ -13,7 +13,7 @@ import { ref, watch } from 'vue';
import { useI18n } from '@/locales/helpers.ts';
import { isObject } from '@/lib/common.ts';
import { isObject, isString } from '@/lib/common.ts';
const emit = defineEmits<{
(e: 'update:show', value: boolean): void;
@@ -37,10 +37,10 @@ function showMessage(message: string, options?: Record<string, unknown>): void {
function showError(error: string | { message: string }): void {
showState.value = true;
if (isObject(error) && (error as { message: string }).message) {
messageContent.value = te((error as { message: string }).message);
} else {
messageContent.value = te(error as string);
if (isObject(error) && error.message) {
messageContent.value = te(error.message);
} else if (isString(error)) {
messageContent.value = te(error);
}
}
+4 -4
View File
@@ -210,7 +210,7 @@ function inputNum(num: number): void {
if (isNumber(props.minValue)) {
const current = parseAmount(newValue);
if (current < (props.minValue as number)) {
if (current < (props.minValue)) {
return;
}
}
@@ -218,7 +218,7 @@ function inputNum(num: number): void {
if (isNumber(props.maxValue)) {
const current = parseAmount(newValue);
if (current > (props.maxValue as number)) {
if (current > (props.maxValue)) {
return;
}
}
@@ -308,14 +308,14 @@ function confirm(): boolean {
}
if (isNumber(props.minValue)) {
if (finalValue < (props.minValue as number)) {
if (finalValue < (props.minValue)) {
showToast('Numeric Overflow');
return false;
}
}
if (isNumber(props.maxValue)) {
if (finalValue > (props.maxValue as number)) {
if (finalValue > (props.maxValue)) {
showToast('Numeric Overflow');
return false;
}
+25 -24
View File
@@ -1,16 +1,17 @@
export function isFunction(val: unknown): boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function isFunction(val: unknown): val is Function {
return typeof(val) === 'function';
}
export function isDefined(val: unknown): boolean {
return typeof val !== 'undefined';
export function isDefined<T>(val: T | null | undefined): val is T {
return val !== null && typeof(val) !== 'undefined';
}
export function isObject(val: unknown): boolean {
export function isObject(val: unknown): val is object {
return val != null && typeof(val) === 'object' && !isArray(val);
}
export function isArray(val: unknown): boolean {
export function isArray(val: unknown): val is [] {
if (isFunction(Array.isArray)) {
return Array.isArray(val);
}
@@ -18,24 +19,24 @@ export function isArray(val: unknown): boolean {
return Object.prototype.toString.call(val) === '[object Array]';
}
export function isString(val: unknown): boolean {
export function isString(val: unknown): val is string {
return typeof(val) === 'string';
}
export function isNumber(val: unknown): boolean {
export function isNumber(val: unknown): val is number {
return typeof(val) === 'number';
}
export function isInteger(val: unknown): boolean {
export function isInteger(val: unknown): val is number {
return Number.isInteger(val);
}
export function isBoolean(val: unknown): boolean {
export function isBoolean(val: unknown): val is boolean {
return typeof(val) === 'boolean';
}
export function isYearMonth(val: unknown): boolean {
if (typeof(val) !== 'string') {
if (!isString(val)) {
return false;
}
@@ -54,8 +55,8 @@ export function isEquals(obj1: unknown, obj2: unknown): boolean {
}
if (isArray(obj1) && isArray(obj2)) {
const arr1 = obj1 as unknown[];
const arr2 = obj2 as unknown[];
const arr1 = obj1;
const arr2 = obj2;
if (arr1.length !== arr2.length) {
return false;
@@ -69,8 +70,8 @@ export function isEquals(obj1: unknown, obj2: unknown): boolean {
return true;
} else if (isObject(obj1) && isObject(obj2)) {
const keys1 = Object.keys(obj1 as Record<string, unknown>);
const keys2 = Object.keys(obj2 as Record<string, unknown>);
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
@@ -135,9 +136,9 @@ export function isObjectEmpty(obj: object): boolean {
export function getNumberValue(value: unknown, defaultValue: number): number {
if (isString(value)) {
return parseInt(value as string, 10);
return parseInt(value, 10);
} else if (isNumber(value)) {
return value as number;
return value;
} else {
return defaultValue;
}
@@ -254,7 +255,7 @@ export function stringToArrayBuffer(str: string): ArrayBuffer {
}
export function getFirstVisibleItem<T>(items: Record<string, T>[] | Record<string, Record<string, T>>, hiddenField: string): Record<string, T> | null {
if (isArray(items) && (items as Record<string, T>[]).length > 0) {
if (isArray(items) && items.length > 0) {
const arr = items as Record<string, T>[];
for (let i = 0; i < arr.length; i++) {
@@ -326,7 +327,7 @@ export function getNameByKeyValue<T>(src: Record<string, T>[] | Record<string, R
}
}
} else if (isNumber(value)) {
const index = value as number;
const index = value;
if (arr[index]) {
const option = arr[index];
@@ -350,7 +351,7 @@ export function getNameByKeyValue<T>(src: Record<string, T>[] | Record<string, R
}
}
} else if (isString(value)) {
const key = value as string;
const key = value;
if (obj[key]) {
const option = obj[key];
@@ -378,20 +379,20 @@ export function copyObjectTo(fromObject: Record<string, unknown> | undefined, to
}
const fromValue = fromObject[key];
const toValue = (toObject as Record<string, unknown>)[key];
const toValue = toObject[key];
if (isArray(fromValue)) {
(toObject as Record<string, unknown>)[key] = copyArrayTo(fromValue as unknown[], toValue as unknown[]);
toObject[key] = copyArrayTo(fromValue, toValue as unknown[]);
} else if (isObject(fromValue)) {
(toObject as Record<string, unknown>)[key] = copyObjectTo(fromValue as Record<string, unknown>, toValue as Record<string, unknown>);
toObject[key] = copyObjectTo(fromValue as Record<string, unknown>, toValue as Record<string, unknown>);
} else {
if (fromValue !== toValue) {
(toObject as Record<string, unknown>)[key] = fromValue;
toObject[key] = fromValue;
}
}
}
return (toObject as Record<string, unknown>);
return toObject;
}
export function copyArrayTo<T>(fromArray: T[], toArray: T[]): T[] {
+3 -3
View File
@@ -14,7 +14,7 @@ export function getCurrencyFraction(currencyCode?: string): number | undefined {
export function appendCurrencySymbol(value: unknown, currencyDisplayType: CurrencyDisplayType, currencyCode: string, currencyUnit: string, currencyName: string, isPlural: boolean): string | null {
if (isNumber(value)) {
value = (value as number).toString();
value = value.toString();
}
if (!isString(value)) {
@@ -24,11 +24,11 @@ export function appendCurrencySymbol(value: unknown, currencyDisplayType: Curren
const symbol = getAmountPrependAndAppendCurrencySymbol(currencyDisplayType, currencyCode, currencyUnit, currencyName, isPlural);
if (!symbol) {
return value as string;
return value;
}
const separator = currencyDisplayType.separator || '';
let ret = value as string;
let ret = value;
if (symbol.prependText) {
ret = symbol.prependText + separator + ret;
+8 -8
View File
@@ -185,7 +185,7 @@ export function parseDateFromUnixTime(unixTime: number, utcOffset?: number, curr
currentUtcOffset = getTimezoneOffsetMinutes();
}
unixTime = getDummyUnixTimeForLocalUsage(unixTime, utcOffset as number, currentUtcOffset as number);
unixTime = getDummyUnixTimeForLocalUsage(unixTime, utcOffset, currentUtcOffset);
}
return moment.unix(unixTime);
@@ -354,9 +354,9 @@ export function getYearMonthFirstUnixTime(yearMonth: YearMonth | string): number
let yearMonthObj: YearMonth | null = null;
if (isString(yearMonth)) {
yearMonthObj = getYearMonthObjectFromString(yearMonth as string);
} else if (isObject(yearMonth) && isYearMonthValid((yearMonth as YearMonth).year, (yearMonth as YearMonth).month)) {
yearMonthObj = yearMonth as YearMonth;
yearMonthObj = getYearMonthObjectFromString(yearMonth);
} else if (isObject(yearMonth) && isYearMonthValid(yearMonth.year, yearMonth.month)) {
yearMonthObj = yearMonth;
}
if (!yearMonthObj) {
@@ -375,15 +375,15 @@ export function getStartEndYearMonthRange(startYearMonth: YearMonth | string, en
let endYearMonthObj: YearMonth | null = null;
if (isString(startYearMonth)) {
startYearMonthObj = getYearMonthObjectFromString(startYearMonth as string);
startYearMonthObj = getYearMonthObjectFromString(startYearMonth);
} else if (isObject(startYearMonth)) {
startYearMonthObj = startYearMonth as YearMonth;
startYearMonthObj = startYearMonth;
}
if (isString(endYearMonth)) {
endYearMonthObj = getYearMonthObjectFromString(endYearMonth as string);
endYearMonthObj = getYearMonthObjectFromString(endYearMonth);
} else {
endYearMonthObj = endYearMonth as YearMonth;
endYearMonthObj = endYearMonth;
}
if (!startYearMonthObj || !endYearMonthObj) {
+3 -3
View File
@@ -9,7 +9,7 @@ export function appendDigitGroupingSymbol(value: number | string, options: Numbe
if (isNumber(value)) {
textualValue = value.toString();
} else {
textualValue = value as string;
textualValue = value;
}
if (!textualValue) {
@@ -137,7 +137,7 @@ export function formatAmount(value: number | string, options: NumberFormatOption
if (isNumber(value)) {
textualValue = value.toString();
} else {
textualValue = value as string;
textualValue = value;
}
if (!textualValue) {
@@ -157,7 +157,7 @@ export function formatAmount(value: number | string, options: NumberFormatOption
const decimalSeparator = options.decimalSeparator || DecimalSeparator.Default.symbol;
let decimalNumberCount = options.decimalNumberCount;
if (!isNumber(decimalNumberCount) || (decimalNumberCount as number) > MAX_SUPPORTED_DECIMAL_NUMBER_COUNT) {
if (!isNumber(decimalNumberCount) || decimalNumberCount > MAX_SUPPORTED_DECIMAL_NUMBER_COUNT) {
decimalNumberCount = DEFAULT_DECIMAL_NUMBER_COUNT;
}
+6 -6
View File
@@ -488,7 +488,7 @@ export function useI18n() {
return undefined;
}
if (text && isTranslate) {
if (isTranslate) {
return t(text);
}
@@ -499,12 +499,12 @@ export function useI18n() {
let finalMessage = '';
let parameters = {};
if (isObject(message) && isObject((message as { error: ErrorResponse }).error)) {
const localizedError = getLocalizedError((message as { error: ErrorResponse }).error);
if (isObject(message) && isObject(message.error)) {
const localizedError = getLocalizedError(message.error);
finalMessage = localizedError.message;
parameters = getLocalizedErrorParameters(localizedError.parameters);
} else if (isString(message)) {
finalMessage = message as string;
finalMessage = message;
} else {
return '';
}
@@ -941,7 +941,7 @@ export function useI18n() {
weekdayTypesMap[weekdayTypes[i]] = true;
}
const allWeekDays = getAllWeekDays(firstDayOfWeek as number);
const allWeekDays = getAllWeekDays(firstDayOfWeek);
const finalWeekdayNames = [];
for (let i = 0; i < allWeekDays.length; i++) {
@@ -1094,7 +1094,7 @@ export function useI18n() {
value = value.toString();
}
let textualValue = value as string;
let textualValue = value;
const isPlural: boolean = textualValue !== '100' && textualValue !== '-100';
if (!notConvertValue) {
+1 -1
View File
@@ -37,7 +37,7 @@ export class TransactionCategory implements TransactionCategoryInfoResponse {
}
get subCategories(): TransactionCategoryInfoResponse[] | undefined {
if (typeof this.secondaryCategories === 'undefined') {
if (typeof(this.secondaryCategories) === 'undefined') {
return undefined;
}
+2 -2
View File
@@ -210,13 +210,13 @@ export const useOverviewStore = defineStore('overview', () => {
const expenseAmount = exchangeRatesStore.getExchangedAmount(amount.expenseAmount, amount.currency, defaultCurrency);
if (isNumber(incomeAmount)) {
totalIncomeAmount += Math.floor(incomeAmount as number);
totalIncomeAmount += Math.floor(incomeAmount);
} else {
hasUnCalculatedTotalIncome = true;
}
if (isNumber(expenseAmount)) {
totalExpenseAmount += Math.floor(expenseAmount as number);
totalExpenseAmount += Math.floor(expenseAmount);
} else {
hasUnCalculatedTotalExpense = true;
}