Files
cut-abstractions/samples/handleAbility/common/base/StringFormat.ts

127 lines
3.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export class StringFormat
{
/**
* 对Date的扩展将 Date 转化为指定格式的String
* 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
* 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
* eg:
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
*/
static Date(date: Date, fmt): string
{
let o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 小时
'H+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
'S': date.getMilliseconds(), // 毫秒
}
let week = {
0: '/u65e5',
1: '/u4e00',
2: '/u4e8c',
3: '/u4e09',
4: '/u56db',
5: '/u4e94',
6: '/u516d',
}
if (/(y+)/.test(fmt))
{
fmt = fmt.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length))
}
if (/(E+)/.test(fmt))
{
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '/u661f/u671f' : '/u5468') : '') + week[`${date.getDay()}`])
}
for (let k in o)
{
if (new RegExp(`(${k})`).test(fmt))
{
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)))
}
}
return fmt
}
/** 返回数字的固定小数点 123.00 */
static number(value: number, bit: number): string
{
return value?.toFixed(bit).toString()
}
static toFixed(value: number, bit: number = 3): number
{
let tt = 10 ** bit
return Math.round(value * tt) / tt
}
static filterIllegalChar(str: string): string
{
// var pattern = new RegExp("[/:*?'<>|\\]");
// var rs = "";
// for (var i = 0; i < str.length; i++)
// {
// rs = rs + str.substr(i, 1).replace(pattern, '');
// }
// return rs;
let rt = str.replace(/\\/g, '').replace(/\:/g, '').replace(/\*/g, '').replace(/\?/g, '').replace(/\"/g, '').replace(/\</g, '').replace(/\>/g, '').replace(/\|/g, '').replace(/\'/g, '')
return rt
}
/** 文本格式化 */
static format(str: string, ...args: any[]): string
{
let data = args
let tmpl = str
for (const item of tmpl.matchAll(/\{(.+?)\}/g))
{
let parts = item[1].split(',').map(i => i.trim())
let index = Number(parts[0])
let arg = data[index]
let val = (arg || '').toString() // 默认
if (arg instanceof Date) // 日期
{
let fm = 'MM-dd HH;mm'
if (parts.length > 1)
{
fm = parts[1]
}
val = this.Date(arg, fm)
}
if (parts.length > 1 && parts[1][0] === '#')
{
// {2,#3} -> 数字 转成3位 001,...023,
val = val.padStart(Number(parts[1].substring(1)), '0')
}
tmpl = tmpl.replace(item[0], val)
}
return tmpl
}
/** 实现右对齐,右边填充 (25,4,'*') => '25**' */
static PadEnd(num: number, totalWidth: number, paddingChar: string): string
{
let str = num.toString()
return str.padEnd(totalWidth, paddingChar[0])
}
/** 实现右对齐,左边填充 (25,4,'0') => '0025' */
static PadStart(num: number, totalWidth: number, paddingChar: string): string
{
let str = num.toString()
return str.padStart(totalWidth, paddingChar[0])
}
}