UNPKG

@mir_king/common

Version:

提供开发中常用的工具函数,提升开发效率的库

40 lines (39 loc) 1.35 kB
export const Validation = { // 判断数组中是否存在相同元素 hasDuplicates(array) { let nullarray = 0; // 检验数组中是否存在空值 array.forEach((item) => { if (item.length == 0) nullarray += 1; }); // console.log('相同元素 ==:', nullarray); return ( array.some((item, index, array) => { return array.indexOf(item) !== index; }) || nullarray >= 2 ); }, // 字符串查找indexOf string 完整字符串 field 查找的字符 indexOf(string, field) { if (!string && field && typeof string !== 'string') return false; //不存在或者不为字符串类型时 返回fasle if (string.indexOf(field) == -1) return false; return string.indexOf(field) + ''; }, //js判断数据为空的方法 isBlank(str) { if (Object.prototype.toString.call(str) === '[object Undefined]') { //空 return true; } else if ( Object.prototype.toString.call(str) === '[object String]' || Object.prototype.toString.call(str) === '[object Array]' ) { //字条串或数组 return str.length == 0 ? true : false; } else if (Object.prototype.toString.call(str) === '[object Object]') { return JSON.stringify(str) == '{}' ? true : false; } else { return true; } }, };