🔨 Refactor: refactor some util func

This commit is contained in:
萌萌哒赫萝
2023-05-11 12:52:35 +08:00
parent 80057c8561
commit 12be9d5802
3 changed files with 7 additions and 31 deletions

View File

@@ -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 = <T extends IStringKeyMap>(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'