index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import dayjs from 'dayjs'
  2. export const getTime = (str) => {
  3. let time = dayjs()
  4. const arr = str.split(':')
  5. if (arr.length < 3) {
  6. arr.push('00')
  7. }
  8. arr.forEach((el, i) => i === 0 ? (time = time.hour(el)) : (i === 1 ? (
  9. time = time.minute(el)) : (time = time.second(el))))
  10. return time
  11. }
  12. export const getRestTime = (time) => {
  13. let hour = 0,
  14. minute = 0,
  15. second = 0
  16. if (time) {
  17. const endTime = getTime(time)
  18. const now = dayjs()
  19. hour = endTime.diff(now, 'hour')
  20. minute = endTime.diff(now, 'minute') - hour * 60
  21. second = endTime.diff(now, 'second') - hour * 60 * 60 - minute * 60
  22. }
  23. return [hour, minute, second]
  24. }
  25. export function parseURL(url) {
  26. const regex = /^(https?):\/\/([^:/?]+)(?::(\d+))?([^?#]+)?(\?[^#]+)?(#.+)?$/;
  27. const match = url.match(regex);
  28. if (!match) return null;
  29. const [, protocol, host, port, path, query, fragment] = match;
  30. let params = {}
  31. if (query) {
  32. const arr = query.slice(1).split('&')
  33. arr.forEach(el => {
  34. const _ = el.split('=')
  35. params[_[0]] = _[1]
  36. })
  37. }
  38. return {
  39. protocol: protocol,
  40. host: host,
  41. port: port ? parseInt(port) : null,
  42. path: path || null,
  43. query: query ? query.slice(1) : null,
  44. fragment: fragment ? fragment.slice(1) : null,
  45. params
  46. };
  47. }