72 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
import checkTypes from "@/utils/checkTypes";
 | 
						||
export function guid() {
 | 
						||
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
 | 
						||
    const r = (Math.random() * 16) | 0
 | 
						||
    const v = c === 'x' ? r : (r & 0x3) | 0x8
 | 
						||
    return v.toString(16)
 | 
						||
  })
 | 
						||
}
 | 
						||
 | 
						||
export const getTheTruth = function (data) {
 | 
						||
  if (typeof data !== 'object' && typeof data !== 'string') return data
 | 
						||
  if (typeof data === 'string') {
 | 
						||
    if (data.startsWith('{') || data.startsWith('['))
 | 
						||
      return getTheTruth(JSON.parse(data))
 | 
						||
    return data
 | 
						||
  }
 | 
						||
  if (Array.isArray(data)) return data.map((item) => getTheTruth(item))
 | 
						||
  for (const key in data) data[key] = getTheTruth(data[key])
 | 
						||
  return data
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
export const isEmpty = (data) => {
 | 
						||
  return checkTypes.isUndefined(data) || checkTypes.isNull(data) || data == NaN
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
export function isDef(v) {
 | 
						||
  return v !== undefined && v !== null
 | 
						||
}
 | 
						||
 | 
						||
export function isPromise(val) {
 | 
						||
  return (
 | 
						||
    isDef(val) &&
 | 
						||
    typeof val.then === 'function' &&
 | 
						||
    typeof val.catch === 'function'
 | 
						||
  )
 | 
						||
}
 | 
						||
 | 
						||
export const toPromise = (task, data) => {
 | 
						||
  return new Promise((resolve, reject) => {
 | 
						||
    let _isPromise = false // 当前任务是不是一个promise
 | 
						||
    try {
 | 
						||
      // 执行任务
 | 
						||
      const results = task(data)
 | 
						||
      // 任务是一个异步函数,执行promise
 | 
						||
      if (isPromise(results)) {
 | 
						||
        _isPromise = true
 | 
						||
        results.then(resolve, reject)
 | 
						||
      } else {
 | 
						||
        // 非异步任务
 | 
						||
        resolve(results)
 | 
						||
      }
 | 
						||
    } catch (e) {
 | 
						||
      if (!_isPromise) {
 | 
						||
        reject(e)
 | 
						||
      }
 | 
						||
    }
 | 
						||
  })
 | 
						||
}
 | 
						||
 | 
						||
export function getSize(size, defaultValue = undefined) {
 | 
						||
  if (!size) {
 | 
						||
    if (checkTypes.isNumber(size)) return size
 | 
						||
    return defaultValue || undefined
 | 
						||
  }
 | 
						||
  if (/^\-?\d+$/.test(`${size}`)) return `${size}px`
 | 
						||
  if (/(%|px)$/.test(`${size}`)) return size
 | 
						||
  if (/calc/.test(`${size}`)) return size
 | 
						||
  return size
 | 
						||
}
 |