/**
* 转换为千分位
* @param value
* @returns {string}
*/
export function toThousandths(value) {
return (value+ '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g,'$1,');
}
/**
* 百分比保留两位小数
* @param value
* @returns {string}
*/
export function fmtPercentage(value) {
if (!value) {
return '-'
}
return (value * 100).toFixed(2) + '%'
}
/**
* 日期转化为中文
* @param value
* @returns {string}
*/
export function fmtText(value) {
if (!value) {
return '-'
}
return moment(value).calendar(null, {
sameDay: '[今天]',
nextDay: '[明天]',
lastDay: '[昨天]',
sameElse: 'YYYY/MM/DD | dddd',//其他时间格式化成具体的时间
//sameElse: 'YYYY/MM/DD'
});
}
/**
* 复制内容
* @param {string} txt 要复制的内容
*/
export function copyText(txt) {
const span = document.createElement('span')
span.innerText = txt
span.style.width = '0px'
span.style.height = '0px'
span.style.position = 'fixed'
document.body.append(span)
const range = document.createRange()
const selection = window.getSelection()
if (selection.rangeCount > 0) {
selection.removeAllRanges()
}
range.selectNode(span)
selection.addRange(range)
document.execCommand('Copy')
span.remove()
notice.success({message: '复制成功'})
}
/**
* 判断两个数组内容是否一样
* @param a
* @param b
* @return {boolean}
*/
export function ArrayEqual(a = [], b = []) {
if (typeof a !== typeof b) {
return false
}
if (a.length !== b.length) {
return false
}
const sortA = merge([], a).sort()
const sortB = merge([], b).sort()
for (let i = 0; i < sortA.length; i++) {
const av = sortA[i]
const bv = sortB[i]
if (av instanceof Array) {
if(!ArrayEqual(av, bv)) {
return false
}
} else if (av instanceof Object && !(av instanceof observableExample.constructor)) {
if (!ObjectEqual(av, bv)) {
return false
}
} else {
if (av !== bv) {
return false
}
}
}
return true
}
/**
* 判断两个对象内容是否一样
* @param a
* @param b
* @return {boolean}
*/
export function ObjectEqual(a = {}, b = {}) {
if (typeof a !== typeof b) {
return false
}
const aProps = Object.getOwnPropertyNames(a);
const bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) {
return false;
}
for (let i = 0; i < aProps.length; i++) {
const propName = aProps[i]
const propA = a[propName]
const propB = b[propName]
if ((typeof (propA) === 'object') && !(propA instanceof observableExample.constructor)) {
if (!ObjectEqual(propA, propB)) {
return false
}
} else {
if (propA !== propB) {
return false
}
}
}
return true
}
vue常用函数
最新推荐文章于 2024-04-16 10:56:46 发布