vue常用函数

/**
 * 转换为千分位
 * @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
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值