@rk-init/filter v1.0.5
过滤器、格式化
// import parseTime, formatTime and set to filter /**
- Parse the time to string
- {{ '2018-09-14 01:05' | formaDate(yyyy-MM-dd hh:mm:ss) }}
- {{ '2018-09-14 01:05' | formaDate(yyyy-MM-dd) }}
- {{ '2018-09-14 01:05' | formaDate(MM/dd) }} 等 */ export const parseTime = (value, fmt = 'yyyy-MM-dd hh:mm:ss') => { const date = new Date(value); const o = { 'M+': date.getMonth() + 1, // 月份 'd+': date.getDate(), // 日 'h+': date.getHours(), // 小时 'm+': date.getMinutes(), // 分 's+': date.getSeconds(), // 秒 'w+': date.getDay(), // 星期 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度 S: date.getMilliseconds(), // 毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); // eslint-disable-next-line no-restricted-syntax for (const k in o) { if (k === 'w+') { if (ok === 0) { fmt = fmt.replace('w', '周日'); } else if (ok === 1) { fmt = fmt.replace('w', '周一'); } else if (ok === 2) { fmt = fmt.replace('w', '周二'); } else if (ok === 3) { fmt = fmt.replace('w', '周三'); } else if (ok === 4) { fmt = fmt.replace('w', '周四'); } else if (ok === 5) { fmt = fmt.replace('w', '周五'); } else if (ok === 6) { fmt = fmt.replace('w', '周六'); } } else if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (ok) : (('00' + ok).substr(('' + ok).length))); } } return fmt; };
/**
- @param {number} time
- @param {string} option
@returns {string} / export const formatTime = (time, option) => { if (('' + time).length === 10) { time = parseInt(time) 1000; } else { time = +time; } const d = new Date(time); const now = Date.now();
const diff = (now - d) / 1000;
if (diff < 30) { return '刚刚'; } if (diff < 3600) { // less 1 hour return Math.ceil(diff / 60) + '分钟前'; } if (diff < 3600 24) { return Math.ceil(diff / 3600) + '小时前'; } if (diff < 3600 24 * 2) { return '1天前'; } if (option) { return parseTime(time, option); } return ( d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分' ); };
/**
- 大数字格式化
- 10000 => 10k
- @param {number} num // 数字
- @param {number} digits // 小数点保留几位 / export const 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 >= sii.value) { return (num / sii.value).toFixed(digits).replace(/.0+$|(.0-91-9)0+$/, '$1') + sii.symbol; } } return num.toString(); };
/**
- 10000 => "10,000"
- @param {number} num */ export const toThousandFilter = (num) => (+num || 0).toString().replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
/**
- 首字母大写
- @param {String} string */ export const uppercaseFirst = (string) => string.charAt(0).toUpperCase() + string.slice(1);
/**
- 货币过滤器(返回带¥,保留两位小数,每三位加,号格式数据)
- @param {String / number} value */ export const currency = (value) => { if (value !== '0') { if (value === '' || value === undefined || value == null) { return '--'; } } return value.toLocaleString('zh', { style: 'currency', currency: 'CNY', }); };
/**
- 去除空格 type 1-所有空格 2-前后空格 3-前空格 4-后空格
- like 10000 => 10k
- @param {String} value
- @param {String} str / export const trim = (value, str) => { switch (str) { case 1: return value.replace(/\s+/g, ''); case 2: return value.replace(/(^\s)|(\s$)/g, ''); case 3: return value.replace(/(^\s)/g, ''); case 4: return value.replace(/(\s*$)/g, ''); default: return value.replace(/\s+/g, ''); } };
/**
- 字母大小写切换 type 1:首字母大写 2:首页母小写 3:大小写转换 4:全部大写 5:全部小写
- @param {String} str
- @param {String} type
- */ export const changeCase = (str, type) => { function ToggleCase(val) { let itemText = ''; val.split('').forEach( (item) => { if (/^(a-z+)/.test(item)) { itemText += item.toUpperCase(); } else if (/^(A-Z+)/.test(item)) { itemText += item.toLowerCase(); } else { itemText += item; } }, ); return itemText; } switch (type) { case 1: return str.replace(/\b\w+\b/g, (word) => word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase()); case 2: return str.replace(/\b\w+\b/g, (word) => word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase()); case 3: return ToggleCase(str); case 4: return str.toUpperCase(); case 5: return str.toLowerCase(); default: return str; } };
/**
- 字符串替换(比如将a13246里的3换成x)
- @param {String} str 源数据
- @param {String} AFindText 需要替换的字符串
- @param {String} ARepText 替换成什么
- */ export const replaceAll = (str, AFindText, ARepText) => { const raRegExp = new RegExp(AFindText, 'g'); return str.replace(raRegExp, ARepText); };
/**
- 手机号格式化
- @param {String} phone */ export const formatPhone = (phone) => { if (!phone) return ''; let str = phone; str = str.toString().replace(/^(\d{3})(\d{4})(\d{4})/g, '$1****$3'); return str; };
/**
- 身份证号格式化
- @param {String} value */ export const formatIDcard = (value) => { if (!value) return ''; let str = value; str = str.toString().replace(/^(.{6})(?:\w+)(.{4})$/, '$1****$2'); return str; };
/**
- 4位一空格(格式化银行卡)
- @param {String} val */ export const formatBank = (val) => { if (val) { return val .toString() .replace(/\s/g, '') .replace(/(.{4})/g, '$1 '); } return val; };
/**
- 现金额大写转换函数
- upDigit(168752632)
- result:"人民币壹亿陆仟捌佰柒拾伍万贰仟陆佰叁拾贰元整"
- upDigit(1682)
- result:"人民币壹仟陆佰捌拾贰元整"
- upDigit(-1693)
- result:"欠人民币壹仟陆佰玖拾叁元整"
- @param {Number} n
- / export const upDigit = (n) => { const fraction = '角', '分', '厘'; const digit = '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'; const unit = [ '元', '万', '亿', '', '拾', '佰', '仟', ]; const head = n < 0 ? '欠人民币' : '人民币'; n = Math.abs(n); let s = ''; for (let i = 0; i < fraction.length; i++) { // eslint-disable-next-line no-restricted-properties s += (digit[Math.floor(n 10 Math.pow(10, i)) % 10] + fractioni).replace(/零./, ''); } s = s || '整'; n = Math.floor(n); for (let i = 0; i < unit0.length && n > 0; i++) { let p = ''; for (let j = 0; j < unit1.length && n > 0; j++) { p = digitn % 10 + unit1 + p; n = Math.floor(n / 10); } s = p.replace(/(零.)零$/, '').replace(/^$/, '零') + unit0 + s; // s = p + unit0 + s; } return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整'); };
/**
- 保留几位小数
- @param {Number} val
- @param {Number} pos
- / export const formatFloat = (val, pos = 2) => { let f = parseFloat(val); // eslint-disable-next-line no-restricted-globals if (isNaN(f)) { return false; } // eslint-disable-next-line no-restricted-properties f = Math.round(val Math.pow(10, pos)) / Math.pow(10, pos); // pow 幂 let s = f.toString(); let rs = s.indexOf('.'); if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + pos) { s += '0'; } return s; };