合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.8 KiB

  1. // @ts-nocheck
  2. import {isDef} from '../isDef'
  3. import {isString} from '../isString'
  4. import {isNumber} from '../isNumber'
  5. /**
  6. *
  7. *
  8. * 0
  9. * 0
  10. * 0
  11. * null或undefinedtrue
  12. *
  13. *
  14. * @param {any} value -
  15. * @returns {boolean} truefalse
  16. */
  17. // #ifdef UNI-APP-X && APP
  18. export function isEmpty(value : any | null) : boolean {
  19. // 为null
  20. if(!isDef(value)){
  21. return true
  22. }
  23. // 为空字符
  24. if(isString(value)){
  25. return value.toString().trim().length == 0
  26. }
  27. // 为数值
  28. if(isNumber(value)){
  29. return false
  30. }
  31. if(typeof value == 'object'){
  32. // 数组
  33. if(Array.isArray(value)){
  34. return (value as Array<unknown>).length == 0
  35. }
  36. // Map
  37. if(value instanceof Map<unknown, unknown>) {
  38. return value.size == 0
  39. }
  40. // Set
  41. if(value instanceof Set<unknown>) {
  42. return value.size == 0
  43. }
  44. if(value instanceof UTSJSONObject) {
  45. return value.toMap().size == 0
  46. }
  47. return JSON.stringify(value) == '{}'
  48. }
  49. return true
  50. }
  51. // #endif
  52. // #ifndef UNI-APP-X && APP
  53. export function isEmpty(value: any): boolean {
  54. // 检查是否为null或undefined
  55. if (value == null) {
  56. return true;
  57. }
  58. // 检查字符串是否为空
  59. if (typeof value === 'string') {
  60. return value.trim().length === 0;
  61. }
  62. // 检查数组是否为空
  63. if (Array.isArray(value)) {
  64. return value.length === 0;
  65. }
  66. // 检查对象是否为空
  67. if (typeof value === 'object') {
  68. return Object.keys(value).length === 0;
  69. }
  70. // 其他类型(如数字、布尔值等)不为空
  71. return false;
  72. }
  73. // #endif