vue-utils__工具类函数
1.is 判断
const toString = Object.prototype.toString;
/**
* @description: 判断值是否为某个类型
*/
export function is(val, type) {
return toString.call(val) === `[object ${type}]`;
}
/**
* @description: 是否为函数
*/
export function isFunction(val) {
return is(val, "Function");
}
/**
* @description: 是否已定义
*/
export const isDef = (val) => {
return typeof val !== "undefined";
};
export const isUnDef = (val) => {
return !isDef(val);
};
/**
* @description: 是否为对象
*/
export const isObject = (val) => {
return val !== null && is(val, "Object");
};
/**
* @description: 是否为时间
*/
export function isDate(val) {
return is(val, "Date");
}
/**
* @description: 是否为数值
*/
export function isNumber(val) {
return is(val, "Number");
}
/**
* @description: 是否为AsyncFunction
*/
export function isAsyncFunction(val){
return is(val, "AsyncFunction");
}
/**
* @description: 是否为promise
*/
export function isPromise(val){
return is(val, "Promise") && isObject(val) && isFunction(val.then) && isFunction(val.catch);
}
/**
* @description: 是否为字符串
*/
export function isString(val){
return is(val, "String");
}
/**
* @description: 是否为boolean类型
*/
export function isBoolean(val) {
return is(val, "Boolean");
}
/**
* @description: 是否为数组
*/
export function isArray(val) {
return val && Array.isArray(val);
}
/**
* @description: 是否客户端
*/
export const isClient = () => {
return typeof window !== "undefined";
};
/**
* @description: 是否为浏览器
*/
export const isWindow = (val) => {
return typeof window !== "undefined" && is(val, "Window");
};
/**
* @description: 是否为Element
*/
export const isElement = (val) => {
return isObject(val) && !!val.tagName;
};
export const isServer = typeof window === "undefined";
// 是否为图片节点
export function isImageDom(o) {
return o && ["IMAGE", "IMG"].includes(o.tagName);
}
// 是否为空
export function isNull(val) {
return val === null;
}
// 是否为 undefined 并且为 null
export function isNullAndUnDef(val) {
return isUnDef(val) && isNull(val);
}
// 是否为 undefined 或者 null
export function isNullOrUnDef(val){
return isUnDef(val) || isNull(val);
}
2.utils
import { isArray } from "@/utils/is";
/**
* @description 获取localStorage
* @param {String} key Storage名称
* @return string
*/
export function localGet(key: string) {
const value = window.localStorage.getItem(key);
try {
return JSON.parse(window.localStorage.getItem(key) as string);
} catch (error) {
return value;
}
}
/**
* @description 存储localStorage
* @param {String} key Storage名称
* @param {Any} value Storage值
* @return void
*/
export function localSet(key: string, value: any) {
window.localStorage.setItem(key, JSON.stringify(value));
}
/**
* @description 清除localStorage
* @param {String} key Storage名称
* @return void
*/
export function localRemove(key: string) {
window.localStorage.removeItem(key);
}
/**
* @description 清除所有localStorage
* @return void
*/
export function localClear() {
window.localStorage.clear();
}
/**
* @description 对象数组深克隆
* @param {Object} obj 源对象
* @return object
*/
export function deepCopy<T>(obj: any): T {
let newObj: any;
try {
newObj = obj.push ? [] : {};
} catch (error) {
newObj = {};
}
for (let attr in obj) {
if (typeof obj[attr] === "object") {
newObj[attr] = deepCopy(obj[attr]);
} else {
newObj[attr] = obj[attr];
}
}
return newObj;
}
/**
* @description 判断数据类型
* @param {Any} val 需要判断类型的数据
* @return string
*/
export function isType(val: any) {
if (val === null) return "null";
if (typeof val !== "object") return typeof val;
else return Object.prototype.toString.call(val).slice(8, -1).toLocaleLowerCase();
}
/**
* @description 生成随机数
* @param {Number} min 最小值
* @param {Number} max 最大值
* @return number
*/
export function randomNum(min: number, max: number): number {
let num = Math.floor(Math.random() * (min - max) + max);
return num;
}
/**
* @description 获取当前时间
* @return string
*/
export function getTimeState() {
// 获取当前时间
let timeNow = new Date();
// 获取当前小时
let hours = timeNow.getHours();
// 判断当前时间段
if (hours >= 6 && hours <= 10) return `早上好 ⛅`;
if (hours >= 10 && hours <= 14) return `中午好
标签:__,function,vue,return,description,val,utils,param,export
From: https://www.cnblogs.com/miyagi-jiye/p/16791265.html