---添加数据转换工具
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,77 +1,113 @@
|
||||
const OBJ_TYPE = {
|
||||
OBJ: "[object Object]",
|
||||
NUMBER: "[object Number]",
|
||||
STRING: "[object String]",
|
||||
UNDEFINED: "[object Undefined]",
|
||||
NULL: "[object Null]",
|
||||
BOOLEAN: "[object Boolean]",
|
||||
ARRAY: "[object Array]",
|
||||
FUNCTION: "[object Function]",
|
||||
DATE: "[object Date]",
|
||||
REGEXP: "[object RegExp]",
|
||||
JSON: "[object JSON]",
|
||||
OBJ: "[object Object]",
|
||||
NUMBER: "[object Number]",
|
||||
STRING: "[object String]",
|
||||
UNDEFINED: "[object Undefined]",
|
||||
NULL: "[object Null]",
|
||||
BOOLEAN: "[object Boolean]",
|
||||
ARRAY: "[object Array]",
|
||||
FUNCTION: "[object Function]",
|
||||
DATE: "[object Date]",
|
||||
REGEXP: "[object RegExp]",
|
||||
JSON: "[object JSON]",
|
||||
}
|
||||
|
||||
const TYPES = {
|
||||
BOOLEAN: "BOOLEAN",
|
||||
NUMBER: "NUMBER",
|
||||
STRING: "STRING",
|
||||
DATE: "DATE",
|
||||
REGEXP: "REGEXP",
|
||||
NULL: "NULL",
|
||||
OBJ: "OBJ",
|
||||
ARRAY: "ARRAY",
|
||||
JSON: "JSON",
|
||||
FUNCTION: "FUNCTION",
|
||||
UNKNOWN: "UNKNOWN",
|
||||
BOOLEAN: "BOOLEAN",
|
||||
NUMBER: "NUMBER",
|
||||
STRING: "STRING",
|
||||
DATE: "DATE",
|
||||
REGEXP: "REGEXP",
|
||||
NULL: "NULL",
|
||||
OBJ: "OBJ",
|
||||
ARRAY: "ARRAY",
|
||||
JSON: "JSON",
|
||||
FUNCTION: "FUNCTION",
|
||||
UNKNOWN: "UNKNOWN",
|
||||
}
|
||||
|
||||
const TypeTools = {
|
||||
parseType: function (obj: any) {
|
||||
if (obj === null) return TYPES.NULL;
|
||||
if(this.isNull(obj)) return TYPES.NULL;
|
||||
if(this.isString(obj)) return TYPES.STRING;
|
||||
if(this.isNumber(obj)) return TYPES.NUMBER;
|
||||
if(this.isBoolean(obj)) return TYPES.BOOLEAN;
|
||||
if(this.isDate(obj)) return TYPES.DATE;
|
||||
if(this.isRegExp(obj)) return TYPES.REGEXP;
|
||||
if(this.isArray(obj)) return TYPES.ARRAY;
|
||||
if(this.isFunction(obj)) return TYPES.FUNCTION;
|
||||
if(this.isObject(obj)) return TYPES.OBJ;
|
||||
if(this.isJSON(obj)) return TYPES.JSON;
|
||||
return TYPES.UNKNOWN;
|
||||
},
|
||||
isString: function (obj: any) {
|
||||
return OBJ_TYPE.STRING === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isBoolean: function (obj: any) {
|
||||
return OBJ_TYPE.BOOLEAN === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isNumber: function (obj: any) {
|
||||
return OBJ_TYPE.NUMBER === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isDate: function (obj: any) {
|
||||
return OBJ_TYPE.DATE === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isRegExp: function (obj: any) {
|
||||
return OBJ_TYPE.REGEXP === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isNull: function (obj: any) {
|
||||
const type = Object.prototype.toString.call(obj);
|
||||
return OBJ_TYPE.NULL === type || OBJ_TYPE.UNDEFINED === type;
|
||||
},
|
||||
isJSON: function (obj: any) {
|
||||
return OBJ_TYPE.JSON === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isArray: function (obj: any) {
|
||||
return OBJ_TYPE.ARRAY === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isFunction: function (obj: any) {
|
||||
return OBJ_TYPE.FUNCTION === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isObject: function (obj: any) {
|
||||
return OBJ_TYPE.OBJ === Object.prototype.toString.call(obj);
|
||||
parseType: function (obj: any) {
|
||||
if (obj === null) return TYPES.NULL;
|
||||
if (this.isNull(obj)) return TYPES.NULL;
|
||||
if (this.isString(obj)) return TYPES.STRING;
|
||||
if (this.isNumber(obj)) return TYPES.NUMBER;
|
||||
if (this.isBoolean(obj)) return TYPES.BOOLEAN;
|
||||
if (this.isDate(obj)) return TYPES.DATE;
|
||||
if (this.isRegExp(obj)) return TYPES.REGEXP;
|
||||
if (this.isArray(obj)) return TYPES.ARRAY;
|
||||
if (this.isFunction(obj)) return TYPES.FUNCTION;
|
||||
if (this.isObject(obj)) return TYPES.OBJ;
|
||||
if (this.isJSON(obj)) return TYPES.JSON;
|
||||
return TYPES.UNKNOWN;
|
||||
},
|
||||
isString: function (obj: any) {
|
||||
return OBJ_TYPE.STRING === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isBoolean: function (obj: any) {
|
||||
return OBJ_TYPE.BOOLEAN === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isNumber: function (obj: any) {
|
||||
return OBJ_TYPE.NUMBER === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isDate: function (obj: any) {
|
||||
return OBJ_TYPE.DATE === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isRegExp: function (obj: any) {
|
||||
return OBJ_TYPE.REGEXP === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isNull: function (obj: any) {
|
||||
const type = Object.prototype.toString.call(obj);
|
||||
return OBJ_TYPE.NULL === type || OBJ_TYPE.UNDEFINED === type;
|
||||
},
|
||||
isJSON: function (obj: any) {
|
||||
return OBJ_TYPE.JSON === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isArray: function (obj: any) {
|
||||
return OBJ_TYPE.ARRAY === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isFunction: function (obj: any) {
|
||||
return OBJ_TYPE.FUNCTION === Object.prototype.toString.call(obj);
|
||||
},
|
||||
isObject: function (obj: any) {
|
||||
return OBJ_TYPE.OBJ === Object.prototype.toString.call(obj);
|
||||
},
|
||||
getNestedValue: function (obj: any, paths: string[]) {
|
||||
let i = 0;
|
||||
let o = obj;
|
||||
do {
|
||||
let key = paths[i];
|
||||
if (o != undefined && Array.isArray(o)) {
|
||||
o = this.getArrayValue(o, key);
|
||||
} else {
|
||||
o = o != undefined ? o[key] : undefined;
|
||||
}
|
||||
i++;
|
||||
if (o == undefined) return undefined;
|
||||
} while (i < paths.length);
|
||||
return o;
|
||||
},
|
||||
getArrayValue: function (o: any[], key: string) {
|
||||
if (o.length > 0) {
|
||||
let next:any = [];
|
||||
for (let i = 0; i < o.length; i++) {
|
||||
const it = o[i];
|
||||
if (it != undefined && Array.isArray(it)) {
|
||||
let vals = this.getArrayValue(it, key);
|
||||
if (vals != undefined && vals.length > 0) {
|
||||
for (let j = 0; j < vals.length; j++) {
|
||||
const val = vals[j];
|
||||
next.push(val);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let val = it[key];
|
||||
if (val != undefined && val != null) next.push(val);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default TypeTools;
|
||||
|
||||
Reference in New Issue
Block a user