mirror of
https://github.com/vbenjs/vue-vben-admin.git
synced 2025-01-24 10:33:50 +08:00
refactor: fix type check (#3246)
This commit is contained in:
parent
11cbe66937
commit
f91d777e4f
@ -2,15 +2,7 @@ import type { ComputedRef, Ref } from 'vue';
|
||||
import type { FormProps, FormSchemaInner as FormSchema, FormActionType } from '../types/form';
|
||||
import type { NamePath } from 'ant-design-vue/lib/form/interface';
|
||||
import { unref, toRaw, nextTick } from 'vue';
|
||||
import {
|
||||
isArray,
|
||||
isFunction,
|
||||
isObject,
|
||||
isString,
|
||||
isDef,
|
||||
isNullOrUnDef,
|
||||
isEmpty,
|
||||
} from '/@/utils/is';
|
||||
import { isArray, isFunction, isObject, isString, isDef, isNil, isEmpty } from '/@/utils/is';
|
||||
import { deepMerge } from '/@/utils';
|
||||
import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper';
|
||||
import { dateUtil } from '/@/utils/dateUtil';
|
||||
@ -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;
|
||||
|
@ -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 { unref } from 'vue';
|
||||
import type { Ref, ComputedRef } from 'vue';
|
||||
@ -115,10 +115,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(endTime) && !isEmpty(endTime)) {
|
||||
set(values, endTimeKey, formatTime(endTime, endTimeFormat));
|
||||
}
|
||||
unset(values, field);
|
||||
@ -150,7 +150,7 @@ export function useFormValues({
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!isNullOrUnDef(defaultValue)) {
|
||||
if (!isNil(defaultValue)) {
|
||||
obj[item.field] = defaultValue;
|
||||
|
||||
if (formModel[item.field] === undefined) {
|
||||
|
@ -29,7 +29,7 @@
|
||||
import { propTypes } from '/@/utils/propTypes';
|
||||
import { REDIRECT_NAME } from '/@/router/constant';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { isFunction, isUrl } from '/@/utils/is';
|
||||
import { isFunction, isHttpUrl } from '/@/utils/is';
|
||||
import { openWindow } from '/@/utils';
|
||||
|
||||
import { useOpenKeys } from './useOpenKeys';
|
||||
@ -129,7 +129,7 @@
|
||||
}
|
||||
|
||||
async function handleSelect(key: string) {
|
||||
if (isUrl(key)) {
|
||||
if (isHttpUrl(key)) {
|
||||
openWindow(key);
|
||||
return;
|
||||
}
|
||||
|
@ -122,7 +122,7 @@
|
||||
import { useTableContext } from '../../hooks/useTableContext';
|
||||
import { useDesign } from '/@/hooks/web/useDesign';
|
||||
// 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 { cloneDeep, omit } from 'lodash-es';
|
||||
import Sortablejs from 'sortablejs';
|
||||
@ -325,7 +325,7 @@
|
||||
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
|
||||
|
@ -15,7 +15,7 @@
|
||||
import { useSplitMenu } from './useLayoutMenu';
|
||||
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';
|
||||
@ -119,7 +119,7 @@
|
||||
* @param menu
|
||||
*/
|
||||
async function beforeMenuClickFn(path: string) {
|
||||
if (!isUrl(path)) {
|
||||
if (!isHttpUrl(path)) {
|
||||
return true;
|
||||
}
|
||||
openWindow(path);
|
||||
|
@ -3,7 +3,7 @@ import type { RouteLocationNormalized } from 'vue-router';
|
||||
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';
|
||||
import { useRouter } from 'vue-router';
|
||||
import { useI18n } from '/@/hooks/web/useI18n';
|
||||
@ -71,7 +71,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;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ import { AppRouteModule } from '/@/router/types';
|
||||
import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
|
||||
import { findPath, treeMap } from '/@/utils/helper/treeHelper';
|
||||
import { cloneDeep } from 'lodash-es';
|
||||
import { isUrl } from '/@/utils/is';
|
||||
import { isHttpUrl } from '/@/utils/is';
|
||||
import { RouteParams } from 'vue-router';
|
||||
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.
|
||||
// 这允许你利用组件嵌套,而无需使用嵌套 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}`;
|
||||
|
@ -5,7 +5,7 @@ import { useAppStoreWithOut } from '/@/store/modules/app';
|
||||
import { usePermissionStore } from '/@/store/modules/permission';
|
||||
import { transformMenuModule, getAllParentPath } 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';
|
||||
import { pathToRegexp } from 'path-to-regexp';
|
||||
@ -115,7 +115,7 @@ export async function getChildrenMenus(parentPath: string) {
|
||||
function basicFilter(routes: RouteRecordNormalized[]) {
|
||||
return (menu: Menu) => {
|
||||
const matchRoute = routes.find((route) => {
|
||||
if (isUrl(menu.path)) return true;
|
||||
if (isHttpUrl(menu.path)) return true;
|
||||
|
||||
if (route.meta?.carryParam) {
|
||||
return pathToRegexp(route.path).test(menu.path);
|
||||
|
2
src/utils/cache/memory.ts
vendored
2
src/utils/cache/memory.ts
vendored
@ -27,7 +27,7 @@ export class Memory<T = any, V = any> {
|
||||
// get<K extends keyof T>(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;
|
||||
|
6
src/utils/cache/storageCache.ts
vendored
6
src/utils/cache/storageCache.ts
vendored
@ -1,5 +1,5 @@
|
||||
import { cacheCipher } from '/@/settings/encryptionSetting';
|
||||
import { isNullOrUnDef } from '/@/utils/is';
|
||||
import { isNil } from '/@/utils/is';
|
||||
import { Encryption, EncryptionFactory, EncryptionParams } from '@/utils/cipher';
|
||||
|
||||
export interface CreateStorageParams extends EncryptionParams {
|
||||
@ -62,7 +62,7 @@ export const 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);
|
||||
@ -82,7 +82,7 @@ export const 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);
|
||||
|
@ -10,7 +10,7 @@ import { checkStatus } from './checkStatus';
|
||||
import { useGlobSetting } from '/@/hooks/setting';
|
||||
import { useMessage } from '/@/hooks/web/useMessage';
|
||||
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 { setObjToUrlParams, deepMerge } from '/@/utils';
|
||||
import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog';
|
||||
@ -58,7 +58,7 @@ const transform: AxiosTransform = {
|
||||
if (hasSuccess) {
|
||||
let successMsg = message;
|
||||
|
||||
if (isNull(successMsg) || isUnDef(successMsg) || isEmpty(successMsg)) {
|
||||
if (isNull(successMsg) || isUndefined(successMsg) || isEmpty(successMsg)) {
|
||||
successMsg = t(`sys.api.operationSuccess`);
|
||||
}
|
||||
|
||||
|
118
src/utils/is.ts
118
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;
|
||||
|
||||
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';
|
||||
}
|
||||
|
||||
export function isUnDef<T = unknown>(val?: T): val is T {
|
||||
return !isDef(val);
|
||||
}
|
||||
|
||||
// TODO 此处 isObject 存在歧义
|
||||
export function isObject(val: any): val is Record<any, any> {
|
||||
return val !== null && is(val, 'Object');
|
||||
}
|
||||
|
||||
export function isNotEmpty(val: any): boolean {
|
||||
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');
|
||||
}
|
||||
|
||||
// TODO 此处 isArray 存在歧义
|
||||
export function isArray(val: any): val is Array<any> {
|
||||
return val && Array.isArray(val);
|
||||
}
|
||||
@ -91,19 +57,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<any, any> {
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user