common_util.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Created by CSL on 2017-06-06.
  3. * public functions.
  4. */
  5. function deleteEmptyObject(arr) {
  6. function isEmptyObject(e) {
  7. var t;
  8. for (t in e)
  9. return !1;
  10. return !0
  11. };
  12. for (var i = 0; i < arr.length; i++) {
  13. if (isEmptyObject(arr[i])) {
  14. arr.splice(i, 1);
  15. i = i - 1;
  16. };
  17. };
  18. };
  19. ((factory) => {
  20. if (typeof module !== 'undefined') {
  21. module.exports = factory();
  22. } else {
  23. window.commonUtil = factory();
  24. }
  25. })(() => {
  26. // 是否定义
  27. function isDef(val) {
  28. return typeof val !== 'undefined' && val !== null;
  29. }
  30. // 是否空值
  31. function isEmptyVal(val) {
  32. return val === null || val === undefined || val === '';
  33. }
  34. // 是否数值
  35. function isNumber(val) {
  36. return !isEmptyVal(val) && !isNaN(val);
  37. }
  38. // 是否近似相等(null = undefined = '', 1 = '1'...)
  39. function similarEqual(a, b) {
  40. // null == '' 为false,所以不能用非严等
  41. if (isEmptyVal(a) && isEmptyVal(b)) {
  42. return true;
  43. }
  44. return a == b;
  45. }
  46. // 递归获取必填项(基本信息、工程特征)
  47. function getRequired(rst, datas) {
  48. if (!datas) {
  49. return rst;
  50. }
  51. for (const data of datas) {
  52. const required = typeof data.required === 'string' ? JSON.parse(data.required) : data.required;
  53. const readOnly = typeof data.readOnly === 'string' ? JSON.parse(data.readOnly) : data.readOnly;
  54. if (required && !readOnly) {
  55. rst.push(data);
  56. }
  57. if (data.items && data.items.length) {
  58. getRequired(rst, data.items);
  59. }
  60. }
  61. return rst;
  62. }
  63. return {
  64. isDef,
  65. isEmptyVal,
  66. isNumber,
  67. similarEqual,
  68. getRequired,
  69. };
  70. });