记录一下平时前端日常开发中使用到的一些工具函数
// 数组去重
export function dedupeArr(arr) {
return Array.from(new Set(arr))
}
// 生成随机uuid
export function getUuid() {
const s = []
const hexDigits = '0123456789abcdef'
for (let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '-'
const uuid = s.join('')
return uuid
}
/**
* list中获取value所对应的item的item[key](下拉框单选)
* @param {Array} list optionsList
* @param {*} value value值
* @param {*} key key值,默认为label
*/
export function checkValue(list, value, key = 'label', key1 = 'value') {
let keyValue = ''
list.forEach((item, index, self) => {
if (item[key1] === value) {
keyValue = item[key]
}
})
return keyValue
}
// 格式化百分数,精确到小数点后几位
export function formatPercent(numerator, denominator, digit = 2) {
const Percent = denominator ? ((numerator / denominator * 100).toFixed(digit)) : 0
return `${Percent}%`
}
/**
* 判断是否文本溢出
* @param e 事件对象
* @returns 返回true为未溢出 false溢出
*/
export function isBeyond(e) {
const ev = window.event || e // 浏览器兼容,最好写一下
const textRange = (el) => {
const textContent = el
const targetW = textContent.getBoundingClientRect().width
const range = document.createRange()
range.setStart(textContent, 0)
range.setEnd(textContent, textContent.childNodes.length)
const rangeWidth = range.getBoundingClientRect().width
return rangeWidth > targetW
}
return !textRange(ev.target) // target可以保证当前划过的dom节点一直保持是:el-tooltip__trigger
}
/************************************* 使用mathjs库运算浮点数 start********************************/
import { create, all } from 'mathjs'
const config = {
epsilon: 1e-12,
matrix: 'Matrix',
number: 'BigNumber', // 可选值:number BigNumber
precision: 64,
predictable: false,
randomSeed: null
}
export const math = create(all, config)
// 加
export const add = (num1, num2) => {
return Number(math.format(math.add(math.bignumber(num1), math.bignumber(num2))))
}
// 减
export const subtract = (num1, num2) => {
return Number(math.format(math.subtract(math.bignumber(num1), math.bignumber(num2))))
}
// 乘
export const multiply = (num1, num2) => {
return Number(math.format(math.multiply(math.bignumber(num1), math.bignumber(num2))))
}
// 除
export const divide = (num1, num2) => {
return Number(math.format(math.divide(math.bignumber(num1), math.bignumber(num2))))
}
/************************************* 使用mathjs库运算浮点数 end********************************/
/************************************* 使用mathjs库运算浮点数 start********************************/
import dayjs from 'dayjs'
/**
* 时间格式化
*/
export function formatDate(time, format = 'YYYY-MM-DD HH:mm:ss') {
if (!time) return undefined
return dayjs(time).format(format)
}
/**
* 获取开始时间 00:00:00
* format:year|month|week|date|day|hour|minute|second
*/
export function getStart(time, format = 'day') {
return formatDate(dayjs(time).startOf(format))
}
/**
* 获取结束时间 23:59:59
* format:year|month|week|date|day|hour|minute|second
*/
export function getEnd(time, format = 'day') {
return formatDate(dayjs(time).endOf(format))
}
/**
* 时间往前推一个月的时间
* @param {*} date
* @returns
*/
export function getLastMonth(date = Date.now(), format = 'YYYY-MM-DD') {
const currentDate = dayjs(date)
const lastMonth = currentDate.subtract(1, 'month')
return lastMonth.format(format)
}
/**
* 时间往前推一年的时间
* @param {*} date
* @returns
*/
export function getLastYear(date = Date.now(), format = 'YYYY-MM-DD') {
const currentDate = dayjs(date)
const lastYear = currentDate.subtract(1, 'year')
return lastYear.format(format)
}
/************************************* 使用mathjs库运算浮点数 end********************************/
// 字节转MB或GB
export function convertBytesToMBOrGB(bytes) {
const kbytes = bytes / 1024
const megabytes = bytes / (1024 * 1024)
const gigabytes = bytes / (1024 * 1024 * 1024)
if (gigabytes > 1) {
return `${gigabytes.toFixed(2)} GB`
}
if (megabytes > 1) {
return `${megabytes.toFixed(2)} MB`
}
return `${kbytes.toFixed(2)} KB`
}