定义全局过滤器

文件格式化

/**
 * 文件大小格式化
 * @param content 格式化内容
 * @returns {string}
 */
export const sizeFilter = (content) => {
    if (content === null || content === '') {
        return "0 Bytes";
    }
    // eslint-disable-next-line no-array-constructor
    let unitArr = new Array("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
    let index = 0;
    let srcsize = parseFloat(content);
    index = Math.floor(Math.log(srcsize) / Math.log(1024));
    let size = srcsize / Math.pow(1024, index);
    size = size.toFixed(2);//保留的小数位数
    return size + unitArr[index];
};

货币转换过滤器

/**
 * 货币格式化
 * @param content 格式化内容
 * @param places 保留小数位数
 * @param symbol 货币前的符号(比如$,¥)
 * @param thousand 每3位数间隔的标志
 * @param decimal 小数点位置的符号,默认为"."可更改
 * @returns {string}
 */
export const currencyFilter = (content, places, symbol, thousand, decimal) => {
    if (content === '' || content === null || typeof content === 'undefined') {
        return '-';
    } else {
        return (content === 0 || content === '0' || content === '0.00') ? '0.00' : Number(content).formatMoney(places, symbol, thousand, decimal);
    }
};

内容过滤器

/**
 * 内容过滤器
 * @param content
 * @returns {string|*}
 */
export const contentFilter = (content) => {
    if (content === '' || content === null || typeof content === 'undefined') {
        return '-';
    } else {
        return content;
    }
};

时间过滤器

import { validator } from 'klwk-ui';
/**
 * 时间过滤器
 * @param value 过滤值
 * @param format 时间格式
 * @param emptyVal 时间为空返回值
 * @returns {string|*}
 */
export const timeFormat = (value, format = 'yyyy/MM/dd', emptyVal = '-') => {
    if (!value) {
        return emptyVal;
    } else if (value instanceof Date) {
        return value.format(format);
    } else if (validator.isNumber(value)) {
        if(value.length == 10){
            return new Date(Number(parseInt(value) * 1000)).format(format);
        }else{
            return new Date(Number(value)).format(format);
        }
    } else if (typeof value === 'string') {
        value = value.replace(/-/g, '/');
        return value.toDate().format(format);
    } else {
        return value;
    }
};

多多点赞,谢谢大家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执着_ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值