util.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. export * from './interceptor';
  2. // 变量类型判断
  3. export const TypeOfFun = (value: any) => {
  4. if (null === value) {
  5. return 'null';
  6. }
  7. const type = typeof value;
  8. if ('undefined' === type || 'string' === type) {
  9. return type;
  10. }
  11. const typeString = toString.call(value);
  12. switch (typeString) {
  13. case '[object Array]':
  14. return 'array';
  15. case '[object Date]':
  16. return 'date';
  17. case '[object Boolean]':
  18. return 'boolean';
  19. case '[object Number]':
  20. return 'number';
  21. case '[object Function]':
  22. return 'function';
  23. case '[object RegExp]':
  24. return 'regexp';
  25. case '[object Object]':
  26. if (undefined !== value.nodeType) {
  27. if (3 == value.nodeType) {
  28. return /\S/.test(value.nodeValue) ? 'textnode' : 'whitespace';
  29. } else {
  30. return 'element';
  31. }
  32. } else {
  33. return 'object';
  34. }
  35. default:
  36. return 'unknow';
  37. }
  38. };
  39. //
  40. export const objectToString = Object.prototype.toString;
  41. export const toTypeString = (value: unknown): string => objectToString.call(value);
  42. export const toRawType = (value: unknown): string => {
  43. // extract "RawType" from strings like "[object RawType]"
  44. return toTypeString(value).slice(8, -1);
  45. };
  46. export const isArray = Array.isArray;
  47. export const isMap = (val: unknown): val is Map<any, any> => toTypeString(val) === '[object Map]';
  48. export const isSet = (val: unknown): val is Set<any> => toTypeString(val) === '[object Set]';
  49. export const isDate = (val: unknown): val is Date => val instanceof Date;
  50. export const isFunction = (val: unknown): val is Function => typeof val === 'function';
  51. export const isString = (val: unknown): val is string => typeof val === 'string';
  52. export const isSymbol = (val: unknown): val is symbol => typeof val === 'symbol';
  53. export const isObject = (val: unknown): val is Record<any, any> => val !== null && typeof val === 'object';
  54. export const isPromise = <T = any>(val: unknown): val is Promise<T> => {
  55. return isObject(val) && isFunction(val.then) && isFunction(val.catch);
  56. };
  57. export const win = window;
  58. export const docu = document;
  59. export const body = docu.body;
  60. export const getPropByPath = (obj: any, keyPath: string) => {
  61. try {
  62. return keyPath.split('.').reduce((prev, curr) => prev[curr], obj);
  63. } catch (error) {
  64. return '';
  65. }
  66. };
  67. export const floatData = (format: any, dataOp: any, mapOps: any) => {
  68. let mergeFormat = Object.assign({}, format);
  69. let mergeMapOps = Object.assign({}, mapOps);
  70. if (Object.keys(dataOp).length > 0) {
  71. Object.keys(mergeFormat).forEach((keys) => {
  72. if (mergeMapOps.hasOwnProperty(keys)) {
  73. const tof = TypeOfFun(mergeMapOps[keys]);
  74. if (tof == 'function') {
  75. mergeFormat[keys] = mergeMapOps[keys](dataOp);
  76. }
  77. if (tof == 'string') {
  78. mergeFormat[keys] = dataOp[mergeMapOps[keys]];
  79. }
  80. } else {
  81. if (dataOp[keys]) mergeFormat[keys] = dataOp[keys];
  82. }
  83. });
  84. return mergeFormat;
  85. }
  86. return format;
  87. };
  88. export const deepMerge = (target: any, newObj: any) => {
  89. Object.keys(newObj).forEach((key) => {
  90. let targetValue = target[key];
  91. let newObjValue = newObj[key];
  92. if (isObject(targetValue) && isObject(newObjValue)) {
  93. deepMerge(targetValue, newObjValue);
  94. } else {
  95. target[key] = newObjValue;
  96. }
  97. });
  98. return target;
  99. };
  100. export function myFixed(num: any, digit: number = 2) {
  101. if (Object.is(parseFloat(num), NaN)) {
  102. return console.log(`传入的值:${num}不是一个数字`);
  103. }
  104. num = parseFloat(num);
  105. return (Math.round((num + Number.EPSILON) * Math.pow(10, digit)) / Math.pow(10, digit)).toFixed(digit);
  106. }
  107. export function preventDefault(event: Event, isStopPropagation?: boolean) {
  108. if (typeof event.cancelable !== 'boolean' || event.cancelable) {
  109. event.preventDefault();
  110. }
  111. if (isStopPropagation) {
  112. event.stopPropagation();
  113. }
  114. }
  115. export const padZero = (num: number | string, length = 2): string => {
  116. num += '';
  117. while ((num as string).length < length) {
  118. num = '0' + num;
  119. }
  120. return num.toString();
  121. };
  122. export const clamp = (num: number, min: number, max: number): number => Math.min(Math.max(num, min), max);
  123. export const getScrollTopRoot = (): number => {
  124. return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  125. };