146 lines
3.5 KiB
TypeScript
146 lines
3.5 KiB
TypeScript
import TypeTools, { TYPES } from './type-tools';
|
|
|
|
const FormatType = {
|
|
DATE: 'date',
|
|
TIME: 'time',
|
|
DATETIME: 'datetime',
|
|
QTY: 'qty',
|
|
AMT: 'amt',
|
|
}
|
|
|
|
const DATE_FORMAT = {
|
|
DEFAULT: 'yyyy-MM-dd HH:mm:ss',
|
|
FULL_DATE: 'yyyy-MM-dd HH:mm:ss.S',
|
|
DATE: 'yyyy-MM-dd',
|
|
YEAR_MONTH: 'yyyy-MM',
|
|
TIME: 'HH:mm:ss'
|
|
}
|
|
|
|
|
|
const DataFormat = {
|
|
/**
|
|
* 格式化日期
|
|
* @param date 日期
|
|
* @param formatString 格式化字符串
|
|
* @returns 格式化后的日期
|
|
*/
|
|
format: (data: any, options:FormatOption[]) => {
|
|
for (let i = 0; i < options.length; i++) {
|
|
const option = options[i];
|
|
data = option.format(data);
|
|
}
|
|
return data;
|
|
},
|
|
test: () => {
|
|
let data: any[] = [
|
|
{
|
|
id: 1,
|
|
name: 'John',
|
|
age: 30,
|
|
total: 100548931,
|
|
amt: 12121.256,
|
|
createDate: new Date()
|
|
},
|
|
{
|
|
id: 2,
|
|
name: 'John-1',
|
|
age: 30,
|
|
total: 8564355.78832,
|
|
amt: 12121.256,
|
|
createDate: "2025-06-26 10:25:06.125"
|
|
}
|
|
];
|
|
|
|
DataFormat.format(data, [
|
|
new FormatOption('total', FormatType.QTY,"###,###,###,###,###,###,###.00"),
|
|
new FormatOption('amt', FormatType.AMT, 2),
|
|
new FormatOption('createDate', FormatType.DATE, DATE_FORMAT.DATE)
|
|
]);
|
|
console.log(data);
|
|
},
|
|
};
|
|
|
|
class FormatOption {
|
|
key:string;
|
|
type: string;
|
|
formatString: string|number;
|
|
|
|
constructor(key:string,type: string, formatString: string|number) {
|
|
this.key = key;
|
|
this.type = type;
|
|
this.formatString = formatString;
|
|
}
|
|
|
|
static createDate(key:string,type: string, formatString: string){
|
|
return new FormatOption(key,type,formatString);
|
|
}
|
|
|
|
static createQty(key:string,decimal?:number){
|
|
return new FormatOption(key,FormatType.QTY,decimal!=undefined?decimal:3);
|
|
}
|
|
|
|
static createAmt(key:string){
|
|
return new FormatOption(key,FormatType.AMT,2);
|
|
}
|
|
|
|
format(data:any):any{
|
|
var keys = this.key.split(".");
|
|
let valueKey = keys[keys.length-1];
|
|
let valueData:any = undefined;
|
|
if(keys.length<=1){
|
|
valueData = data;
|
|
}else{
|
|
valueData = TypeTools.getNestedValue(data,keys.splice(0,keys.length-1));
|
|
}
|
|
if(valueData!=undefined && TypeTools.isArray(valueData)){
|
|
for (let i = 0; i < valueData.length; i++) {
|
|
let item = valueData[i];
|
|
let val = item[valueKey];
|
|
item[valueKey] = this.formatValue(val);
|
|
}
|
|
}else{
|
|
let val = valueData[valueKey];
|
|
valueData[valueKey] = this.formatValue(val);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
formatValue(value:any):any{
|
|
if(value==undefined || value==null) return value;
|
|
try {
|
|
if(this.type==FormatType.DATE || this.type==FormatType.TIME || this.type==FormatType.DATETIME){
|
|
let fmt = this.formatString as string;
|
|
return Date.valueOf(value).format(fmt);
|
|
}else if(this.type==FormatType.QTY || this.type==FormatType.AMT){
|
|
let numFmt = undefined;
|
|
if(Number.is(this.formatString)){
|
|
numFmt = this.createNumberFmt(this.formatString as number);
|
|
}else{
|
|
numFmt = this.formatString as string;
|
|
}
|
|
return Number.format(Number.parse(value),numFmt) ;
|
|
}
|
|
} catch (error) {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
createNumberFmt(num:number){
|
|
let numFmt = "###,###,###,###,###,###";
|
|
if(num>0){
|
|
numFmt += ".";
|
|
for (let i = 0; i < num; i++) {
|
|
numFmt += "0";
|
|
}
|
|
}
|
|
return numFmt;
|
|
}
|
|
}
|
|
|
|
export {DataFormat,FormatOption,DATE_FORMAT,FormatType};
|
|
|
|
|
|
|
|
|
|
|