常用工具函数
General
isEmpty
export function isEmpty(data: any) {
if (data === '' || data === null || data === undefined) {
return true; } // [] {} 0 false/true else {
const typeofs = Object.prototype.toString.call(data); // 数组 if (typeofs === '[object Array]') {
if (data.length > 0) {
return false; } else {
return true; } } // 对象 else if (typeofs === '[object Object]') {
if (Object.keys(data).length > 0) {
return false; } else {
return true; } } else {
// 不为空 return false; } } }
讯享网
getType
讯享网type Types = | 'Number' | 'String' | 'Boolean' | 'Null' | 'Undefined' | 'Array' | 'Object'; export const getType = function (value: any): Types {
const typeofs = Object.prototype.toString.call(value); switch (typeofs) {
case '[object Number]': return 'Number'; // NaN Infinity 也是Number类型 case '[object String]': return 'String'; case '[object Boolean]': return 'Boolean'; case '[object Null]': return 'Null'; case '[object Undefined]': return 'Undefined'; case '[object Array]': return 'Array'; case '[object Object]': return 'Object'; default: return typeofs as any; } };
is
import {
getType } from './get-type'; const typeCheck = function (type: string, val: any): boolean {
if (getType(val) === type) {
return true; } else {
return false; } }; type TypeChecking = {
number: (val: any) => boolean; string: (val: any) => boolean; array: (val: any) => boolean; object: (val: any) => boolean; }; export const is: TypeChecking = {
number: (val: any) => {
return Number.isFinite(val); }, string: (val: any) => {
return typeCheck('String', val); }, array: (val: any) => {
return typeCheck('Array', val); }, object: (val: any) => {
return typeCheck('Object', val); }, };
isEqual
isEqual:判断两个数据是否相等
isObjectEqual:判断两个对象是否相等
isArrayEqual:判断两个数组是否相等
讯享网import {
isEmpty } from './isEmpty'; type IndexKey = string | number | symbol; interface AnyObject {
[propName: IndexKey]: any; } / * @description 判断两个对象是否相等 * @param {AnyObject} obj1 * @param {AnyObject} obj2 * @param {boolean} omitEmpty 默认为false,当为true时,为空的值判断为相等 * @return {boolean} */ export const isObjectEqual = function ( obj1: AnyObject, obj2: AnyObject, omitEmpty = false ) {
const keys1 = Object.keys(obj1); const keys2 = Object.keys(obj2); if (keys1.length === keys2.length) {
const sortKeys1 = keys1.sort(); const sortKeys2 = keys2.sort(); const sortStr1 = JSON.stringify(sortKeys1); const sortStr2 = JSON.stringify(sortKeys2); // 对象的key值都相等 if (sortStr1 === sortStr2) {
let flag = true; for (const key1 in obj1) {
const value1 = obj1[key1]; const value2 = obj2[key1]; const typeofs = Object.prototype.toString.call(value1); const typeofs2 = Object.prototype.toString.call(value2); // 如果两个都为空,判断为相等 if (omitEmpty && isEmpty(value1) && isEmpty(value2)) {
// flag = true; } else {
if (typeofs === '[object Object]' && typeofs2 === '[object Object]') {
const _flag = isObjectEqual(value1, value2, omitEmpty); if (!_flag) {
flag = false; break; } } else if ( typeofs === '[object Array]' && typeofs2 === '[object Array]' ) {
const _flag = isArrayEqual(value1, value2, omitEmpty); if (!_flag) {
flag = false; break; } } else if (typeofs === typeofs2) {
// string number boolean null undefined if (value1 !== value2) {
flag = false; break; } } else {
// 类型不同,不相等 flag = false; break; } } } return flag; } else {
return false; } } else {
return false; } }; / * @description 判断两个数组是否相等 * @param {any[]} arr1 * @param {any[]} arr2 * @param {boolean} omitEmpty 默认为false,当为true时,为空的值判断为相等 * @return {boolean} */ export const isArrayEqual = function ( arr1: any[], arr2: any[], omitEmpty = false ) {
if (arr1.length === arr2.length) {
let flag = true; let i = 0; for (const item of arr1) {
const value1 = item; const value2 = arr2[i]; const typeofs = Object.prototype.toString.call(value1); const typeofs2 = Object.prototype.toString.call(value2); // 如果两个都为空,判断为相等 if (omitEmpty && isEmpty(value1) && isEmpty(value2)) {
// flag = true; } else {
if (typeofs === '[object Object]' && typeofs2 === '[object Object]') {
const _flag = isObjectEqual(value1, value2, omitEmpty); if (!_flag) {
flag = false; break; } } else if ( typeofs === '[object Array]' && typeofs2 === '[object Array]' ) {
const _flag = isArrayEqual(value1, value2, omitEmpty); if (!_flag) {
flag = false; break; } } else if (typeofs === typeofs2) {
// string number boolean null undefined if (value1 !== value2) {
flag = false; break; } } else {
// 类型不同,不相等 flag = false; break; } } i++; } return flag; } else {
return false; } }; / * @description 判断两个数据是否相等 * @param {any} data1 * @param {any} data2 * @param {boolean} omitEmpty 默认为false,当为true时,为空的值判断为相等 * @return {boolean} */ export const isEqual = function (data1: any, data2: any, omitEmpty = false) {
let flag = true; // 如果两个都为空,判断为相等 if (omitEmpty && isEmpty(data1) && isEmpty(data2)) {
// flag = true; } else {
const typeofs = Object.prototype.toString.call(data1); const typeofs2 = Object.prototype.toString.call(data2); if (typeofs === '[object Object]' && typeofs2 === '[object Object]') {
const _flag = isObjectEqual(data1, data2, omitEmpty); if (!_flag) {
flag = false; } } else if (typeofs === '[object Array]' && typeofs2 === '[object Array]') {
const _flag = isArrayEqual(data1, data2, omitEmpty); if (!_flag) {
flag = false; } } else if (typeofs === typeofs2) {
// string number boolean null undefined if (data1 !== data2) {
flag = false; } } else {
// 类型不同,不相等 flag = false; } } return flag; };
isBasicData
/ * @description 是否是基本数据类型 * @param {any} data * @return {boolean} */ export function isBasicData(data: any) {
const basicDataTypes = [ '[object String]', '[object Number]', '[object Boolean]', '[object Null]', '[object Undefined]', ]; const typeofs = Object.prototype.toString.call(data); return basicDataTypes.includes(typeofs); }
getEnumMap
讯享网export function getEnumMap(list: any[], key = 'name') {
const typeofs = Object.prototype.toString.call(list); const isArray = typeofs === '[object Array]'; const newObj: any = {
}; if (isArray && list.length > 0) {
for (const item of list) {
newObj[item[key]] = item; } } return newObj; }
getRandomInt
返回了一个在指定值之间的随机整数
/ * 返回了一个在指定值之间的随机整数 * @param min 这个值不小于 min (如果 min 不是整数,则不小于 min 的向上取整数) * @param max 这个值小于(不等于)max * @returns [min ,max ) */ export const getRandomInt = function (min: number, max: number) {
min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; // 不含最大值,含最小值 };
getAngle
讯享网/ * 计算从x1y1到x2y2的直线,与水平线形成的夹角 * 计算规则为顺时针从左侧0°到与该直线形成的夹角 * @param {Object} x1 * @param {Object} y1 * @param {Object} x2 * @param {Object} y2 * @param {Boolean} toAngle 是否转换为角度值,默认false */ export const getAngle = function ( x1: number, y1: number, x2: number, y2: number, toAngle = false ) {
const x = x1 - x2; const y = y1 - y2; if (!x && !y) {
return 0; } // 弧度 radian = 角度 * Math.PI / 180 // 角度 angle = 弧度 * 180 / Math.PI let res; // 角度 const angle = (180 + (Math.atan2(-y, -x) * 180) / Math.PI + 360) % 360; res = 360 - angle; if (!toAngle) {
res = (res * Math.PI) / 180; } return res; };
omitEmpty
去除对象中为空的属性值 返回一个新对象
import {
isEmpty } from './isEmpty'; type IndexKey = string | number | symbol; interface AnyObject {
[propName: IndexKey]: any; } / * @description 去除对象中为空的属性值 返回一个新对象 * @param {AnyObject} data * @return 返回一个新对象 */ export const omitEmpty = function (data: AnyObject) {
const typeofs = Object.prototype.toString.call(data); if (typeofs === '[object Object]') {
const obj: AnyObject = {
}; for (const key in data) {
if (!isEmpty(data[key])) {
obj[key] = data[key]; } } return obj; } else {
console.error(`Expect type [object Object], but get type ${
typeofs}`); return data; } };
Custom
useWhere
讯享网export function useWhere( columns: {
wustOper: string; wustField: string }[] = [], value: any = {
} ) {
const newObj: any = {
}; for (const key in value) {
let newValue = value[key]; if (!isEmpty(newValue)) {
const ta = columns.find((item) => item.wustField == key); if (ta) {
const taOper = ta.wustOper; const newKey = key + '@' + taOper; if (taOper == 'like' || taOper == 'ilike') {
newValue = '%' + <string>newValue + '%'; } newObj[newKey] = newValue; } } } return newObj; }
stringToArr(字符串转数组)
export const isArrEmpty = function (data: any) {
if (isEmpty(data)) {
return true; } else {
if (data == 'None' || data == '[]' || data === '{}') {
return true; } else {
return false; } } }; // 字符串转数组 export const stringToArr = function (data: any) {
let res = []; if (!isArrEmpty(data)) {
const typeofs = Object.prototype.toString.call(data); if (typeofs === '[object String]') {
const start = data.slice(0, 1); const end = data.slice(-1); if ((start === '{' && end === '}') || (start === '[' && end === ']')) {
res = JSON.parse(data); } else {
console.warn('字符串' + '【' + data + '】' + '不能解析为数组'); res.push(data); } } else {
console.error('【' + data + '】' + '的数据类型不是字符串'); } } return res; };
arrToString(数组转字符串)
讯享网// 数组转字符串 export const arrToString = function (data: any) {
let res = ''; const typeofs = Object.prototype.toString.call(data); if (typeofs === '[object Array]') {
if (!isEmpty(data)) {
res = JSON.stringify(data); } } else {
console.error('数据类型【' + typeofs + '】错误'); } return res; };
未完待续…

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/90139.html