Files
geg-gas-web/src/utils/type-tools.ts

79 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-01-19 16:43:02 +08:00
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]",
}
const TYPES = {
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);
}
};
export default TypeTools;
export { TYPES };