---添加数据转换工具
This commit is contained in:
109
src/utils/dataFormat.ts
Normal file
109
src/utils/dataFormat.ts
Normal file
@ -0,0 +1,109 @@
|
||||
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 格式化后的日期
|
||||
*/
|
||||
formatDate: (date: Date | string | number, fmt: string = 'yyyy-MM-dd HH:mm:ss') => {
|
||||
return Date.valueOf(date).format(fmt);
|
||||
},
|
||||
formatQty: (val: string | number, digits:number) => {
|
||||
return format(new Date(date), digits);
|
||||
},
|
||||
formatAmt: (val: string | number, formatString: number) => {
|
||||
return format(new Date(date), digits);
|
||||
},
|
||||
parseDate: (date: string | Date, formatString: string = 'yyyy-MM-dd HH:mm:ss') => {
|
||||
return new Date(date);
|
||||
},
|
||||
format:(data:any,option:any) => {
|
||||
},
|
||||
};
|
||||
|
||||
export 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;
|
||||
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.format(val);
|
||||
}
|
||||
}
|
||||
if(this.type==FormatType.DATE){
|
||||
|
||||
}else{
|
||||
|
||||
}
|
||||
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){
|
||||
return DataFormat.formatDate(value,this.formatString as string);
|
||||
}else if(this.type==FormatType.QTY){
|
||||
return DataFormat.formatQty(value,this.formatString as number);
|
||||
}else if(this.type==FormatType.AMT){
|
||||
return DataFormat.formatAmt(value,this.formatString as number);
|
||||
}
|
||||
} catch (error) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user