123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- let commonUtil = {};
- /**
- * @method isType 判断对象类型
- * @param obj {Object} 需要判断的对象
- * @param type {String} 类型字符串
- * @returns {Boolean}
- * */
- commonUtil.isType = (obj, type) => {
- return Object.prototype.toString.call(obj) === '[object ' + type + ']';
- };
- /**
- * @method isArray 判断该对象是否是数组
- * @param obj {Object} 需要判断的对象
- * @returns {Boolean}
- * */
- commonUtil.isArray = (obj) => {
- return commonUtil.isType(obj, 'Array');
- };
- /**
- * @method isStr 判断该对象是否是字符串
- * @param obj {Object} 需要判断的对象
- * @returns {Boolean}
- * */
- commonUtil.isStr = (obj) => {
- return commonUtil.isType(obj, 'String');
- };
- /**
- * @method isObject 判断该对象是否是Object
- * @param obj {Object} 需要判断的对象
- * @returns {Boolean}
- * */
- commonUtil.isObject = (obj) => {
- return commonUtil.isType(obj, 'Object');
- };
- /**
- * @method isFun 判断该对象是否是函数
- * @param obj {Object} 需要判断的对象
- * @returns {Boolean}
- * */
- commonUtil.isFun = (obj) => {
- return commonUtil.isType(obj, 'Function');
- };
- /**
- * @method isJson 判断该对象是否是json
- * @param obj {Object} 需要判断的对象
- * @returns {Boolean}
- * */
- commonUtil.isNull = (obj) => {
- return commonUtil.isType(obj, 'Null');
- };
- /**
- * @method isJson 判断该对象是否是json
- * @param obj {Object} 需要判断的对象
- * @returns {Boolean}
- * */
- commonUtil.isJson = (obj) => {
- return commonUtil.isObject(obj) && !commonUtil.isNull(obj) && !commonUtil.isArray(obj);
- };
- /**
- * @method isJson 判断该对象是否是json
- * @param str {Object} 需要判断的对象
- * @returns {str | JSON.parse(str)}
- * */
- commonUtil.isJsonStr = (str) => {
- try {
- return JSON.parse(str);
- } catch (error) {
- return str;
- }
- };
- /**
- * 判断值是否为空
- * @param value 值
- * @returns {boolean}
- */
- commonUtil.isValueEmpty = (value) => {
- if (value === null || value === undefined) {
- return true;
- }
- if (typeof value === "string" && value.trim() === "") {
- return true;
- }
- if (Array.isArray(value) && value.length === 0) {
- return true;
- }
- if (
- typeof value === "object" &&
- !Array.isArray(value) &&
- Object.keys(value).length === 0
- ) {
- return true;
- }
- if (typeof value === "symbol" && value.toString() === "Symbol()") {
- return true;
- }
- return false;
- }
- /**
- * 获取表单数据中有值的数据
- * @param formData 表单数据
- * @returns {T}
- */
- commonUtil.getFromValue = (formData) => {
- const result = {};
- for (const key in formData) {
- if (!commonUtil.isValueEmpty(formData[key])) {
- result[key] = formData[key];
- }
- }
- return result;
- }
- export default commonUtil;
|