is.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // copy to vben-admin
  2. const toString = Object.prototype.toString
  3. export const is = (val, type) => {
  4. return toString.call(val) === `[object ${type}]`
  5. }
  6. export const isDef = (val) => {
  7. return typeof val !== 'undefined'
  8. }
  9. export const isUnDef = (val) => {
  10. return !isDef(val)
  11. }
  12. export const isObject = (val) => {
  13. return val !== null && is(val, 'Object')
  14. }
  15. export const isEmpty1 = (value) => {
  16. if (value === '' || value === undefined || value === null){
  17. return true;
  18. }
  19. if (isArray(value)) {
  20. return value.length === 0;
  21. }
  22. if (isObject(value)) {
  23. return Object.keys(value).length === 0;
  24. }
  25. return false
  26. }
  27. export const isEmpty = (val) => {
  28. if (val === null) {
  29. return true
  30. }
  31. if (isArray(val) || isString(val)) {
  32. return val.length === 0
  33. }
  34. if (val instanceof Map || val instanceof Set) {
  35. return val.size === 0
  36. }
  37. if (isObject(val)) {
  38. return Object.keys(val).length === 0
  39. }
  40. return false
  41. }
  42. export const isDate = (val) => {
  43. return is(val, 'Date')
  44. }
  45. export const isNull = (val) => {
  46. return val === null
  47. }
  48. export const isNullAndUnDef = (val) => {
  49. return isUnDef(val) && isNull(val)
  50. }
  51. export const isNullOrUnDef = (val) => {
  52. return isUnDef(val) || isNull(val)
  53. }
  54. export const isNumber = (val) => {
  55. return is(val, 'Number')
  56. }
  57. export const isPromise = (val) => {
  58. return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch)
  59. }
  60. export const isString = (val) => {
  61. return is(val, 'String')
  62. }
  63. export const isFunction = (val) => {
  64. return typeof val === 'function'
  65. }
  66. export const isBoolean = (val) => {
  67. return is(val, 'Boolean')
  68. }
  69. export const isRegExp = (val) => {
  70. return is(val, 'RegExp')
  71. }
  72. export const isArray = (val) => {
  73. return val && Array.isArray(val)
  74. }
  75. export const isWindow = (val) => {
  76. return typeof window !== 'undefined' && is(val, 'Window')
  77. }
  78. export const isElement = (val) => {
  79. return isObject(val) && !!val.tagName
  80. }
  81. export const isMap = (val) => {
  82. return is(val, 'Map')
  83. }
  84. export const isServer = typeof window === 'undefined'
  85. export const isClient = !isServer
  86. // export const isUrl = (path) => {
  87. // const reg =
  88. // /(((^https?:(?:\/\/)?)(?:[-:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&%@.\w_]*)#?(?:[\w]*))?)$/
  89. // return reg.test(path)
  90. // }
  91. export const isDark = () => {
  92. return window.matchMedia('(prefers-color-scheme: dark)').matches
  93. }
  94. // 是否是图片链接
  95. export const isImgPath = (path) => {
  96. return /(https?:\/\/|data:image\/).*?\.(png|jpg|jpeg|gif|svg|webp|ico)/gi.test(path)
  97. }
  98. export const isEmptyVal = (val) => {
  99. return val === '' || val === null || val === undefined
  100. }