commonUtil.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. let commonUtil = {};
  2. /**
  3. * @method isType 判断对象类型
  4. * @param obj {Object} 需要判断的对象
  5. * @param type {String} 类型字符串
  6. * @returns {Boolean}
  7. * */
  8. commonUtil.isType = (obj, type) => {
  9. return Object.prototype.toString.call(obj) === '[object ' + type + ']';
  10. };
  11. /**
  12. * @method isArray 判断该对象是否是数组
  13. * @param obj {Object} 需要判断的对象
  14. * @returns {Boolean}
  15. * */
  16. commonUtil.isArray = (obj) => {
  17. return commonUtil.isType(obj, 'Array');
  18. };
  19. /**
  20. * @method isStr 判断该对象是否是字符串
  21. * @param obj {Object} 需要判断的对象
  22. * @returns {Boolean}
  23. * */
  24. commonUtil.isStr = (obj) => {
  25. return commonUtil.isType(obj, 'String');
  26. };
  27. /**
  28. * @method isObject 判断该对象是否是Object
  29. * @param obj {Object} 需要判断的对象
  30. * @returns {Boolean}
  31. * */
  32. commonUtil.isObject = (obj) => {
  33. return commonUtil.isType(obj, 'Object');
  34. };
  35. /**
  36. * @method isFun 判断该对象是否是函数
  37. * @param obj {Object} 需要判断的对象
  38. * @returns {Boolean}
  39. * */
  40. commonUtil.isFun = (obj) => {
  41. return commonUtil.isType(obj, 'Function');
  42. };
  43. /**
  44. * @method isJson 判断该对象是否是json
  45. * @param obj {Object} 需要判断的对象
  46. * @returns {Boolean}
  47. * */
  48. commonUtil.isNull = (obj) => {
  49. return commonUtil.isType(obj, 'Null');
  50. };
  51. /**
  52. * @method isJson 判断该对象是否是json
  53. * @param obj {Object} 需要判断的对象
  54. * @returns {Boolean}
  55. * */
  56. commonUtil.isJson = (obj) => {
  57. return commonUtil.isObject(obj) && !commonUtil.isNull(obj) && !commonUtil.isArray(obj);
  58. };
  59. /**
  60. * @method isJson 判断该对象是否是json
  61. * @param str {Object} 需要判断的对象
  62. * @returns {str | JSON.parse(str)}
  63. * */
  64. commonUtil.isJsonStr = (str) => {
  65. try {
  66. return JSON.parse(str);
  67. } catch (error) {
  68. return str;
  69. }
  70. };
  71. /**
  72. * 判断值是否为空
  73. * @param value 值
  74. * @returns {boolean}
  75. */
  76. commonUtil.isValueEmpty = (value) => {
  77. if (value === null || value === undefined) {
  78. return true;
  79. }
  80. if (typeof value === "string" && value.trim() === "") {
  81. return true;
  82. }
  83. if (Array.isArray(value) && value.length === 0) {
  84. return true;
  85. }
  86. if (
  87. typeof value === "object" &&
  88. !Array.isArray(value) &&
  89. Object.keys(value).length === 0
  90. ) {
  91. return true;
  92. }
  93. if (typeof value === "symbol" && value.toString() === "Symbol()") {
  94. return true;
  95. }
  96. return false;
  97. }
  98. /**
  99. * 获取表单数据中有值的数据
  100. * @param formData 表单数据
  101. * @returns {T}
  102. */
  103. commonUtil.getFromValue = (formData) => {
  104. const result = {};
  105. for (const key in formData) {
  106. if (!commonUtil.isValueEmpty(formData[key])) {
  107. result[key] = formData[key];
  108. }
  109. }
  110. return result;
  111. }
  112. export default commonUtil;