From dc9ceb7e328f628b1bd19c71a0206b03c8f5a4f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A6=8F=E8=B4=A2?= <1471584931@qq.com> Date: Mon, 2 Feb 2026 10:38:58 +0800 Subject: [PATCH] =?UTF-8?q?---=E6=95=B0=E6=8D=AE=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/dataFormat.ts | 77 +++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/src/utils/dataFormat.ts b/src/utils/dataFormat.ts index e21d906..6f9c3b1 100644 --- a/src/utils/dataFormat.ts +++ b/src/utils/dataFormat.ts @@ -24,12 +24,43 @@ const DataFormat = { * @param formatString 格式化字符串 * @returns 格式化后的日期 */ - formatDate: (date: Date | string | number, fmt: string = 'yyyy-MM-dd HH:mm:ss') => { - // return Date.valueOf(date).format(fmt); + 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); }, }; -export class FormatOption { +class FormatOption { key:string; type: string; formatString: string|number; @@ -45,7 +76,7 @@ export class FormatOption { } static createQty(key:string,decimal?:number){ - return new FormatOption(key,FormatType.QTY,decimal!=undefined?decimal:3); + return new FormatOption(key,FormatType.QTY,decimal!=undefined?decimal:3); } static createAmt(key:string){ @@ -55,7 +86,7 @@ export class FormatOption { format(data:any):any{ var keys = this.key.split("."); let valueKey = keys[keys.length-1]; - let valueData; + let valueData:any = undefined; if(keys.length<=1){ valueData = data; }else{ @@ -65,13 +96,11 @@ export class FormatOption { for (let i = 0; i < valueData.length; i++) { let item = valueData[i]; let val = item[valueKey]; - item[valueKey] = this.format(val); + item[valueKey] = this.formatValue(val); } - } - if(this.type==FormatType.DATE){ - }else{ - + let val = valueData[valueKey]; + valueData[valueKey] = this.formatValue(val); } return data; } @@ -80,18 +109,36 @@ export class FormatOption { 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); + 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}; +