From 12be9d580211acb051b8d6566776bd278958cefe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E8=90=8C=E5=93=92=E8=B5=AB=E8=90=9D?= Date: Thu, 11 May 2023 12:52:35 +0800 Subject: [PATCH] :hammer: Refactor: refactor some util func --- src/renderer/utils/common.ts | 8 -------- src/renderer/utils/dataSender.ts | 13 +++---------- src/universal/utils/common.ts | 17 ++++------------- 3 files changed, 7 insertions(+), 31 deletions(-) diff --git a/src/renderer/utils/common.ts b/src/renderer/utils/common.ts index 964cb67..db2b617 100644 --- a/src/renderer/utils/common.ts +++ b/src/renderer/utils/common.ts @@ -11,14 +11,6 @@ export const handleTalkingDataEvent = (data: ITalkingDataOptions) => { } } -export const trimValues = (obj: IStringKeyMap) => { - const newObj = {} as IStringKeyMap - Object.keys(obj).forEach(key => { - newObj[key] = typeof obj[key] === 'string' ? obj[key].trim() : obj[key] - }) - return newObj -} - /** * get raw data from reactive or ref */ diff --git a/src/renderer/utils/dataSender.ts b/src/renderer/utils/dataSender.ts index 3a2ad34..531e490 100644 --- a/src/renderer/utils/dataSender.ts +++ b/src/renderer/utils/dataSender.ts @@ -4,16 +4,9 @@ import { v4 as uuid } from 'uuid' import { IRPCActionType } from '~/universal/types/enum' import { getRawData } from './common' -export function saveConfig (_config: IObj | string, value?: any) { - let config - if (typeof _config === 'string') { - config = { - [_config]: value - } - } else { - config = getRawData(_config) - } - ipcRenderer.send(PICGO_SAVE_CONFIG, config) +export function saveConfig (config: IObj | string, value?: any) { + const configObject = typeof config === 'string' ? { [config]: value } : getRawData(config) + ipcRenderer.send(PICGO_SAVE_CONFIG, configObject) } export function getConfig (key?: string): Promise { diff --git a/src/universal/utils/common.ts b/src/universal/utils/common.ts index 262cc82..67e1a93 100644 --- a/src/universal/utils/common.ts +++ b/src/universal/utils/common.ts @@ -20,12 +20,7 @@ export const isUrlEncode = (url: string): boolean => { } } -export const handleUrlEncode = (url: string): string => { - if (!isUrlEncode(url)) { - url = encodeURI(url) - } - return url -} +export const handleUrlEncode = (url: string): string => isUrlEncode(url) ? url : encodeURI(url) /** * streamline the full plugin name to a simple one @@ -47,17 +42,13 @@ export const simpleClone = (obj: any) => { } export const enforceNumber = (num: number | string) => { - return isNaN(Number(num)) ? 0 : Number(num) + return isNaN(+num) ? 0 : +num } export const isDev = process.env.NODE_ENV === 'development' -export const trimValues = (obj: IStringKeyMap) => { - const newObj = {} as IStringKeyMap - Object.keys(obj).forEach(key => { - newObj[key] = typeof obj[key] === 'string' ? obj[key].trim() : obj[key] - }) - return newObj +export const trimValues = (obj: T): {[K in keyof T]: T[K] extends string ? string : T[K]} => { + return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, typeof value === 'string' ? value.trim() : value])) as {[K in keyof T]: T[K] extends string ? string : T[K]} } const c1nApi = 'https://c1n.cn/link/short'