文件格式化
/**
* 文件大小格式化
* @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;
}
};
多多点赞,谢谢大家!