refactor: fix type check (#3246)

This commit is contained in:
Kirk Lin 2023-11-07 10:31:32 +08:00 committed by GitHub
parent 11cbe66937
commit f91d777e4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 63 additions and 113 deletions

View File

@ -2,15 +2,7 @@ import type { ComputedRef, Ref } from 'vue';
import type { FormProps, FormSchemaInner as FormSchema, FormActionType } from '../types/form'; import type { FormProps, FormSchemaInner as FormSchema, FormActionType } from '../types/form';
import type { NamePath } from 'ant-design-vue/lib/form/interface'; import type { NamePath } from 'ant-design-vue/lib/form/interface';
import { unref, toRaw, nextTick } from 'vue'; import { unref, toRaw, nextTick } from 'vue';
import { import { isArray, isFunction, isObject, isString, isDef, isNil, isEmpty } from '/@/utils/is';
isArray,
isFunction,
isObject,
isString,
isDef,
isNullOrUnDef,
isEmpty,
} from '/@/utils/is';
import { deepMerge } from '/@/utils'; import { deepMerge } from '/@/utils';
import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper';
import { dateUtil } from '/@/utils/dateUtil'; import { dateUtil } from '/@/utils/dateUtil';
@ -317,9 +309,9 @@ export function useFormEvents({
item.component != 'Divider' && item.component != 'Divider' &&
Reflect.has(item, 'field') && Reflect.has(item, 'field') &&
item.field && item.field &&
!isNullOrUnDef(item.defaultValue) && !isNil(item.defaultValue) &&
(!(item.field in currentFieldsValue) || (!(item.field in currentFieldsValue) ||
isNullOrUnDef(currentFieldsValue[item.field]) || isNil(currentFieldsValue[item.field]) ||
isEmpty(currentFieldsValue[item.field])) isEmpty(currentFieldsValue[item.field]))
) { ) {
obj[item.field] = item.defaultValue; obj[item.field] = item.defaultValue;

View File

@ -1,4 +1,4 @@
import { isArray, isFunction, isNotEmpty, isObject, isString, isNullOrUnDef } from '/@/utils/is'; import { isArray, isFunction, isEmpty, isObject, isString, isNil } from '/@/utils/is';
import { dateUtil } from '/@/utils/dateUtil'; import { dateUtil } from '/@/utils/dateUtil';
import { unref } from 'vue'; import { unref } from 'vue';
import type { Ref, ComputedRef } from 'vue'; import type { Ref, ComputedRef } from 'vue';
@ -115,10 +115,10 @@ export function useFormValues({
const [startTimeFormat, endTimeFormat] = Array.isArray(format) ? format : [format, format]; const [startTimeFormat, endTimeFormat] = Array.isArray(format) ? format : [format, format];
if (isNotEmpty(startTime)) { if (!isNil(startTime) && !isEmpty(startTime)) {
set(values, startTimeKey, formatTime(startTime, startTimeFormat)); set(values, startTimeKey, formatTime(startTime, startTimeFormat));
} }
if (isNotEmpty(endTime)) { if (!isNil(endTime) && !isEmpty(endTime)) {
set(values, endTimeKey, formatTime(endTime, endTimeFormat)); set(values, endTimeKey, formatTime(endTime, endTimeFormat));
} }
unset(values, field); unset(values, field);
@ -150,7 +150,7 @@ export function useFormValues({
} }
}); });
} }
if (!isNullOrUnDef(defaultValue)) { if (!isNil(defaultValue)) {
obj[item.field] = defaultValue; obj[item.field] = defaultValue;
if (formModel[item.field] === undefined) { if (formModel[item.field] === undefined) {

View File

@ -29,7 +29,7 @@
import { propTypes } from '/@/utils/propTypes'; import { propTypes } from '/@/utils/propTypes';
import { REDIRECT_NAME } from '/@/router/constant'; import { REDIRECT_NAME } from '/@/router/constant';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import { isFunction, isUrl } from '/@/utils/is'; import { isFunction, isHttpUrl } from '/@/utils/is';
import { openWindow } from '/@/utils'; import { openWindow } from '/@/utils';
import { useOpenKeys } from './useOpenKeys'; import { useOpenKeys } from './useOpenKeys';
@ -129,7 +129,7 @@
} }
async function handleSelect(key: string) { async function handleSelect(key: string) {
if (isUrl(key)) { if (isHttpUrl(key)) {
openWindow(key); openWindow(key);
return; return;
} }

View File

@ -122,7 +122,7 @@
import { useTableContext } from '../../hooks/useTableContext'; import { useTableContext } from '../../hooks/useTableContext';
import { useDesign } from '/@/hooks/web/useDesign'; import { useDesign } from '/@/hooks/web/useDesign';
// import { useSortable } from '/@/hooks/web/useSortable'; // import { useSortable } from '/@/hooks/web/useSortable';
import { isFunction, isNullAndUnDef } from '/@/utils/is'; import { isFunction, isNil } from '/@/utils/is';
import { getPopupContainer as getParentContainer } from '/@/utils'; import { getPopupContainer as getParentContainer } from '/@/utils';
import { cloneDeep, omit } from 'lodash-es'; import { cloneDeep, omit } from 'lodash-es';
import Sortablejs from 'sortablejs'; import Sortablejs from 'sortablejs';
@ -325,7 +325,7 @@
handle: '.table-column-drag-icon ', handle: '.table-column-drag-icon ',
onEnd: (evt) => { onEnd: (evt) => {
const { oldIndex, newIndex } = evt; const { oldIndex, newIndex } = evt;
if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) { if (isNil(oldIndex) || isNil(newIndex) || oldIndex === newIndex) {
return; return;
} }
// Sort column // Sort column

View File

@ -15,7 +15,7 @@
import { useSplitMenu } from './useLayoutMenu'; import { useSplitMenu } from './useLayoutMenu';
import { openWindow } from '/@/utils'; import { openWindow } from '/@/utils';
import { propTypes } from '/@/utils/propTypes'; import { propTypes } from '/@/utils/propTypes';
import { isUrl } from '/@/utils/is'; import { isHttpUrl } from '/@/utils/is';
import { useRootSetting } from '/@/hooks/setting/useRootSetting'; import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { useAppInject } from '/@/hooks/web/useAppInject'; import { useAppInject } from '/@/hooks/web/useAppInject';
import { useDesign } from '/@/hooks/web/useDesign'; import { useDesign } from '/@/hooks/web/useDesign';
@ -119,7 +119,7 @@
* @param menu * @param menu
*/ */
async function beforeMenuClickFn(path: string) { async function beforeMenuClickFn(path: string) {
if (!isUrl(path)) { if (!isHttpUrl(path)) {
return true; return true;
} }
openWindow(path); openWindow(path);

View File

@ -3,7 +3,7 @@ import type { RouteLocationNormalized } from 'vue-router';
import { useDesign } from '/@/hooks/web/useDesign'; import { useDesign } from '/@/hooks/web/useDesign';
import { useSortable } from '/@/hooks/web/useSortable'; import { useSortable } from '/@/hooks/web/useSortable';
import { useMultipleTabStore } from '/@/store/modules/multipleTab'; import { useMultipleTabStore } from '/@/store/modules/multipleTab';
import { isNullAndUnDef } from '/@/utils/is'; import { isNil } from '/@/utils/is';
import projectSetting from '/@/settings/projectSetting'; import projectSetting from '/@/settings/projectSetting';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import { useI18n } from '/@/hooks/web/useI18n'; import { useI18n } from '/@/hooks/web/useI18n';
@ -71,7 +71,7 @@ export function useTabsDrag(affixTextList: string[]) {
onEnd: (evt) => { onEnd: (evt) => {
const { oldIndex, newIndex } = evt; const { oldIndex, newIndex } = evt;
if (isNullAndUnDef(oldIndex) || isNullAndUnDef(newIndex) || oldIndex === newIndex) { if (isNil(oldIndex) || isNil(newIndex) || oldIndex === newIndex) {
return; return;
} }

View File

@ -2,7 +2,7 @@ import { AppRouteModule } from '/@/router/types';
import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types'; import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
import { findPath, treeMap } from '/@/utils/helper/treeHelper'; import { findPath, treeMap } from '/@/utils/helper/treeHelper';
import { cloneDeep } from 'lodash-es'; import { cloneDeep } from 'lodash-es';
import { isUrl } from '/@/utils/is'; import { isHttpUrl } from '/@/utils/is';
import { RouteParams } from 'vue-router'; import { RouteParams } from 'vue-router';
import { toRaw } from 'vue'; import { toRaw } from 'vue';
@ -20,7 +20,7 @@ function joinParentPath(menus: Menu[], parentPath = '') {
// 请注意,以 / 开头的嵌套路径将被视为根路径。 // 请注意,以 / 开头的嵌套路径将被视为根路径。
// This allows you to leverage the component nesting without having to use a nested URL. // This allows you to leverage the component nesting without having to use a nested URL.
// 这允许你利用组件嵌套,而无需使用嵌套 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 // path doesn't start with /, nor is it a url, join parent path
// 路径不以 / 开头,也不是 url加入父路径 // 路径不以 / 开头,也不是 url加入父路径
menu.path = `${parentPath}/${menu.path}`; menu.path = `${parentPath}/${menu.path}`;

View File

@ -5,7 +5,7 @@ import { useAppStoreWithOut } from '/@/store/modules/app';
import { usePermissionStore } from '/@/store/modules/permission'; import { usePermissionStore } from '/@/store/modules/permission';
import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper'; import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper';
import { filter } from '/@/utils/helper/treeHelper'; import { filter } from '/@/utils/helper/treeHelper';
import { isUrl } from '/@/utils/is'; import { isHttpUrl } from '/@/utils/is';
import { router } from '/@/router'; import { router } from '/@/router';
import { PermissionModeEnum } from '/@/enums/appEnum'; import { PermissionModeEnum } from '/@/enums/appEnum';
import { pathToRegexp } from 'path-to-regexp'; import { pathToRegexp } from 'path-to-regexp';
@ -115,7 +115,7 @@ export async function getChildrenMenus(parentPath: string) {
function basicFilter(routes: RouteRecordNormalized[]) { function basicFilter(routes: RouteRecordNormalized[]) {
return (menu: Menu) => { return (menu: Menu) => {
const matchRoute = routes.find((route) => { const matchRoute = routes.find((route) => {
if (isUrl(menu.path)) return true; if (isHttpUrl(menu.path)) return true;
if (route.meta?.carryParam) { if (route.meta?.carryParam) {
return pathToRegexp(route.path).test(menu.path); return pathToRegexp(route.path).test(menu.path);

View File

@ -27,7 +27,7 @@ export class Memory<T = any, V = any> {
// get<K extends keyof T>(key: K) { // get<K extends keyof T>(key: K) {
// const item = this.getItem(key); // const item = this.getItem(key);
// const time = item?.time; // const time = item?.time;
// if (!isNullOrUnDef(time) && time < new Date().getTime()) { // if (!isNil(time) && time < new Date().getTime()) {
// this.remove(key); // this.remove(key);
// } // }
// return item?.value ?? undefined; // return item?.value ?? undefined;

View File

@ -1,5 +1,5 @@
import { cacheCipher } from '/@/settings/encryptionSetting'; import { cacheCipher } from '/@/settings/encryptionSetting';
import { isNullOrUnDef } from '/@/utils/is'; import { isNil } from '/@/utils/is';
import { Encryption, EncryptionFactory, EncryptionParams } from '@/utils/cipher'; import { Encryption, EncryptionFactory, EncryptionParams } from '@/utils/cipher';
export interface CreateStorageParams extends EncryptionParams { export interface CreateStorageParams extends EncryptionParams {
@ -62,7 +62,7 @@ export const createStorage = ({
const stringData = JSON.stringify({ const stringData = JSON.stringify({
value, value,
time: Date.now(), 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; const stringifyValue = this.hasEncrypt ? this.encryption.encrypt(stringData) : stringData;
this.storage.setItem(this.getKey(key), stringifyValue); this.storage.setItem(this.getKey(key), stringifyValue);
@ -82,7 +82,7 @@ export const createStorage = ({
const decVal = this.hasEncrypt ? this.encryption.decrypt(val) : val; const decVal = this.hasEncrypt ? this.encryption.decrypt(val) : val;
const data = JSON.parse(decVal); const data = JSON.parse(decVal);
const { value, expire } = data; const { value, expire } = data;
if (isNullOrUnDef(expire) || expire >= new Date().getTime()) { if (isNil(expire) || expire >= new Date().getTime()) {
return value; return value;
} }
this.remove(key); this.remove(key);

View File

@ -10,7 +10,7 @@ import { checkStatus } from './checkStatus';
import { useGlobSetting } from '/@/hooks/setting'; import { useGlobSetting } from '/@/hooks/setting';
import { useMessage } from '/@/hooks/web/useMessage'; import { useMessage } from '/@/hooks/web/useMessage';
import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum'; import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum';
import { isString, isUnDef, isNull, isEmpty } from '/@/utils/is'; import { isString, isUndefined, isNull, isEmpty } from '/@/utils/is';
import { getToken } from '/@/utils/auth'; import { getToken } from '/@/utils/auth';
import { setObjToUrlParams, deepMerge } from '/@/utils'; import { setObjToUrlParams, deepMerge } from '/@/utils';
import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog';
@ -58,7 +58,7 @@ const transform: AxiosTransform = {
if (hasSuccess) { if (hasSuccess) {
let successMsg = message; let successMsg = message;
if (isNull(successMsg) || isUnDef(successMsg) || isEmpty(successMsg)) { if (isNull(successMsg) || isUndefined(successMsg) || isEmpty(successMsg)) {
successMsg = t(`sys.api.operationSuccess`); successMsg = t(`sys.api.operationSuccess`);
} }

View File

@ -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; const toString = Object.prototype.toString;
export function is(val: unknown, type: string) { export function is(val: unknown, type: string) {
@ -9,80 +43,12 @@ export function isDef<T = unknown>(val?: T): val is T {
return typeof val !== 'undefined'; return typeof val !== 'undefined';
} }
export function isUnDef<T = unknown>(val?: T): val is T { // TODO 此处 isObject 存在歧义
return !isDef(val);
}
export function isObject(val: any): val is Record<any, any> { export function isObject(val: any): val is Record<any, any> {
return val !== null && is(val, 'Object'); return val !== null && is(val, 'Object');
} }
export function isNotEmpty(val: any): boolean { // TODO 此处 isArray 存在歧义
return !isNil(val) && !isEmpty(val);
}
export function isEmpty<T = unknown>(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<T = any>(val: unknown): val is Promise<T> {
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<any> { export function isArray(val: any): val is Array<any> {
return val && Array.isArray(val); return val && Array.isArray(val);
} }
@ -91,19 +57,11 @@ export function isWindow(val: any): val is Window {
return typeof window !== 'undefined' && is(val, '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<any, any> {
return is(val, 'Map');
}
export const isServer = typeof window === 'undefined'; export const isServer = typeof window === 'undefined';
export const isClient = !isServer; export const isClient = !isServer;
export function isUrl(path: string): boolean { export function isHttpUrl(path: string): boolean {
const reg = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/; const reg = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/;
return reg.test(path); return reg.test(path);
} }