12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import utils from '@/common/utils';
- const { ztMoment } = utils;
- import iconMap from '../../config/btn-icon-config';
- function pluralize(time, label) {
- if (time === 1) {
- return time + label;
- }
- return time + label + 's';
- }
- export function timeAgo(time) {
- const between = Date.now() / 1000 - Number(time);
- if (between < 3600) {
- return pluralize(~~(between / 60), ' minute');
- } else if (between < 86400) {
- return pluralize(~~(between / 3600), ' hour');
- } else {
- return pluralize(~~(between / 86400), ' day');
- }
- }
- /* 数字 格式化*/
- export function numberFormatter(num, digits) {
- const si = [
- { value: 1e18, symbol: 'E' },
- { value: 1e15, symbol: 'P' },
- { value: 1e12, symbol: 'T' },
- { value: 1e9, symbol: 'G' },
- { value: 1e6, symbol: 'M' },
- { value: 1e3, symbol: 'k' }
- ];
- for (let i = 0; i < si.length; i++) {
- if (num >= si[i].value) {
- return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol;
- }
- }
- return num.toString();
- }
- export function toThousandFilter(num) {
- return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
- }
- export function dateFormatter(value, fmt) {
- if (value) {
- return ztMoment(value).format(fmt || 'YYYY-MM-DD');
- } else {
- return '';
- }
- }
- // 操作按钮图标
- export function optIcon(type, defaultIcon) {
- if (defaultIcon) {
- return defaultIcon;
- }
- let icon = '';
- for (const item of iconMap) {
- const texts = item[0].split(',').map(ele => ele.trim());
- if (texts.includes(type)) {
- icon = item[1];
- break;
- }
- }
- // const icon = iconMap.get(type);
- return icon ? icon : 'el-icon-edit';
- }
- const install = function(Vue) {
- let filters = { pluralize, timeAgo, numberFormatter, toThousandFilter, dateFormatter, optIcon };
- Object.keys(filters).forEach(key => {
- Vue.filter(key, filters[key]);
- });
- };
- /* istanbul ignore if */
- if (typeof window !== 'undefined' && window.Vue) {
- install(window.Vue);
- }
- export default {
- version: '1.0.0',
- install
- };
|