| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- // 变量类型判断
- export const TypeOfFun = (value: any) => {
- if (null === value) {
- return 'null';
- }
- const type = typeof value;
- if ('undefined' === type || 'string' === type) {
- return type;
- }
- const typeString = toString.call(value);
- switch (typeString) {
- case '[object Array]':
- return 'array';
- case '[object Date]':
- return 'date';
- case '[object Boolean]':
- return 'boolean';
- case '[object Number]':
- return 'number';
- case '[object Function]':
- return 'function';
- case '[object RegExp]':
- return 'regexp';
- case '[object Object]':
- if (undefined !== value.nodeType) {
- if (3 == value.nodeType) {
- return /\S/.test(value.nodeValue) ? 'textnode' : 'whitespace';
- } else {
- return 'element';
- }
- } else {
- return 'object';
- }
- default:
- return 'unknow';
- }
- };
- //
- export const objectToString = Object.prototype.toString;
- export const toTypeString = (value: unknown): string => objectToString.call(value);
- export const toRawType = (value: unknown): string => {
- // extract "RawType" from strings like "[object RawType]"
- return toTypeString(value).slice(8, -1);
- };
- export const isArray = Array.isArray;
- export const isMap = (val: unknown): val is Map<any, any> => toTypeString(val) === '[object Map]';
- export const isSet = (val: unknown): val is Set<any> => toTypeString(val) === '[object Set]';
- export const isDate = (val: unknown): val is Date => val instanceof Date;
- export const isFunction = (val: unknown): val is Function => typeof val === 'function';
- export const isString = (val: unknown): val is string => typeof val === 'string';
- export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol';
- export const isObject = (val: unknown): val is Record<any, any> => val !== null && typeof val === 'object';
- export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
- return isObject(val) && isFunction(val.then) && isFunction(val.catch);
- };
- export const getPropByPath = (obj: any, keyPath: string) => {
- try {
- return keyPath.split('.').reduce((prev, curr) => prev[curr], obj);
- } catch (error) {
- return '';
- }
- };
|