path-utils.js 910 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Whether path is the top page
  3. * @param {string} path
  4. * @returns {boolean}
  5. */
  6. const isTopPage = (path) => {
  7. return path === '/';
  8. };
  9. /**
  10. * Whether path belongs to the trash page
  11. * @param {string} path
  12. * @returns {boolean}
  13. */
  14. const isTrashPage = (path) => {
  15. // https://regex101.com/r/BSDdRr/1
  16. if (path.match(/^\/trash(\/.*)?$/)) {
  17. return true;
  18. }
  19. return false;
  20. };
  21. /**
  22. * Whether path belongs to the user page
  23. * @param {string} path
  24. * @returns {boolean}
  25. */
  26. const isUserPage = (path) => {
  27. // https://regex101.com/r/SxPejV/1
  28. if (path.match(/^\/user(\/.*)?$/)) {
  29. return true;
  30. }
  31. return false;
  32. };
  33. /**
  34. * return user path
  35. * @param {Object} user
  36. * @return {string}
  37. */
  38. const userPageRoot = (user) => {
  39. if (!user || !user.username) {
  40. return '';
  41. }
  42. return `/user/${user.username}`;
  43. };
  44. module.exports = {
  45. isTopPage,
  46. isTrashPage,
  47. isUserPage,
  48. userPageRoot,
  49. };