index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. export const isValue = (val) => {
  2. if (val === null || val === undefined || (typeof val === 'string' && val.trim() === '') || (typeof val === 'object' && val?.length === 0)) {
  3. return false
  4. }
  5. return true
  6. }
  7. // 对象转驼峰命名
  8. export const objToCamel = (obj) => {
  9. for (let key in obj) {
  10. let newKey = key.replace(/_([a-z])/g, function($0, $1){ return $1.toUpperCase(); });
  11. if (newKey !== key) {
  12. obj[newKey] = obj[key];
  13. delete obj[key];
  14. }
  15. }
  16. }
  17. // 对象转下划线命名
  18. export const objToUnder = (obj) => {
  19. for (let key in obj) {
  20. let newKey = key.replace(/([A-Z])/g, '_$1').toLowerCase();
  21. if (newKey !== key) {
  22. obj[newKey] = obj[key];
  23. delete obj[key];
  24. }
  25. }
  26. }
  27. // 参数转驼峰命名
  28. export const paramToCamel = (param) => {
  29. return param.replace(/_([a-z])/g, function($0, $1){ return $1.toUpperCase(); });
  30. }
  31. // 参数转下划线命名
  32. export const paramToUnder = (param) => {
  33. return param.replace(/([A-Z])/g, '_$1').toLowerCase();
  34. }
  35. export const YMDHms = (date) => {
  36. const _date = new Date(date)
  37. const Y = `${_date.getFullYear()}`;
  38. const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  39. const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
  40. const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
  41. const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
  42. const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
  43. return `${Y}-${M}-${D} ${H}:${m}:${s}`;
  44. }
  45. export const YMD = (date, format = false) => {
  46. const _date = new Date(date)
  47. const Y = `${_date.getFullYear()}`;
  48. const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`;
  49. const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`;
  50. return format ? `${Y}年${M}月${D}日` : `${Y}-${M}-${D}`;
  51. }
  52. export const Hms = (date, format = false) => {
  53. const _date = new Date(date)
  54. const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`;
  55. const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`;
  56. const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds();
  57. return format ? `${H}时${m}分${s}秒` : `${H}:${m}:${s}`;
  58. }