njzscloud-dispose-web/src/common/utils/objects.ts

98 lines
2.1 KiB
TypeScript

import { TypeTag } from '@/common/utils/index.ts'
import Types from '@/common/utils/types.ts'
/**
* 检查是否为普通对象
* @param obj 待检查对象
*/
export function isSimpleObject(obj: any) {
if (Object.prototype.toString.call(obj) !== TypeTag.OBJECT) {
return false
}
let firstProto = Object.getPrototypeOf(obj)
if (firstProto == null) {
return true
}
let proto = firstProto
let pt = null
while ((pt = Object.getPrototypeOf(proto)) != null) {
proto = pt
}
return firstProto === proto
}
/**
* 检查是否为类数组
* @param obj 待检查对象
*/
export function isArrayLike(obj: any) {
return obj != null && typeof obj !== 'function' && typeof obj.length === 'number' && obj.length > -1 && obj.length % 1 === 0 && obj.length <= Number.MAX_SAFE_INTEGER
}
/**
* 检查是否为纯数字字符串
* @param obj 待检查对象
*/
export function isNumStr(obj: any) {
return Types.isString(obj) && !isNaN(Number(obj))
}
type tags = 'error' | 'info' | 'warning' | 'risk' | 'error' | 'fatal' | 'success' | undefined;
export function switchStatus(status: string): tags {
switch (status) {
case 'YiJinChang':
return 'warning'
case 'YiYuYue':
return 'info'
case 'DaiPaiDan':
return 'warning'
case 'DaiJieDan':
return 'error'
case 'YiJieDan':
return 'info'
case 'QingYunZhong':
return 'info'
case 'YiZhiFu':
return 'info'
case 'YiChuChang':
return 'warning'
case 'YiTuiKuan':
return 'warning'
case 'YiWanCheng':
return 'success'
case 'YiQuXiao':
return undefined
default:
return undefined
}
}
export function auditTagColor(status: string): tags {
switch (status) {
case 'TongGuo':
return 'success'
case 'BoHui':
return 'warning'
case 'YiCheXiao':
return undefined
case 'DaiShenHe':
case 'QuDaiShenHe':
case 'ShiDaiShenHe':
return 'info'
default:
return undefined
}
}
export default {
isSimpleObject,
isArrayLike,
isNumStr,
switchStatus,
auditTagColor,
}