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

115 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-01-19 16:43:02 +08:00
const OBJ_TYPE = {
2026-01-29 18:39:46 +08:00
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]",
2026-01-19 16:43:02 +08:00
}
const TYPES = {
2026-01-29 18:39:46 +08:00
BOOLEAN: "BOOLEAN",
NUMBER: "NUMBER",
STRING: "STRING",
DATE: "DATE",
REGEXP: "REGEXP",
NULL: "NULL",
OBJ: "OBJ",
ARRAY: "ARRAY",
JSON: "JSON",
FUNCTION: "FUNCTION",
UNKNOWN: "UNKNOWN",
2026-01-19 16:43:02 +08:00
}
const TypeTools = {
2026-01-29 18:39:46 +08:00
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;
2026-01-19 16:43:02 +08:00
}
2026-01-29 18:39:46 +08:00
},
2026-01-19 16:43:02 +08:00
};
export default TypeTools;
export { TYPES };