From 28b21bb4664fe7eb4c660eda64be3bcf906dd406 Mon Sep 17 00:00:00 2001 From: xingyu Date: Thu, 9 Nov 2023 11:29:32 +0800 Subject: [PATCH] refactor: fix type check --- .../Form/src/components/ImageUpload.vue | 8 +- .../Form/src/hooks/useFormEvents.ts | 14 +-- .../Form/src/hooks/useFormValues.ts | 8 +- src/components/SimpleMenu/src/SimpleMenu.vue | 4 +- .../src/components/settings/ColumnSetting.vue | 4 +- src/layouts/default/menu/index.vue | 4 +- src/layouts/default/tabs/useMultipleTabs.ts | 4 +- src/router/helper/menuHelper.ts | 4 +- src/router/helper/routeHelper.ts | 4 +- src/router/menus/index.ts | 4 +- src/utils/cache/memory.ts | 2 +- src/utils/cache/storageCache.ts | 6 +- src/utils/http/axios/index.ts | 4 +- src/utils/is.ts | 110 ++++++------------ 14 files changed, 67 insertions(+), 113 deletions(-) diff --git a/src/components/Form/src/components/ImageUpload.vue b/src/components/Form/src/components/ImageUpload.vue index ea0c8382..f70a9d3d 100644 --- a/src/components/Form/src/components/ImageUpload.vue +++ b/src/components/Form/src/components/ImageUpload.vue @@ -10,7 +10,7 @@ import { useI18n } from '@/hooks/web/useI18n' import { propTypes } from '@/utils/propTypes' import { buildShortUUID } from '@/utils/uuid' import { getAccessToken, getTenantId } from '@/utils/auth' -import { isArray, isNotEmpty, isUrl } from '@/utils/is' +import { isArray, isHttpUrl, isNil } from '@/utils/is' import { useRuleFormItem } from '@/hooks/component/useFormItem' import { useAttrs } from '@/hooks/core/useAttrs' @@ -69,7 +69,7 @@ watch( (v) => { fileState.newList = v .filter((item: any) => { - return item?.url && item.status === 'done' && isUrl(item?.url) + return item?.url && item.status === 'done' && isHttpUrl(item?.url) }) .map((item: any) => item?.url) fileState.newStr = join(fileState.newList) @@ -97,7 +97,7 @@ function changeFileValue(value: any) { fileState.oldStr = stateStr let list: string[] = [] if (props.multiple) { - if (isNotEmpty(value)) { + if (!isNil(value)) { if (isArray(value)) list = value as string[] else @@ -105,7 +105,7 @@ function changeFileValue(value: any) { } } else { - if (isNotEmpty(value)) + if (!isNil(value)) list.push(value as string) } fileList.value = list.map((item) => { diff --git a/src/components/Form/src/hooks/useFormEvents.ts b/src/components/Form/src/hooks/useFormEvents.ts index 6582795a..bc81506e 100644 --- a/src/components/Form/src/hooks/useFormEvents.ts +++ b/src/components/Form/src/hooks/useFormEvents.ts @@ -4,15 +4,7 @@ import { nextTick, toRaw, unref } from 'vue' import { cloneDeep, get, set, uniqBy } from 'lodash-es' import type { FormActionType, FormProps, FormSchemaInner as FormSchema } from '../types/form' import { dateItemType, defaultValueComponents, handleInputNumberValue } from '../helper' -import { - isArray, - isDef, - isEmpty, - isFunction, - isNullOrUnDef, - isObject, - isString, -} from '@/utils/is' +import { isArray, isDef, isEmpty, isFunction, isNil, isObject, isString } from '@/utils/is' import { deepMerge } from '@/utils' import { dateUtil } from '@/utils/dateUtil' import { error } from '@/utils/log' @@ -317,9 +309,9 @@ export function useFormEvents({ item.component !== 'Divider' && Reflect.has(item, 'field') && item.field - && !isNullOrUnDef(item.defaultValue) + && !isNil(item.defaultValue) && (!(item.field in currentFieldsValue) - || isNullOrUnDef(currentFieldsValue[item.field]) + || isNil(currentFieldsValue[item.field]) || isEmpty(currentFieldsValue[item.field])) ) obj[item.field] = item.defaultValue diff --git a/src/components/Form/src/hooks/useFormValues.ts b/src/components/Form/src/hooks/useFormValues.ts index 7166b842..2ff05d33 100644 --- a/src/components/Form/src/hooks/useFormValues.ts +++ b/src/components/Form/src/hooks/useFormValues.ts @@ -3,7 +3,7 @@ import type { ComputedRef, Ref } from 'vue' import { cloneDeep, get, set, unset } from 'lodash-es' import type { FormProps, FormSchemaInner as FormSchema } from '../types/form' import { dateUtil } from '@/utils/dateUtil' -import { isArray, isFunction, isNotEmpty, isNullOrUnDef, isObject, isString } from '@/utils/is' +import { isArray, isEmpty, isFunction, isNil, isObject, isString } from '@/utils/is' interface UseFormValuesContext { defaultValueRef: Ref @@ -113,10 +113,10 @@ export function useFormValues({ const [startTimeFormat, endTimeFormat] = Array.isArray(format) ? format : [format, format] - if (isNotEmpty(startTime)) + if (!isNil(startTime) && !isEmpty(startTime)) set(values, startTimeKey, formatTime(startTime, startTimeFormat)) - if (isNotEmpty(endTime)) + if (!isNil(startTime) && !isEmpty(startTime)) set(values, endTimeKey, formatTime(endTime, endTimeFormat)) unset(values, field) @@ -149,7 +149,7 @@ export function useFormValues({ formModel[field] = defaultValueObj![field] }) } - if (!isNullOrUnDef(defaultValue)) { + if (!isNil(defaultValue)) { obj[item.field] = defaultValue if (formModel[item.field] === undefined) diff --git a/src/components/SimpleMenu/src/SimpleMenu.vue b/src/components/SimpleMenu/src/SimpleMenu.vue index f5de2eb1..0d6e41ae 100644 --- a/src/components/SimpleMenu/src/SimpleMenu.vue +++ b/src/components/SimpleMenu/src/SimpleMenu.vue @@ -11,7 +11,7 @@ import { useDesign } from '@/hooks/web/useDesign' import { listenerRouteChange } from '@/logics/mitt/routeChange' import { propTypes } from '@/utils/propTypes' import { REDIRECT_NAME } from '@/router/constant' -import { isFunction, isUrl } from '@/utils/is' +import { isFunction, isHttpUrl } from '@/utils/is' import { openWindow } from '@/utils' defineOptions({ name: 'SimpleMenu', inheritAttrs: false }) @@ -99,7 +99,7 @@ async function handleMenuChange(route?: RouteLocationNormalizedLoaded) { } async function handleSelect(key: string) { - if (isUrl(key)) { + if (isHttpUrl(key)) { openWindow(key) return } diff --git a/src/components/Table/src/components/settings/ColumnSetting.vue b/src/components/Table/src/components/settings/ColumnSetting.vue index 26cf0c28..9a49d112 100644 --- a/src/components/Table/src/components/settings/ColumnSetting.vue +++ b/src/components/Table/src/components/settings/ColumnSetting.vue @@ -13,7 +13,7 @@ import { ScrollContainer } from '@/components/Container' import { useI18n } from '@/hooks/web/useI18n' import { useDesign } from '@/hooks/web/useDesign' -import { isFunction, isNullAndUnDef } from '@/utils/is' +import { isFunction, isNil } from '@/utils/is' import { getPopupContainer as getParentContainer } from '@/utils' interface State { @@ -208,7 +208,7 @@ function handleOpenChange() { handle: '.table-column-drag-icon ', onEnd: (evt) => { const { oldIndex, newIndex } = evt - if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) + if (isNil(oldIndex) || isNil(newIndex) || oldIndex === newIndex) return // Sort column diff --git a/src/layouts/default/menu/index.vue b/src/layouts/default/menu/index.vue index ac132877..8ebb3a87 100644 --- a/src/layouts/default/menu/index.vue +++ b/src/layouts/default/menu/index.vue @@ -15,7 +15,7 @@ import { ScrollContainer } from '@/components/Container' import { useGo } from '@/hooks/web/usePage' import { openWindow } from '@/utils' import { propTypes } from '@/utils/propTypes' -import { isUrl } from '@/utils/is' +import { isHttpUrl } from '@/utils/is' import { useRootSetting } from '@/hooks/setting/useRootSetting' import { useAppInject } from '@/hooks/web/useAppInject' import { useDesign } from '@/hooks/web/useDesign' @@ -115,7 +115,7 @@ export default defineComponent({ * @param path */ async function beforeMenuClickFn(path: string) { - if (!isUrl(path)) + if (!isHttpUrl(path)) return true openWindow(path) diff --git a/src/layouts/default/tabs/useMultipleTabs.ts b/src/layouts/default/tabs/useMultipleTabs.ts index 45c5cd2d..4c364208 100644 --- a/src/layouts/default/tabs/useMultipleTabs.ts +++ b/src/layouts/default/tabs/useMultipleTabs.ts @@ -5,7 +5,7 @@ import { useI18n } from '@/hooks/web/useI18n' import { useDesign } from '@/hooks/web/useDesign' import { useSortable } from '@/hooks/web/useSortable' import { useMultipleTabStore } from '@/store/modules/multipleTab' -import { isNullAndUnDef } from '@/utils/is' +import { isNil } from '@/utils/is' import projectSetting from '@/settings/projectSetting' const { t } = useI18n() @@ -70,7 +70,7 @@ export function useTabsDrag(affixTextList: string[]) { onEnd: (evt) => { const { oldIndex, newIndex } = evt - if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) + if (isNil(oldIndex) || isNil(newIndex) || oldIndex === newIndex) return tabStore.sortTabs(oldIndex, newIndex) diff --git a/src/router/helper/menuHelper.ts b/src/router/helper/menuHelper.ts index 9dfea944..ddeb8bb6 100644 --- a/src/router/helper/menuHelper.ts +++ b/src/router/helper/menuHelper.ts @@ -3,7 +3,7 @@ import type { RouteParams } from 'vue-router' import { toRaw } from 'vue' import type { AppRouteModule, AppRouteRecordRaw, Menu, MenuModule } from '@/router/types' import { findPath, treeMap } from '@/utils/helper/treeHelper' -import { isUrl } from '@/utils/is' +import { isHttpUrl } from '@/utils/is' export function getAllParentPath(treeData: T[], path: string) { const menuList = findPath(treeData, n => n.path === path) as Menu[] @@ -19,7 +19,7 @@ function joinParentPath(menus: Menu[], parentPath = '') { // 请注意,以 / 开头的嵌套路径将被视为根路径。 // This allows you to leverage the component nesting without having to use a nested URL. // 这允许你利用组件嵌套,而无需使用嵌套 URL。 - if (!(menu.path.startsWith('/') || isUrl(menu.path))) { + if (!(menu.path.startsWith('/') || isHttpUrl(menu.path))) { // path doesn't start with /, nor is it a url, join parent path // 路径不以 / 开头,也不是 url,加入父路径 menu.path = `${parentPath}/${menu.path}` diff --git a/src/router/helper/routeHelper.ts b/src/router/helper/routeHelper.ts index 85064441..f5bef83a 100644 --- a/src/router/helper/routeHelper.ts +++ b/src/router/helper/routeHelper.ts @@ -4,7 +4,7 @@ import { cloneDeep, omit } from 'lodash-es' import { EXCEPTION_COMPONENT, LAYOUT, getParentLayout } from '@/router/constant' import type { AppRouteModule, AppRouteRecordRaw } from '@/router/types' import { warn } from '@/utils/log' -import { isUrl } from '@/utils/is' +import { isHttpUrl } from '@/utils/is' export type LayoutMapKey = 'LAYOUT' const IFRAME = () => import('@/views/base/iframe/FrameBlank.vue') @@ -77,7 +77,7 @@ function dynamicImport(dynamicViewsModules: Record Promise(routeList: AppRouteModule[]): T[] { routeList.forEach((route) => { - if (isUrl(route.path)) + if (isHttpUrl(route.path)) route.component = 'IFrame' else if (route.children && route.parentId === 0) route.component = 'LAYOUT' diff --git a/src/router/menus/index.ts b/src/router/menus/index.ts index d4ad146a..c275eef9 100644 --- a/src/router/menus/index.ts +++ b/src/router/menus/index.ts @@ -6,7 +6,7 @@ import { useAppStoreWithOut } from '@/store/modules/app' import { usePermissionStore } from '@/store/modules/permission' import { getAllParentPath, transformMenuModule } from '@/router/helper/menuHelper' import { filter } from '@/utils/helper/treeHelper' -import { isUrl } from '@/utils/is' +import { isHttpUrl } from '@/utils/is' import { router } from '@/router' import { PermissionModeEnum } from '@/enums/appEnum' @@ -114,7 +114,7 @@ export async function getChildrenMenus(parentPath: string) { function basicFilter(routes: RouteRecordNormalized[]) { return (menu: Menu) => { const matchRoute = routes.find((route) => { - if (isUrl(menu.path)) + if (isHttpUrl(menu.path)) return true if (route.meta?.carryParam) diff --git a/src/utils/cache/memory.ts b/src/utils/cache/memory.ts index 51e9d244..59e77003 100644 --- a/src/utils/cache/memory.ts +++ b/src/utils/cache/memory.ts @@ -27,7 +27,7 @@ export class Memory { // get(key: K) { // const item = this.getItem(key); // const time = item?.time; - // if (!isNullOrUnDef(time) && time < new Date().getTime()) { + // if (!isNil(time) && time < new Date().getTime()) { // this.remove(key); // } // return item?.value ?? undefined; diff --git a/src/utils/cache/storageCache.ts b/src/utils/cache/storageCache.ts index 906adb72..77dd375f 100644 --- a/src/utils/cache/storageCache.ts +++ b/src/utils/cache/storageCache.ts @@ -1,5 +1,5 @@ import { cacheCipher } from '@/settings/encryptionSetting' -import { isNullOrUnDef } from '@/utils/is' +import { isNil } from '@/utils/is' import type { Encryption, EncryptionParams } from '@/utils/cipher' import { EncryptionFactory } from '@/utils/cipher' @@ -58,7 +58,7 @@ export function createStorage({ const stringData = JSON.stringify({ value, time: Date.now(), - expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null, + expire: !isNil(expire) ? new Date().getTime() + expire * 1000 : null, }) const stringifyValue = this.hasEncrypt ? this.encryption.encrypt(stringData) : stringData this.storage.setItem(this.getKey(key), stringifyValue) @@ -79,7 +79,7 @@ export function createStorage({ const decVal = this.hasEncrypt ? this.encryption.decrypt(val) : val const data = JSON.parse(decVal) const { value, expire } = data - if (isNullOrUnDef(expire) || expire >= new Date().getTime()) + if (isNil(expire) || expire >= new Date().getTime()) return value this.remove(key) diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index fd6d08e9..cfa755c6 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -12,7 +12,7 @@ import type { RequestOptions, Result } from '@/types/axios' import { useGlobSetting } from '@/hooks/setting' import { useMessage } from '@/hooks/web/useMessage' import { ContentTypeEnum, RequestEnum, ResultEnum } from '@/enums/httpEnum' -import { isEmpty, isNull, isString, isUnDef } from '@/utils/is' +import { isEmpty, isNull, isString, isUndefined } from '@/utils/is' import { getAccessToken, getTenantId } from '@/utils/auth' import { deepMerge, setObjToUrlParams } from '@/utils' import { useErrorLogStoreWithOut } from '@/store/modules/errorLog' @@ -67,7 +67,7 @@ const transform: AxiosTransform = { if (successMsg === null || successMsg === undefined || successMsg === '') successMsg = t('sys.api.operationSuccess') - if (isNull(successMsg) || isUnDef(successMsg) || isEmpty(successMsg)) + if (isNull(successMsg) || isUndefined(successMsg) || isEmpty(successMsg)) successMsg = t('sys.api.operationSuccess') if (options.successMessageMode === 'modal') diff --git a/src/utils/is.ts b/src/utils/is.ts index 15946c7b..c4c19176 100644 --- a/src/utils/is.ts +++ b/src/utils/is.ts @@ -1,4 +1,38 @@ -import { isNil } from 'lodash-es' +export { + isArguments, + isArrayBuffer, + isArrayLike, + isArrayLikeObject, + isBuffer, + isBoolean, + isDate, + isElement, + isEmpty, + isEqual, + isEqualWith, + isError, + isFunction, + isFinite, + isLength, + isMap, + isMatch, + isMatchWith, + isNative, + isNil, + isNumber, + isNull, + isObjectLike, + isPlainObject, + isRegExp, + isSafeInteger, + isSet, + isString, + isSymbol, + isTypedArray, + isUndefined, + isWeakMap, + isWeakSet, +} from 'lodash-es' const toString = Object.prototype.toString @@ -10,74 +44,10 @@ export function isDef(val?: T): val is T { return typeof val !== 'undefined' } -export function isUnDef(val?: T): val is T { - return !isDef(val) -} - export function isObject(val: any): val is Record { return val !== null && is(val, 'Object') } -export function isNotEmpty(val: any): boolean { - return !isNil(val) && !isEmpty(val) -} - -export function isEmpty(val: T): val is T { - if (isNil(val)) - return true - - if (isArray(val) || isString(val)) - return val.length === 0 - - if (val instanceof Map || val instanceof Set) - return val.size === 0 - - if (isObject(val)) - return Object.keys(val).length === 0 - - return false -} - -export function isDate(val: unknown): val is Date { - return is(val, 'Date') -} - -export function isNull(val: unknown): val is null { - return val === null -} - -export function isNullAndUnDef(val: unknown): val is null | undefined { - return isUnDef(val) && isNull(val) -} - -export function isNullOrUnDef(val: unknown): val is null | undefined { - return isUnDef(val) || isNull(val) -} - -export function isNumber(val: unknown): val is number { - return is(val, 'Number') -} - -export function isPromise(val: unknown): val is Promise { - return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch) -} - -export function isString(val: unknown): val is string { - return is(val, 'String') -} - -export function isFunction(val: unknown): val is Function { - return typeof val === 'function' -} - -export function isBoolean(val: unknown): val is boolean { - return is(val, 'Boolean') -} - -export function isRegExp(val: unknown): val is RegExp { - return is(val, 'RegExp') -} - export function isArray(val: any): val is Array { return val && Array.isArray(val) } @@ -86,19 +56,11 @@ export function isWindow(val: any): val is Window { return typeof window !== 'undefined' && is(val, 'Window') } -export function isElement(val: unknown): val is Element { - return isObject(val) && !!val.tagName -} - -export function isMap(val: unknown): val is Map { - return is(val, 'Map') -} - export const isServer = typeof window === 'undefined' export const isClient = !isServer -export function isUrl(path: string): boolean { +export function isHttpUrl(path: string): boolean { const reg = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/ return reg.test(path) }